mirror of
https://github.com/soypat/lneto.git
synced 2026-08-26 01:19:06 +00:00
Reduce heap allocations (#42)
* pcap: reuse Frame memory * slog: reduce heap allocations of addresses; also prevent heap alloc of dhcp options in pcap * dns: heapless improvement; add StackAsync buffer for more heapless operation; start thinking of errors * reuse function for reclaiming frames among all protocols for lower memory usage * remove extraneous code
This commit is contained in:
@@ -46,3 +46,22 @@ func SliceReuse[T any](buf *[]T, n int) {
|
||||
*buf = (*buf)[:0]
|
||||
}
|
||||
}
|
||||
|
||||
// SliceReclaim extends the slice length by one and returns a pointer to
|
||||
// the new last element. The returned element is not zeroed, so callers
|
||||
// can reuse any existing allocations it may hold from prior use.
|
||||
//
|
||||
// Beware: as the name implies SliceReclaim reuses previously held memory
|
||||
// so it is up to caller to ensure the reclaimed data is coherent by zeroing out
|
||||
// the needed fields and reusing the previously held slices or pointers responsibly.
|
||||
func SliceReclaim[T any](ptr *[]T) *T {
|
||||
b := *ptr
|
||||
n := len(b)
|
||||
if n == cap(b) {
|
||||
var z T
|
||||
*ptr = append(b, z)
|
||||
} else {
|
||||
*ptr = b[:n+1]
|
||||
}
|
||||
return &(*ptr)[n]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user