perf: avoid heap allocation via local System.Lazy instance#5356
Draft
Flash0ver wants to merge 6 commits into
Draft
perf: avoid heap allocation via local System.Lazy instance#5356Flash0ver wants to merge 6 commits into
System.Lazy instance#5356Flash0ver wants to merge 6 commits into
Conversation
Flash0ver
commented
Jul 8, 2026
| if (factory is not null) | ||
| { | ||
| _factory = null; | ||
| _value = factory(); |
Contributor
Author
There was a problem hiding this comment.
question: exception
If the factory-callback throws, we propagate the Exception, but only once ... I think System.Lazy captures it and re-throws it every time accessing Value. On the other hand, we don't really need this functionality right now.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5356 +/- ##
==========================================
+ Coverage 74.19% 74.22% +0.02%
==========================================
Files 508 510 +2
Lines 18373 18396 +23
Branches 3595 3602 +7
==========================================
+ Hits 13632 13654 +22
Misses 3869 3869
- Partials 872 873 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Avoid a heap allocation via a local System.Lazy instance.
Remarks
In #5159, we introduced a local
System.Lazyinstance in favor of readability and maintainability. This change is introducing a non-thread-safestruct-based Lazy variant to have your cake and eat it too.Discussion
Is this new type worth it
I'd argue: yes ... to keep readability of the usage of
PreferTransactionNameProviderhigh, yet avoid40 Bof heap allocations per traced request, which can add up in busy web servicesNaming (is hard)
I initially went with
LazyLite, to align withConcurrentBagLiteandConcurrentQueueLite. But arguably, bothConcurrentBagLiteandConcurrentQueueLiteare replacements forConcurrentBagandConcurrentQueuerespectively, whereLazyLitedoes not replaceLazy, but should only be used for local variables when there is no thread contention at all.Changes
BatchProcessorBenchmarksto a newInternalnamespaceSentry.Benchmarksis getting long/test/is also mimicking the respective/src/namespacesbenchmarks/Sentry.Benchmarks/README.mdLazyLite+ Tests + BenchmarksLazyusage withLazyLite#skip-changelog