Skip to content

ThreadsafeCounterDictionary allocates a CounterItem on every recorded drop #5686

Closed

Description

@jamescrosswell

Part of #5634.

Branch: main. No public API change, so this doesn't need to wait for v7.

ThreadsafeCounterDictionary.Add and Increment call _items.GetOrAdd(key, new CounterItem()) (ThreadsafeCounterDictionary.cs:27,33). That allocates a CounterItem on every call, even when the key already exists. #5607 adds call sites on hot paths, such as every dropped log or metric.

Measured: a dropped log went from 0 B to 24 B allocated. The factory overload, GetOrAdd(key, static _ => new CounterItem()), brings it back to 0 B. CollectionsExtensions.cs:10 already uses the factory overload.

Activity

  1. linear-code commented on Oct 7, 2026

    @linear-code
  2. added a commit that references this issue on Oct 8, 2026
    803adc0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETPull requests that update .net codeBugSomething isn't workingOptimization

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions