Skip to content

Commit

Permalink
allocate slice with struct once and decode into every item (#71)
Browse files Browse the repository at this point in the history
otherwise every separate struct is allocated on heap, and later is copied into original array.
  • Loading branch information
dshulyak authored Sep 15, 2023
1 parent 5f4f279 commit 12f1fe1
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,13 @@ func DecodeStructSliceWithLimit[V any, H DecodablePtr[V]](d *Decoder, limit uint
return nil, total, nil
}

value := make([]V, 0, lth)
value := make([]V, lth)
for i := uint32(0); i < lth; i++ {
val, n, err := DecodeStruct[V, H](d)
n, err := H(&value[i]).DecodeScale(d)
total += n
if err != nil {
return nil, total, err
}
value = append(value, val)
}
return value, total, nil
}
Expand Down

0 comments on commit 12f1fe1

Please sign in to comment.