Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,20 @@ func (fs *fields) OmitEmpty(e *Encoder, strct reflect.Value) []*field {
return fs.List
}

fields := make([]*field, 0, len(fs.List))
// First pass: count surviving fields. If all survive, return the
// original slice without allocating a filtered copy.
n := 0
for _, f := range fs.List {
if !f.Omit(e, strct) {
n++
}
}
if n == len(fs.List) {
return fs.List
}

// Second pass: build the filtered slice only when necessary.
fields := make([]*field, 0, n)
for _, f := range fs.List {
if !f.Omit(e, strct) {
fields = append(fields, f)
Expand Down