Skip to content

perf(profiler): reserve(1+|pids|) for errs slice — eliminate O(log n) reallocations in Flush#97

Open
KorsarOfficial wants to merge 1 commit into
yandex:mainfrom
KorsarOfficial:perf/preallocate-error-slice
Open

perf(profiler): reserve(1+|pids|) for errs slice — eliminate O(log n) reallocations in Flush#97
KorsarOfficial wants to merge 1 commit into
yandex:mainfrom
KorsarOfficial:perf/preallocate-error-slice

Conversation

@KorsarOfficial
Copy link
Copy Markdown

@KorsarOfficial KorsarOfficial commented Mar 18, 2026

Closes #92

Complexity analysis

Let k = len(c.p.pids) (number of tracked PIDs).

Before After
Initial capacity 0 1 + k
append reallocations ⌈log₂(1 + k)⌉ 0
Heap allocations 1 + ⌈log₂(1+k)⌉ 1

The standard Go append doubling strategy means make([]error, 0) triggers reallocations at sizes 1, 2, 4, 8 … up to 1+k. With a correct capacity hint the backing array is allocated once at the final size.

Change

// Before — O(log k) reallocations
errs := make([]error, 0)

// After — O(1) single allocation, capacity = worst-case append count
errs := make([]error, 0, 1+len(c.p.pids))

The bound 1 + len(c.p.pids) is tight: at most one error from wholeSystem flush + one per PID consumer.

@KorsarOfficial KorsarOfficial force-pushed the perf/preallocate-error-slice branch from 42fbc7a to d82d914 Compare March 18, 2026 20:55
@KorsarOfficial
Copy link
Copy Markdown
Author

📄 Full analysis report (PDF): 08-perforator-optimizations.pdf

Covers complexity analysis, concurrency audit, and verification for all 7 optimizations.

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.

perf(agent): pre-size error slice in Flush to avoid repeated reallocation

1 participant