Skip to content

perf: avoid heap allocation via local System.Lazy instance#5356

Draft
Flash0ver wants to merge 6 commits into
mainfrom
perf/avoid-heap-alloc-via-local-lazy
Draft

perf: avoid heap allocation via local System.Lazy instance#5356
Flash0ver wants to merge 6 commits into
mainfrom
perf/avoid-heap-alloc-via-local-lazy

Conversation

@Flash0ver

@Flash0ver Flash0ver commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Avoid a heap allocation via a local System.Lazy instance.

Remarks

In #5159, we introduced a local System.Lazy instance in favor of readability and maintainability. This change is introducing a non-thread-safe struct-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 PreferTransactionNameProvider high, yet avoid 40 B of heap allocations per traced request, which can add up in busy web services

Naming (is hard)

I initially went with LazyLite, to align with ConcurrentBagLite and ConcurrentQueueLite. But arguably, both ConcurrentBagLite and ConcurrentQueueLite are replacements for ConcurrentBag and ConcurrentQueue respectively, where LazyLite does not replace Lazy, but should only be used for local variables when there is no thread contention at all.

Changes

  • move existing BatchProcessorBenchmarks to a new Internal namespace
    • the list of documents in the root of Sentry.Benchmarks is getting long
    • to be consistent with how /test/ is also mimicking the respective /src/ namespaces
  • a fix to the command in benchmarks/Sentry.Benchmarks/README.md
  • added LazyLite + Tests + Benchmarks
  • replaced the local Lazy usage with LazyLite

#skip-changelog

@Flash0ver Flash0ver self-assigned this Jul 8, 2026
@Flash0ver Flash0ver added the skip-changelog Suppress automatic changelog generation via Craft label Jul 8, 2026
if (factory is not null)
{
_factory = null;
_value = factory();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.22%. Comparing base (afd3873) to head (87f9f46).
⚠️ Report is 5 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-changelog Suppress automatic changelog generation via Craft

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant