You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Go SDK: ExtractExtraProperties decodes every object three times #18118
In SDKs from fern-go-sdk, every generated UnmarshalJSON calls internal.ExtractExtraProperties with the bytes it has just decoded into the struct. As a result, each object in a response is decoded three times:
into the struct, by UnmarshalJSON itself;
by json.Unmarshal(bytes, &value) inside ExtractExtraProperties. value is a struct held in an interface{}, so encoding/json replaces it with a fresh map[string]any, and that result is never used;
into map[string]any once more, to find the extra properties.
Nested objects repeat this at every level, so one page of a listing is decoded several times over. The declared keys are also recomputed by reflection on every call.
Impact
We profiled one call to a listing endpoint through a generated SDK (fern-go-sdk 1.58.1): a page of 50 objects, each with four nested objects. ExtractExtraProperties accounts for 83.5% of the bytes allocated by the whole call. Decoding that page costs about 1.2 ms, 960 KiB and 20.9k allocations on Go 1.27.
Proposal
Validate the object once with json.Valid, which does not allocate, then scan it in place. Compare each property name as it appears in the input, skip a declared property's value without reading it, and decode only the extra properties. Cache the declared keys per type. The extra properties returned stay exactly the same. This uses encoding/json only, so it keeps working on every Go version the SDKs support. The implementation and benchmarks are in #18119.
Activity
fix(go): find extra properties in one allocation-free scan