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

Migrate to LazyVariable<TOwner, TValue> to avoid delegate closure allocation #428

Merged
merged 3 commits into from
Apr 29, 2023

Conversation

Washi1337
Copy link
Owner

Moves the entire code-base from LazyVariable<T> to LazyVariable<TOwner, TValue>, where the current instance is no longer captured in a closure but instead is passed on as an argument of the GetValue method.. This can significantly reduce the amount of allocations when many lazily initialized structures are being initialized (e.g., with large input binaries).

@Washi1337
Copy link
Owner Author

Washi1337 commented Apr 20, 2023

Benchmarks show a reduction of up to 12% on memory allocations, as well as performance improvements up to 19% for larger binaries:

BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.22621.1555/22H2/2022Update/SunValley2)
AMD Ryzen 5 3600, 1 CPU, 12 logical and 6 physical cores
.NET SDK=7.0.203
  [Host]     : .NET 6.0.14 (6.0.1423.7309), X64 RyuJIT AVX2
  DefaultJob : .NET 6.0.14 (6.0.1423.7309), X64 RyuJIT AVX2
  5.2.0      : .NET 6.0.14 (6.0.1423.7309), X64 RyuJIT AVX2
Method Job Mean Error StdDev Ratio RatioSD Gen0 Gen1 Gen2 Allocated Alloc Ratio
HelloWorld_Read DefaultJob 161.89 μs 3.143 μs 3.859 μs 1.03 0.02 5.1270 0.4883 - 42.91 KB 0.94
HelloWorld_Read 5.2.0 157.30 μs 1.534 μs 1.435 μs 1.00 0.00 5.3711 0.4883 - 45.72 KB 1.00
HelloWorld_ReadWrite DefaultJob 494.87 μs 7.758 μs 7.257 μs 1.00 0.02 29.2969 4.8828 - 241.96 KB 0.95
HelloWorld_ReadWrite 5.2.0 496.11 μs 4.994 μs 4.427 μs 1.00 0.00 30.2734 4.8828 - 253.96 KB 1.00
CrackMe_Read DefaultJob 32.03 μs 0.541 μs 0.480 μs 0.96 0.01 4.9438 0.4883 - 40.44 KB 0.93
CrackMe_Read 5.2.0 33.49 μs 0.428 μs 0.400 μs 1.00 0.00 5.3101 0.5493 - 43.5 KB 1.00
CrackMe_ReadWrite DefaultJob 393.07 μs 4.084 μs 3.820 μs 0.99 0.02 30.7617 5.3711 - 254.19 KB 0.95
CrackMe_ReadWrite 5.2.0 396.74 μs 6.686 μs 6.254 μs 1.00 0.00 32.7148 7.3242 - 267.88 KB 1.00
ManyMethods_Read DefaultJob 163.50 μs 3.252 μs 3.994 μs 1.03 0.03 5.1270 0.4883 - 42.92 KB 0.94
ManyMethods_Read 5.2.0 159.40 μs 0.656 μs 0.547 μs 1.00 0.00 5.3711 0.4883 - 45.74 KB 1.00
ManyMethods_ReadWrite DefaultJob 93,840.43 μs 1,831.848 μs 2,036.094 μs 0.96 0.04 3833.3333 1666.6667 666.6667 35449.76 KB 0.92
ManyMethods_ReadWrite 5.2.0 97,748.87 μs 1,862.766 μs 2,287.643 μs 1.00 0.00 4333.3333 2000.0000 833.3333 38587.23 KB 1.00
SystemPrivateCoreLib_ReadWrite DefaultJob 729,874.47 μs 12,476.893 μs 12,253.978 μs 0.81 0.02 23000.0000 9000.0000 2000.0000 206384.95 KB 0.89
SystemPrivateCoreLib_ReadWrite 5.2.0 899,562.87 μs 15,046.231 μs 14,074.254 μs 1.00 0.00 27000.0000 12000.0000 3000.0000 231105.77 KB 1.00
SystemRuntimeLib_ReadWrite DefaultJob 3,947.00 μs 42.452 μs 37.633 μs 0.98 0.02 343.7500 156.2500 - 2861.99 KB 0.88
SystemRuntimeLib_ReadWrite 5.2.0 4,026.73 μs 51.275 μs 45.454 μs 1.00 0.00 398.4375 195.3125 - 3262.55 KB 1.00
SystemPrivateXml_ReadWrite DefaultJob 674,377.37 μs 9,212.898 μs 8,617.751 μs 0.92 0.02 18000.0000 8000.0000 2000.0000 161901.08 KB 0.90
SystemPrivateXml_ReadWrite 5.2.0 735,245.58 μs 13,184.794 μs 12,333.064 μs 1.00 0.00 20000.0000 9000.0000 2000.0000 180644.56 KB 1.00

@Washi1337 Washi1337 changed the title Migrate to LazyVariable<TOwner, TValue> to avoid delegate allocation Migrate to LazyVariable<TOwner, TValue> to avoid delegate closure allocation Apr 20, 2023
@Washi1337 Washi1337 added this to the 5.3.0 milestone Apr 27, 2023
@Washi1337 Washi1337 merged commit 2007f94 into development Apr 29, 2023
@Washi1337 Washi1337 deleted the feature/owned-lazyvars branch April 29, 2023 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant