-
-
Notifications
You must be signed in to change notification settings - Fork 241
perf: avoid heap allocation via local System.Lazy instance
#5356
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
Draft
Flash0ver
wants to merge
6
commits into
main
Choose a base branch
from
perf/avoid-heap-alloc-via-local-lazy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
149a17e
perf: add internal LazyLite type
Flash0ver 43a56ba
ref: move BatchProcessorBenchmarks to Internal namespace
Flash0ver 0533236
perf: add Benchmarks
Flash0ver 84c7851
docs: fix benchmark instructions
Flash0ver c08d8b6
perf: use new local Lazy struct
Flash0ver 87f9f46
docs: update XML documentation comments
Flash0ver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
14 changes: 14 additions & 0 deletions
14
...et.Artifacts/results/Sentry.Benchmarks.Internal.LazyBenchmarks-report-github.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| ``` | ||
|
|
||
| BenchmarkDotNet v0.15.8, macOS Tahoe 26.5.1 (25F80) [Darwin 25.5.0] | ||
| Apple M3 Pro, 1 CPU, 12 logical and 12 physical cores | ||
| .NET SDK 10.0.301 | ||
| [Host] : .NET 10.0.9 (10.0.9, 10.0.926.27113), Arm64 RyuJIT armv8.0-a | ||
| DefaultJob : .NET 10.0.9 (10.0.9, 10.0.926.27113), Arm64 RyuJIT armv8.0-a | ||
|
|
||
|
|
||
| ``` | ||
| | Method | Mean | Error | StdDev | Gen0 | Allocated | | ||
| |------------------------- |---------:|----------:|----------:|-------:|----------:| | ||
| | System_Lazy | 6.909 ns | 0.0807 ns | 0.0715 ns | 0.0048 | 40 B | | ||
| | Sentry_Internal_LazyLite | 1.006 ns | 0.0102 ns | 0.0085 ns | - | - | |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #nullable enable | ||
|
|
||
| using BenchmarkDotNet.Attributes; | ||
| using Sentry.Internal; | ||
|
|
||
| namespace Sentry.Benchmarks.Internal; | ||
|
|
||
| public class LazyBenchmarks | ||
| { | ||
| private static string Text = null!; | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| Text = "Factory"; | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public (char, char) System_Lazy() | ||
| { | ||
| var lazy = new Lazy<char>(ValueFactory, LazyThreadSafetyMode.None); | ||
|
|
||
| var first = lazy.Value; | ||
| var second = lazy.Value; | ||
|
|
||
| return (first, second); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public (char, char) Sentry_Internal_LazyLite() | ||
| { | ||
| var lazy = new LazyLite<char>(ValueFactory); | ||
|
|
||
| var first = lazy.Value; | ||
| var second = lazy.Value; | ||
|
|
||
| return (first, second); | ||
| } | ||
|
|
||
| private static char ValueFactory() | ||
| { | ||
| return Text[^1]; | ||
| } | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| namespace Sentry.Internal; | ||
|
|
||
| /// <summary> | ||
| /// A <see cref="ValueType"/>-based variant of <see cref="Lazy{T}"/> that is not thread safe. | ||
| /// | ||
| /// We're using this for local variables, where no <see cref="LazyThreadSafetyMode"/> is required, | ||
| /// but a value is still cached, without any allocations via the <see langword="class"/>-based <see cref="Lazy{T}"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This type is not thread safe. | ||
| /// </remarks> | ||
| internal struct LazyLite<T> | ||
| { | ||
| private Func<T>? _factory; | ||
| private T? _value; | ||
|
|
||
| public LazyLite(Func<T>? valueFactory) | ||
| { | ||
| _factory = valueFactory; | ||
| _value = default; | ||
| } | ||
|
|
||
| public readonly bool IsValueCreated => _factory is null; | ||
|
|
||
| public T Value => _factory is null ? _value! : CreateValue(); | ||
|
|
||
| private T CreateValue() | ||
| { | ||
| var factory = _factory; | ||
| if (factory is not null) | ||
| { | ||
| _factory = null; | ||
| _value = factory(); | ||
| } | ||
|
|
||
| return _value!; | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #nullable enable | ||
|
|
||
| namespace Sentry.Tests.Internals; | ||
|
|
||
| public class LazyLiteTests | ||
| { | ||
| private int _valueFactoryInvoked = 0; | ||
|
|
||
| [Fact] | ||
| public void Default_HasNoFactory_ValueConsideredCreated() | ||
| { | ||
| LazyLite<string?> lazy = default; | ||
|
|
||
| lazy.IsValueCreated.Should().BeTrue(); | ||
| _valueFactoryInvoked.Should().Be(0); | ||
|
|
||
| lazy.Value.Should().BeNull(); | ||
|
|
||
| lazy.IsValueCreated.Should().BeTrue(); | ||
| _valueFactoryInvoked.Should().Be(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Null_HasNoFactory_ValueConsideredCreated() | ||
| { | ||
| LazyLite<string?> lazy = new(null); | ||
|
|
||
| lazy.IsValueCreated.Should().BeTrue(); | ||
| _valueFactoryInvoked.Should().Be(0); | ||
|
|
||
| lazy.Value.Should().BeNull(); | ||
|
|
||
| lazy.IsValueCreated.Should().BeTrue(); | ||
| _valueFactoryInvoked.Should().Be(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NullFactory_HasFactory_CreatesNullValue() | ||
| { | ||
| LazyLite<string?> lazy = new(NullFactory); | ||
|
|
||
| lazy.IsValueCreated.Should().BeFalse(); | ||
| _valueFactoryInvoked.Should().Be(0); | ||
|
|
||
| lazy.Value.Should().BeNull(); | ||
|
|
||
| lazy.IsValueCreated.Should().BeTrue(); | ||
| _valueFactoryInvoked.Should().Be(1); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValueFactory_HasFactory_CreatesValue() | ||
| { | ||
| LazyLite<string?> lazy = new(ValueFactory); | ||
|
|
||
| lazy.IsValueCreated.Should().BeFalse(); | ||
| _valueFactoryInvoked.Should().Be(0); | ||
|
|
||
| lazy.Value.Should().Be("Created"); | ||
|
|
||
| lazy.IsValueCreated.Should().BeTrue(); | ||
| _valueFactoryInvoked.Should().Be(1); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValueFactory_HasFactory_ReuseCachedValue() | ||
| { | ||
| LazyLite<string?> lazy = new(ValueFactory); | ||
|
|
||
| lazy.Value.Should().Be("Created"); | ||
|
|
||
| lazy.IsValueCreated.Should().BeTrue(); | ||
| _valueFactoryInvoked.Should().Be(1); | ||
|
|
||
| lazy.Value.Should().Be("Created"); | ||
|
|
||
| lazy.IsValueCreated.Should().BeTrue(); | ||
| _valueFactoryInvoked.Should().Be(1); | ||
| } | ||
|
|
||
| private string? NullFactory() | ||
| { | ||
| _valueFactoryInvoked++; | ||
| return null; | ||
| } | ||
|
|
||
| private string ValueFactory() | ||
| { | ||
| _valueFactoryInvoked++; | ||
| return "Created"; | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
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.Lazycaptures it and re-throws it every time accessingValue. On the other hand, we don't really need this functionality right now.