Skip to content

perf: reuse counter snapshot buffer in CounterGroup.OnTimer#127886

Open
unsafePtr wants to merge 1 commit intodotnet:mainfrom
unsafePtr:perf/countergroup-buffer-reuse
Open

perf: reuse counter snapshot buffer in CounterGroup.OnTimer#127886
unsafePtr wants to merge 1 commit intodotnet:mainfrom
unsafePtr:perf/countergroup-buffer-reuse

Conversation

@unsafePtr
Copy link
Copy Markdown
Contributor

CounterGroup.OnTimer allocates a fresh DiagnosticCounter[] snapshot every poll to copy the counter list. In practice the counter set is fixed at EventSource construction and rarely changes. Reuse a per-instance buffer instead, grow on demand, and clear trailing slots for disposed counters.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 6, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @dotnet/area-system-diagnostics-tracing
See info in area-owners.md if you want to be subscribed.

_counters.CopyTo(counters);

counterCount = _counters.Count;
if (_onTimerCounters.Length < counterCount)
Copy link
Copy Markdown
Member

@tarekgh tarekgh May 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

race condition can be raised? The _onTimerCounters is allocated/resized and then filled using _counters.CopyTo inside the s_counterGroupLock but later it is used outside the lock. This means theoretically _onTimerCounters or its contents can be changed before executing the next foreach loop.

I would suggest instead of doing that, we can use ArrayPool?

@tarekgh tarekgh requested a review from noahfalk May 6, 2026 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Diagnostics.Tracing community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants