Skip to content

Encode structured Fields without allocations, byte-equal to encoding/json - #49

Merged
eitamring merged 2 commits into
mainfrom
perf/structured-sink
Jul 11, 2026
Merged

Encode structured Fields without allocations, byte-equal to encoding/json#49
eitamring merged 2 commits into
mainfrom
perf/structured-sink

Conversation

@eitamring

Copy link
Copy Markdown
Contributor

Last code PR of the audit series (#47, #48). The structured output path (records carrying decoded Fields) went through encoding/json at ~1.6us and 10 allocs per record; the plain path was already zero-alloc.

  • Hand-rolled fast path for the value shapes the pipeline produces (string, json.Number, bool, nil, nested maps and arrays), with sorted keys and escaping matching encoding/json exactly. Measured: 732-794ns, 0 B/op, 0 allocs (count=5); plain-record path unchanged.
  • Anything the fast path cannot prove equivalent rewinds and falls back to encoding/json: unsupported types, non-finite floats, malformed json.Number, and nesting past a depth bound of 64. The bound counts arrays as well as objects, so cyclic maps and cyclic slices reach the fallback instead of overflowing the stack; deep acyclic values encode identically, cycles return the reference error.
  • Key buffers release their string references on every exit, keeping capacity but never retaining caller maps.
  • Tests: differential fuzz vs encoding/json (3M execs clean), cycle and depth-boundary regression tests, golden and escaping suites unchanged.

vet, test, and race pass; the 1M-line demos are byte-identical to main.

@eitamring
eitamring force-pushed the perf/structured-sink branch from 9eb3c80 to fd92875 Compare July 11, 2026 07:11
@eitamring
eitamring force-pushed the perf/structured-sink branch from fd92875 to 850035c Compare July 11, 2026 07:12
@eitamring
eitamring merged commit 558cbf7 into main Jul 11, 2026
4 checks passed
@eitamring
eitamring deleted the perf/structured-sink branch July 11, 2026 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant