Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ConcurrentQueue<T> in BackgroundWorker #3355

Merged
merged 18 commits into from
May 9, 2024

Conversation

jamescrosswell
Copy link
Collaborator

@jamescrosswell jamescrosswell commented May 8, 2024

Resolves #2516

That issue has quite a few messages in the history but see here for what finally gave rise to this PR:

Memory profile

Before this PR

image

After this PR

image

Performance

I was a worried about performance using a bespoke concurrent queue but we seem to have pretty comparable performance to ConcurrentQueue<T> in our specific use case (a bit better in fact).

Method N Mean Error StdDev Gen0 Gen1 Gen2 Allocated
ConcurrentQueueLiteAsync 1000 4.106 ms 0.0962 ms 0.2835 ms 1074.2188 507.8125 23.4375 2.18 MB
ConcurrentQueueAsync 1000 5.073 ms 0.1282 ms 0.3781 ms 1062.5000 515.6250 125.0000 2.21 MB

I tried a few different variations of the code (using a LinkedList<T> and Queue<T> for the internal queue) bit plain old List<T> turned out to be the fastest by quite a margin (and use the least memory).

@jamescrosswell jamescrosswell marked this pull request as ready for review May 8, 2024 09:49
@jamescrosswell jamescrosswell changed the title Spike: Replace ConcurrentQueue<T> in BackgroundWorker Replace ConcurrentQueue<T> in BackgroundWorker May 8, 2024
Copy link
Collaborator

@vaind vaind left a comment

Choose a reason for hiding this comment

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

Looks pretty good to me except for the tests that are a bit sparse. I've added som suggestions although they're not exhaustive.

test/Sentry.Tests/Internals/ConcurrentQueueLiteTests.cs Outdated Show resolved Hide resolved
src/Sentry/Internal/ConcurrentQueueLite.cs Outdated Show resolved Hide resolved
benchmarks/Sentry.Benchmarks/ConcurrentQueueBenchmarks.cs Outdated Show resolved Hide resolved
benchmarks/Sentry.Benchmarks/ConcurrentQueueBenchmarks.cs Outdated Show resolved Hide resolved
src/Sentry/Internal/BackgroundWorker.cs Outdated Show resolved Hide resolved
modules/sentry-cocoa Outdated Show resolved Hide resolved
@jamescrosswell jamescrosswell requested a review from vaind May 9, 2024 09:08
Copy link
Collaborator

@vaind vaind left a comment

Choose a reason for hiding this comment

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

LGTM except for the unrelated sentry-cocoa change.

Also, should we change breadcrumbs too in a followup PR? That's the only other place we use concurent queue in (other than tests)

@vaind vaind merged commit adb6cfb into main May 9, 2024
13 checks passed
@vaind vaind deleted the fix/concurrentqueue-memory-leak branch May 9, 2024 10:11
@bruno-garcia
Copy link
Member

Can we release this asap?

@bruno-garcia
Copy link
Member

It's out on: https://github.com/getsentry/sentry-dotnet/releases/tag/4.6.0

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.

SentryEvent memory leak with large exception object graphs
5 participants