Benchmarking the Marshaler-interface commit (#1084) against v2 on
linux/amd64 (interleaved rounds, n=12, benchstat) showed statistically
significant sec/op regressions on the default (interface-disabled)
Marshal path — up to +10% on micro-benchmarks — with allocations
unchanged. Bisecting the diff piece by piece isolated the dominant
cause: widening the per-type typeEncProps cache struct from 2 to 3
bytes. Its cached p.(typeEncProps) assertion and by-value return run
two to three times per encoded value, and the extra field changes the
copy size and register shape of every lookup; that lone change
reproduced the regression with zero encoder-code modifications.
- typeEncProps and encPropsForType are restored to their exact baseline
form. The unstable.Marshaler classification lives in its own
sync.Map (typeMarshalerCache/marshalerPropsForType), consulted only
on EnableMarshalerInterface paths.
- encodeTable, encodeKeyValue and encodeArrayTable are restored to
their exact baseline source. The Marshaler-aware variants of the
table passes live in encodeTableWithMarshalers and
encodeArrayTableWithMarshalers, selected by one branch per table
instead of one per entry. Marshaler values on a `key =` line flow
through appendValue's gated guard, which returns without rejoining
the fall-through path and re-classifies the fetched bytes (keeping
the emit-time guard), so appendMarshalerInlineValue is gone.
- All Marshaler-interface machinery moves to a new marshaler_raw.go so
the feature's cold code does not sit between the default path's hot
functions.
After this change the interface-off encoder differs from baseline only
by four single-branch dispatches; typeEncProps, encPropsForType and the
hot encoder functions are byte-identical to the pre-#1084 source.
allocs/op are identical across the whole ./benchmark suite. Remaining
µs-scale sec/op deltas are within this host's address-alignment noise
floor, demonstrated by dead-code A/A controls and -randlayout runs.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
What
Follow-up to #1084 (
Encoder.EnableMarshalerInterface). Benchmarking that feature against v2 on linux/amd64 (idle host, pinned cores, interleaved rounds, n=12, benchstat) showed statistically significant sec/op regressions on the default (interface-disabled) Marshal path — up to +10% on micro-benchmarks — with allocations unchanged.Root cause
Bisecting the #1084 diff piece by piece isolated the dominant cost: widening the per-type
typeEncPropscache struct from 2 to 3 bytes (the addedmarshaler uint8classification). Its cachedp.(typeEncProps)assertion and by-value return run 2–3× per encoded value (viaisValueKindand the TextMarshaler switch), and the extra field changes the copy size and register-ABI shape of every lookup. That lone change — with zero encoder-code modifications — reproduced the regression (geomean +3.8% on the Marshal micro set); padding the struct to 4 bytes only halved it.Changes
typeEncProps/encPropsForTyperestored to their exact baseline form; theunstable.Marshalerclassification lives in its ownsync.Map(typeMarshalerCache/marshalerPropsForType), consulted only onEnableMarshalerInterfacepaths.encodeTable,encodeKeyValue,encodeArrayTablerestored to their exact baseline source; the Marshaler-aware table passes live inencodeTableWithMarshalers/encodeArrayTableWithMarshalers, selected by one branch per table instead of one per entry. Marshaler values on akey =line flow throughappendValue's gated guard, which returns without rejoining the fall-through path and re-classifies the fetched bytes (keeping the emit-time guard against non-deterministic marshalers).marshaler_raw.go, out from between the default path's hot functions.After this change the interface-off encoder differs from the pre-#1084 source only by four single-branch dispatches;
typeEncProps,encPropsForTypeand the hot encoder functions are byte-identical to baseline. No behavior change: the #1084 test suite passes unmodified.Measurements
Full benchstat posted as a collapsed comment below (and on #1084). Summary: allocs/op identical across the whole
./benchmarksuite; ms-scale dataset benchmarks unchanged; remaining µs-scale sec/op deltas are within the measurement host's address-alignment noise floor (demonstrated with dead-code A/A controls and-ldflags=-randlayoutruns).🤖 Generated with Claude Code