Skip to content

perf(encode): skip OmitEmpty slice allocation when no fields omitted#49

Merged
xe-nvdk merged 1 commit intov6from
perf/omitempty-two-pass
Mar 3, 2026
Merged

perf(encode): skip OmitEmpty slice allocation when no fields omitted#49
xe-nvdk merged 1 commit intov6from
perf/omitempty-two-pass

Conversation

@xe-nvdk
Copy link
Member

@xe-nvdk xe-nvdk commented Mar 3, 2026

Summary

  • Two-pass OmitEmpty: first counts surviving fields, returns fs.List directly if all survive (zero allocs)
  • Only allocates a filtered []*field slice when fields are actually omitted
  • Common fast path for time-series data where most struct fields have values

Details

Before this change, every struct encode with omitempty tags (or SetOmitEmpty(true)) allocated a new []*field slice even when no fields were empty. This is wasteful since the common case is all fields populated. The two-pass approach avoids this allocation entirely when no fields are omitted.

Test plan

  • All existing tests pass (including TestSetOmitEmpty, TestOmitEmptyUnexportedFields)
  • Benchmarks verified — no regressions

Two-pass approach: first counts surviving fields, returns fs.List
directly if all survive (zero allocs). Only allocates the filtered
slice when fields are actually omitted. Common case for time-series
data where most fields have values.
@xe-nvdk xe-nvdk merged commit 54f8276 into v6 Mar 3, 2026
3 checks passed
@xe-nvdk xe-nvdk deleted the perf/omitempty-two-pass branch March 3, 2026 01:44
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