Skip to content

Conversation

@rogerioyuuki
Copy link
Contributor

Hi, I was benchmarking a few caching libraries and noticed two closure allocations which can be avoided.

  1. FastCache.GetOrAdd was using the TArg overload of ConcurrentDictionary (as pointed out by A few small issues #1 and fixed by ebef51e), but it was still capturing valueFactory variable. I changed TArg to a tuple of the two variables needed.
  2. FastCache.AddOrUpdate could be similarly changed to use another overload and prevent capturing ttlValue variable.

I ran the project benchmark on my machine with the following results:

BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.1702)
12th Gen Intel Core i7-12800H, 1 CPU, 20 logical and 14 physical cores
.NET SDK=7.0.202
  [Host]   : .NET 6.0.14 (6.0.1423.7309), X64 RyuJIT AVX2
  ShortRun : .NET 6.0.14 (6.0.1423.7309), X64 RyuJIT AVX2

Job=ShortRun  IterationCount=3  LaunchCount=1  
WarmupCount=3  

Main branch (3e9a35a)

Method Mean Error StdDev Gen0 Allocated
DictionaryLookup 57.49 ns 11.028 ns 0.604 ns - -
FastCacheLookup 55.94 ns 5.386 ns 0.295 ns - -
MemoryCacheLookup 347.25 ns 135.332 ns 7.418 ns 0.0100 128 B
FastCacheGetOrAdd 81.88 ns 12.146 ns 0.666 ns 0.0076 96 B
FastCacheAddRemove 73.22 ns 21.811 ns 1.196 ns 0.0134 168 B
MemoryCacheAddRemove 389.02 ns 80.734 ns 4.425 ns 0.0257 328 B

This PR (686fd04)

Method Mean Error StdDev Gen0 Allocated
DictionaryLookup 57.67 ns 5.942 ns 0.326 ns - -
FastCacheLookup 59.16 ns 25.778 ns 1.413 ns - -
MemoryCacheLookup 343.65 ns 145.375 ns 7.969 ns 0.0100 128 B
FastCacheGetOrAdd 71.71 ns 23.982 ns 1.315 ns - -
FastCacheAddRemove 72.49 ns 103.096 ns 5.651 ns 0.0063 80 B
MemoryCacheAddRemove 369.58 ns 83.251 ns 4.563 ns 0.0257 328 B

Method AddOrUpdate: used another overload of ConcurrentDictionary.AddOrUpdate to avoid capturing ttlValue variable.

Method GetOrAdd: used a tuple in the TArg parameter to avoid capturing valueFactory
@alex-jitbit
Copy link
Member

Thanks!

@alex-jitbit alex-jitbit merged commit 76f384f into jitbit:main Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants