Skip to content

Observations as JSON number lists dominate step time; hex-encoded float32 is 2.3x faster with 100 agents #267

Backlog

Description

@kodexArg

Summary

Every training step, the Sync node sends each agent's observation as a JSON list of numbers (JSON.stringify in Godot, json.loads + np.array in Python). Writing and parsing thousands of floats as text is the dominant cost of a step as soon as there are more than a few agents. Sending the float32 bytes of the observation as a hex string, the same trick already used for *2d* keys, makes the step 2.3x faster with 100 agents in an end-to-end test with the real plugin and package, with bit-for-bit identical observations.

Measurements

Setup: AMD Ryzen 5 4600G, Linux, Godot 4.7.2 headless, Python 3.14, numpy 2.5. Each agent sends a 69-float observation. Each case ran 3 times for 1,500 steps, and the first 100 steps were discarded. The table shows the median step time seen from GodotEnv.step (median of the 3 medians, with the range of the 3 runs).

Real plugin Sync + real GodotEnv (--speedup=1000 --action_repeat=1 --fixed-fps 60000, so physics pacing does not hide the transport cost):

Agents JSON lists (today) Hex float32 (proposed) Speedup
1 207 µs (193–232) 172 µs (169–173) 1.2x
10 666 µs (665–684) 448 µs (425–467) 1.5x
100 5,435 µs (5,254–5,604) 2,355 µs (2,326–2,357) 2.3x

Every 50th step, the decoded observations were compared with the expected values: they matched bit for bit in both modes.

Where the time goes (100 agents, protocol-identical harness, Godot side):

Step JSON lists Hex float32
Godot builds and sends the step message 2,805 µs 197 µs
Godot parses the action message (JSON, unchanged) 162 µs 149 µs

Micro-benchmarks for the same 100 × 69 floats in Godot 4.7.2: hex_encode() takes 23 µs, while Marshalls.raw_to_base64() takes 159 µs. That is why the proposal uses hex rather than base64, and it matches what *2d* keys already do.

Proposal

  • Plugin: a binary_observations export on Sync, also settable with --binary_observations, off by default. When it is on, each float observation (Array, PackedFloat32Array or PackedFloat64Array) in the training path becomes PackedFloat32Array(value).to_byte_array().hex_encode(). Keys containing 2d and ONNX inference are unchanged.
  • Package: in GodotEnv._process_obs, a non-2d key whose value is a string is decoded with np.frombuffer(bytes.fromhex(s), dtype="<f4").reshape(shape). Plugins that send lists keep working unchanged.

The observation space already declares these keys as float32 Boxes, so the network sees exactly the same values. Doubles from GDScript are rounded to float32 once in Godot instead of in numpy.

Notes

  • to_byte_array() uses the native byte order. The decoder assumes little-endian, which covers the platforms Godot RL Agents targets.
  • With observations solved, the next per-step cost is the action message, which is still JSON. It is out of scope here.
  • Sending all agents' observations in a single string, instead of one per agent, saves more: 1,573 µs against 2,355 µs per step with 100 agents in the harness. That changes the message shape, so it is left as a possible follow-up.

PRs:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions