Skip to content

Commit ec05a78

Browse files
authored
disassemble depth5 (#93)
* depth5 * measure
1 parent d7b182d commit ec05a78

File tree

6 files changed

+129
-103
lines changed

6 files changed

+129
-103
lines changed
Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
33
using System.Runtime.CompilerServices;
4-
using System.Text;
5-
using BenchmarkDotNet.Attributes;
6-
using Microsoft.CodeAnalysis.CSharp.Syntax;
7-
using Microsoft.Diagnostics.Runtime.Interop;
8-
9-
namespace BitFaster.Caching.Benchmarks
10-
{
11-
// Is it possible to write a class to eliminate the dispose code for types that are not IDisposable?
12-
// https://github.com/dotnet/runtime/issues/4920
13-
[DisassemblyDiagnoser(printSource: true)]
14-
[MemoryDiagnoser]
15-
public class DisposerBench
4+
using System.Text;
5+
using BenchmarkDotNet.Attributes;
6+
using Microsoft.CodeAnalysis.CSharp.Syntax;
7+
using Microsoft.Diagnostics.Runtime.Interop;
8+
9+
namespace BitFaster.Caching.Benchmarks
10+
{
11+
// Is it possible to write a class to eliminate the dispose code for types that are not IDisposable?
12+
// https://github.com/dotnet/runtime/issues/4920
13+
[DisassemblyDiagnoser(printSource: true)]
14+
[MemoryDiagnoser]
15+
public class DisposerBench
1616
{
17-
[Benchmark(Baseline = true)]
18-
public void HandWritten()
19-
{
20-
for (int i = 0; i < 1000; i++)
21-
{
17+
[Benchmark(Baseline = true)]
18+
public void HandWritten()
19+
{
20+
for (int i = 0; i < 1000; i++)
21+
{
2222
NotDisposable notDisposable = new NotDisposable();
23-
Disposable disposable = new Disposable();
23+
Disposable disposable = new Disposable();
2424
disposable.Dispose();
25-
}
25+
}
2626
}
2727

28-
[Benchmark()]
29-
public void NotOptimized()
28+
[Benchmark()]
29+
public void NotOptimized()
3030
{
31-
for (int i = 0; i < 1000; i++)
32-
{
31+
for (int i = 0; i < 1000; i++)
32+
{
3333
NotDisposable notDisposable = new NotDisposable();
3434
Disposable disposable = new Disposable();
3535

@@ -45,69 +45,69 @@ public void NotOptimized()
4545
}
4646
}
4747

48-
[Benchmark()]
49-
public void GenericDisposerReadonlyProperty()
48+
[Benchmark()]
49+
public void GenericDisposerReadonlyProperty()
5050
{
51-
for (int i = 0; i < 1000; i++)
52-
{
51+
for (int i = 0; i < 1000; i++)
52+
{
5353
NotDisposable notDisposable = new NotDisposable();
54-
Disposable disposable = new Disposable();
54+
Disposable disposable = new Disposable();
5555
Disposer<Disposable>.Dispose(disposable);
5656
Disposer<NotDisposable>.Dispose(notDisposable);
5757
}
58-
}
59-
60-
[Benchmark()]
61-
public void GenericDisposerStdCheck()
58+
}
59+
60+
[Benchmark()]
61+
public void GenericDisposerStdCheck()
6262
{
63-
for (int i = 0; i < 1000; i++)
64-
{
63+
for (int i = 0; i < 1000; i++)
64+
{
6565
NotDisposable notDisposable = new NotDisposable();
66-
Disposable disposable = new Disposable();
66+
Disposable disposable = new Disposable();
6767
Disposer2<Disposable>.Dispose(disposable);
6868
Disposer2<NotDisposable>.Dispose(notDisposable);
6969
}
70-
}
71-
}
72-
73-
public static class Disposer<T>
74-
{
75-
// try using a static readonly field
76-
private static readonly bool shouldDispose = typeof(IDisposable).IsAssignableFrom(typeof(T));
77-
78-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
79-
public static void Dispose(T value)
80-
{
81-
if (shouldDispose)
82-
{
83-
((IDisposable)value).Dispose();
84-
}
85-
}
86-
}
87-
70+
}
71+
}
72+
73+
public static class Disposer<T>
74+
{
75+
// try using a static readonly field
76+
private static readonly bool shouldDispose = typeof(IDisposable).IsAssignableFrom(typeof(T));
77+
78+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
79+
public static void Dispose(T value)
80+
{
81+
if (shouldDispose)
82+
{
83+
((IDisposable)value).Dispose();
84+
}
85+
}
86+
}
87+
8888
public static class Disposer2<T>
89-
{
90-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
91-
public static void Dispose(T value)
92-
{
89+
{
90+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
91+
public static void Dispose(T value)
92+
{
9393
if (value is IDisposable d)
9494
{
9595
d.Dispose();
96-
}
97-
}
98-
}
99-
100-
public class NotDisposable
96+
}
97+
}
98+
}
99+
100+
public class NotDisposable
101101
{ }
102102

103-
public class Disposable : IDisposable
104-
{
105-
private bool isDisposed = false;
106-
107-
public void Dispose()
108-
{
109-
if (!isDisposed)
110-
this.isDisposed = true;
111-
}
112-
}
113-
}
103+
public class Disposable : IDisposable
104+
{
105+
private bool isDisposed = false;
106+
107+
public void Dispose()
108+
{
109+
if (!isDisposed)
110+
this.isDisposed = true;
111+
}
112+
}
113+
}

BitFaster.Caching.Benchmarks/Lru/LruCycleBench.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ namespace BitFaster.Caching.Benchmarks.Lru
1515
// DefaultJob : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT
1616

1717

18-
//| Method | Mean | Error | StdDev | Code Size | Gen 0 | Allocated |
19-
//|------------------- |---------:|---------:|---------:|----------:|-------:|----------:|
20-
//| FastConcurrentLru | 22.61 us | 0.125 us | 0.110 us | 0 KB | 2.1362 | 9 KB |
21-
//| ConcurrentLru | 24.39 us | 0.389 us | 0.364 us | 0 KB | 2.1362 | 9 KB |
22-
//| FastConcurrentTLru | 31.28 us | 0.067 us | 0.062 us | 1 KB | 2.3193 | 10 KB |
23-
//| ConcurrentTLru | 31.75 us | 0.074 us | 0.062 us | 1 KB | 2.3193 | 10 KB |
24-
[DisassemblyDiagnoser(printSource: true)]
18+
//| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Code Size | Allocated |
19+
//|------------------- |---------:|---------:|---------:|------:|--------:|-------:|----------:|----------:|
20+
//| FastConcurrentLru | 23.25 us | 0.128 us | 0.114 us | 1.00 | 0.00 | 2.1362 | 5 KB | 9 KB |
21+
//| ConcurrentLru | 23.78 us | 0.116 us | 0.097 us | 1.02 | 0.01 | 2.1362 | 5 KB | 9 KB |
22+
//| FastConcurrentTLru | 32.17 us | 0.463 us | 0.433 us | 1.38 | 0.02 | 2.3193 | 6 KB | 10 KB |
23+
//| ConcurrentTLru | 32.52 us | 0.386 us | 0.361 us | 1.40 | 0.02 | 2.3193 | 6 KB | 10 KB |
24+
//| ClassicLru | 16.29 us | 0.195 us | 0.163 us | 0.70 | 0.01 | 3.2959 | 5 KB | 14 KB |
25+
[DisassemblyDiagnoser(printSource: true, maxDepth: 5)]
2526
[MemoryDiagnoser]
2627
public class LruCycleBench
2728
{
@@ -31,7 +32,7 @@ public class LruCycleBench
3132
private static readonly FastConcurrentLru<int, int> fastConcurrentLru = new(8, 9, EqualityComparer<int>.Default);
3233
private static readonly FastConcurrentTLru<int, int> fastConcurrentTLru = new(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(1));
3334

34-
[Benchmark()]
35+
[Benchmark(Baseline = true)]
3536
public void FastConcurrentLru()
3637
{
3738
Func<int, int> func = x => x;
@@ -66,5 +67,14 @@ public void ConcurrentTLru()
6667
for (int i = 0; i < 128; i++)
6768
concurrentTlru.GetOrAdd(i, func);
6869
}
70+
71+
[Benchmark()]
72+
public void ClassicLru()
73+
{
74+
Func<int, int> func = x => x;
75+
76+
for (int i = 0; i < 128; i++)
77+
classicLru.GetOrAdd(i, func);
78+
}
6979
}
7080
}

BitFaster.Caching.Benchmarks/Lru/LruJustGetOrAdd.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ namespace BitFaster.Caching.Benchmarks
2121

2222
//| Method | Mean | Error | StdDev | Ratio | RatioSD | Code Size | Gen 0 | Allocated |
2323
//|------------------------- |-----------:|----------:|----------:|------:|--------:|----------:|-------:|----------:|
24-
//| ConcurrentDictionary | 7.761 ns | 0.0393 ns | 0.0307 ns | 1.00 | 0.00 | 340 B | - | - |
25-
//| FastConcurrentLru | 9.559 ns | 0.0625 ns | 0.0585 ns | 1.23 | 0.01 | 427 B | - | - |
26-
//| ConcurrentLru | 13.580 ns | 0.0531 ns | 0.0443 ns | 1.75 | 0.01 | 449 B | - | - |
27-
//| FastConcurrentTLru | 27.109 ns | 0.1041 ns | 0.0813 ns | 3.49 | 0.02 | 613 B | - | - |
28-
//| ConcurrentTLru | 29.622 ns | 0.2369 ns | 0.2216 ns | 3.81 | 0.03 | 684 B | - | - |
29-
//| ClassicLru | 48.060 ns | 0.2447 ns | 0.2169 ns | 6.19 | 0.03 | 738 B | - | - |
30-
//| RuntimeMemoryCacheGet | 106.117 ns | 0.4677 ns | 0.4375 ns | 13.69 | 0.08 | 49 B | 0.0074 | 32 B |
31-
//| ExtensionsMemoryCacheGet | 92.386 ns | 0.4539 ns | 0.4023 ns | 11.91 | 0.07 | 78 B | 0.0055 | 24 B |
32-
[DisassemblyDiagnoser(printSource: true)]
24+
//| ConcurrentDictionary | 7.868 ns | 0.0543 ns | 0.0481 ns | 1.00 | 0.00 | 1,523 B | - | - |
25+
//| FastConcurrentLru | 10.340 ns | 0.0496 ns | 0.0464 ns | 1.31 | 0.01 | 2,185 B | - | - |
26+
//| ConcurrentLru | 13.739 ns | 0.0979 ns | 0.0916 ns | 1.75 | 0.01 | 2,207 B | - | - |
27+
//| FastConcurrentTLru | 25.820 ns | 0.0933 ns | 0.0729 ns | 3.28 | 0.02 | 2,371 B | - | - |
28+
//| ConcurrentTLru | 29.732 ns | 0.1387 ns | 0.1229 ns | 3.78 | 0.03 | 2,442 B | - | - |
29+
//| ClassicLru | 49.041 ns | 0.8575 ns | 0.8021 ns | 6.23 | 0.11 | 3,013 B | - | - |
30+
//| RuntimeMemoryCacheGet | 107.769 ns | 1.1901 ns | 0.9938 ns | 13.69 | 0.15 | 49 B | 0.0074 | 32 B |
31+
//| ExtensionsMemoryCacheGet | 93.188 ns | 0.2321 ns | 0.2171 ns | 11.85 | 0.07 | 78 B | 0.0055 | 24 B |
32+
[DisassemblyDiagnoser(printSource: true, maxDepth: 5)]
3333
[MemoryDiagnoser]
3434
public class LruJustGetOrAdd
3535
{

BitFaster.Caching.Benchmarks/Lru/LruJustTryGet.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ namespace BitFaster.Caching.Benchmarks.Lru
1616

1717
//| Method | Mean | Error | StdDev | Ratio | RatioSD | Code Size | Allocated |
1818
//|--------------------- |----------:|----------:|----------:|------:|--------:|----------:|----------:|
19-
//| ConcurrentDictionary | 4.421 ns | 0.0295 ns | 0.0276 ns | 1.00 | 0.00 | 364 B | - |
20-
//| FastConcurrentLru | 7.645 ns | 0.0339 ns | 0.0300 ns | 1.73 | 0.02 | 339 B | - |
21-
//| FastConcurrentTLru | 26.139 ns | 0.0741 ns | 0.0619 ns | 5.92 | 0.04 | 437 B | - |
22-
[DisassemblyDiagnoser(printSource: true)]
19+
//| ConcurrentDictionary | 4.480 ns | 0.0230 ns | 0.0204 ns | 1.00 | 0.00 | 364 B | - |
20+
//| FastConcurrentLru | 7.705 ns | 0.0343 ns | 0.0286 ns | 1.72 | 0.01 | 448 B | - |
21+
//| FastConcurrentTLru | 25.350 ns | 0.3301 ns | 0.3088 ns | 5.66 | 0.08 | 546 B | - |
22+
[DisassemblyDiagnoser(printSource: true, maxDepth: 5)]
2323
[MemoryDiagnoser]
2424
public class LruJustTryGet
2525
{

BitFaster.Caching.Benchmarks/Lru/LruMultiGet.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,23 @@
1212

1313
namespace BitFaster.Caching.Benchmarks.Lru
1414
{
15-
[DisassemblyDiagnoser(printSource: true)]
15+
//BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
16+
//Intel Xeon W-2133 CPU 3.60GHz, 1 CPU, 12 logical and 6 physical cores
17+
//.NET SDK= 6.0.100
18+
// [Host] : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT
19+
// DefaultJob : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT
20+
21+
22+
//| Method | Mean | Error | StdDev | Ratio | RatioSD | Code Size | Gen 0 | Allocated |
23+
//|--------------------- |-----------:|----------:|----------:|------:|--------:|----------:|-------:|----------:|
24+
//| ConcurrentDictionary | 8.389 ns | 0.0233 ns | 0.0206 ns | 1.00 | 0.00 | 1,544 B | - | - |
25+
//| FastConcurrentLru | 10.637 ns | 0.0808 ns | 0.0755 ns | 1.27 | 0.01 | 3,149 B | - | - |
26+
//| ConcurrentLru | 13.977 ns | 0.0674 ns | 0.0526 ns | 1.67 | 0.01 | 3,171 B | - | - |
27+
//| FastConcurrentTLru | 27.107 ns | 0.0810 ns | 0.0632 ns | 3.23 | 0.01 | 3,468 B | - | - |
28+
//| ConcurrentTLru | 33.733 ns | 0.6613 ns | 0.6791 ns | 4.02 | 0.09 | 3,539 B | - | - |
29+
//| ClassicLru | 52.898 ns | 0.3079 ns | 0.2404 ns | 6.30 | 0.03 | 3,021 B | - | - |
30+
//| MemoryCache | 117.075 ns | 1.7664 ns | 1.5658 ns | 13.96 | 0.18 | 94 B | 0.0073 | 32 B |
31+
[DisassemblyDiagnoser(printSource: true, maxDepth: 5)]
1632
[MemoryDiagnoser]
1733
public class LruMultiGet
1834
{

BitFaster.Caching.Benchmarks/Lru/LruZipDistribution.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ namespace BitFaster.Caching.Benchmarks.Lru
1717

1818
//| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Code Size | Allocated |
1919
//|------------------- |---------:|--------:|--------:|------:|--------:|-------:|----------:|----------:|
20-
//| ClassicLru | 108.4 ns | 0.26 ns | 0.20 ns | 1.00 | 0.00 | 0.0154 | 799 B | 67 B |
21-
//| FastConcurrentLru | 123.1 ns | 0.97 ns | 0.86 ns | 1.14 | 0.01 | 0.0093 | 488 B | 41 B |
22-
//| ConcurrentLru | 128.7 ns | 2.12 ns | 1.98 ns | 1.19 | 0.02 | 0.0093 | 510 B | 40 B |
23-
//| FastConcurrentTLru | 166.1 ns | 0.99 ns | 0.83 ns | 1.53 | 0.01 | 0.0100 | 674 B | 43 B |
24-
//| ConcurrentTLru | 172.2 ns | 0.52 ns | 0.46 ns | 1.59 | 0.00 | 0.0103 | 745 B | 45 B |
25-
[DisassemblyDiagnoser(printSource: true)]
20+
//| ClassicLru | 111.3 ns | 1.33 ns | 1.11 ns | 1.00 | 0.00 | 0.0148 | 4,108 B | 64 B |
21+
//| FastConcurrentLru | 121.6 ns | 1.45 ns | 1.21 ns | 1.09 | 0.01 | 0.0090 | 5,085 B | 39 B |
22+
//| ConcurrentLru | 127.4 ns | 0.51 ns | 0.48 ns | 1.14 | 0.01 | 0.0093 | 5,107 B | 41 B |
23+
//| FastConcurrentTLru | 175.6 ns | 1.08 ns | 1.01 ns | 1.58 | 0.02 | 0.0100 | 5,911 B | 44 B |
24+
//| ConcurrentTLru | 169.7 ns | 0.86 ns | 0.80 ns | 1.52 | 0.02 | 0.0098 | 5,982 B | 43 B |
25+
[DisassemblyDiagnoser(printSource: true, maxDepth: 5)]
2626
[MemoryDiagnoser]
2727
public class LruZipDistribution
2828
{

0 commit comments

Comments
 (0)