From 0e48247bc1eb46ff650f25fc5e9e8e94247f76e1 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Thu, 16 Apr 2026 12:15:07 -0700 Subject: [PATCH] bench inline --- BitFaster.Caching.Benchmarks/AltBenchmark.cs | 44 +++++++++++++++++++ .../BitFaster.Caching.Benchmarks.csproj | 2 +- BitFaster.Caching/Lru/ConcurrentLruCore.cs | 16 ++++++- 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 BitFaster.Caching.Benchmarks/AltBenchmark.cs diff --git a/BitFaster.Caching.Benchmarks/AltBenchmark.cs b/BitFaster.Caching.Benchmarks/AltBenchmark.cs new file mode 100644 index 00000000..6ef0de38 --- /dev/null +++ b/BitFaster.Caching.Benchmarks/AltBenchmark.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; +using BitFaster.Caching.Lru; + +namespace BitFaster.Caching.Benchmarks +{ + [MemoryDiagnoser] + [HideColumns("Job", "Median", "RatioSD")] + public class AltBenchmark + { + private static readonly ConcurrentLru concurrentLru = new ConcurrentLru(8, 9, EqualityComparer.Default); + + [GlobalSetup] + public void GlobalSetup() + { + concurrentLru.AddOrUpdate("1", 1); + } + +#if NET9_0_OR_GREATER + + [Benchmark(Baseline = true)] + public int GetAlternateInline() + { + var alt = concurrentLru.GetAlternateLookup>(); + alt.TryGet("1", out int value); + return value; + } + + [Benchmark()] + public int GetAlternateNoInline() + { + var alt = concurrentLru.GetAlternateLookup2>(); + alt.TryGet("1", out int value); + return value; + } +#endif + } +} diff --git a/BitFaster.Caching.Benchmarks/BitFaster.Caching.Benchmarks.csproj b/BitFaster.Caching.Benchmarks/BitFaster.Caching.Benchmarks.csproj index a2e03eae..482ba456 100644 --- a/BitFaster.Caching.Benchmarks/BitFaster.Caching.Benchmarks.csproj +++ b/BitFaster.Caching.Benchmarks/BitFaster.Caching.Benchmarks.csproj @@ -3,7 +3,7 @@ Exe latest - net48;net6.0;net8.0;net9.0 + net10.0;net9.0;net48;net6.0;net8.0; True true diff --git a/BitFaster.Caching/Lru/ConcurrentLruCore.cs b/BitFaster.Caching/Lru/ConcurrentLruCore.cs index cd8f6cd8..ec7d96cb 100644 --- a/BitFaster.Caching/Lru/ConcurrentLruCore.cs +++ b/BitFaster.Caching/Lru/ConcurrentLruCore.cs @@ -903,10 +903,11 @@ private static Optional> CreateEvents(ConcurrentLruCore + [MethodImpl(MethodImplOptions.AggressiveInlining)] public IAlternateLookup GetAlternateLookup() where TAlternateKey : notnull, allows ref struct { @@ -918,6 +919,17 @@ public IAlternateLookup GetAlternateLookup() return new AlternateLookup(this); } + public IAlternateLookup GetAlternateLookup2() + where TAlternateKey : notnull, allows ref struct + { + if (!this.dictionary.IsCompatibleKey()) + { + Throw.IncompatibleComparer(); + } + + return new AlternateLookup(this); + } + /// public bool TryGetAlternateLookup([MaybeNullWhen(false)] out IAlternateLookup lookup) where TAlternateKey : notnull, allows ref struct