Skip to content

Commit 5f99f9b

Browse files
authored
perf: reduce allocation in MemoizeFields for common cases (#1726)
## Which problem is this PR solving? All key fields are memoized during ingest, `MemoizeFields` are called right before making a sampling decision. Its purpose is to make sure key fields are memoized once even after a config reload. Config reload should be rare. We don't need to pre-allocate the `keysToFind` slice during normal operation. ## Short description of the changes - don't pre-allocate `keysToFind` for common cases
1 parent b178b80 commit 5f99f9b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

types/payload.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,14 @@ func (p *Payload) MemoizeFields(keys ...string) {
568568
p.missingFields = make(map[string]struct{}, len(keys))
569569
}
570570

571-
keysToFind := make(map[string]struct{}, len(keys))
571+
// It is rare for a key field to not be memoized.
572+
// Intentionally not allocating memory for keysToFind because it is rarely needed.
573+
// It is worth the compute cost to grow this map on those rare occassions instead
574+
// of allocating memory we rarely use.
575+
// CAUTION: This optimization is under the assumption that MemoizeFields() are only
576+
// called after the first memoization operation. If this assumption ever changes,
577+
// we should reevaluate this optimization
578+
keysToFind := make(map[string]struct{})
572579
for _, key := range keys {
573580
if _, ok := p.missingFields[key]; ok {
574581
continue

0 commit comments

Comments
 (0)