|
| 1 | +using System.Linq; |
| 2 | +using System.Linq.Expressions; |
| 3 | +using System.Reflection; |
| 4 | + |
| 5 | +namespace Xledger.Collections.Bench; |
| 6 | + |
| 7 | +[MemoryDiagnoser(displayGenColumns: false)] |
| 8 | +[SimpleJob(RuntimeMoniker.Net48)] |
| 9 | +[SimpleJob(RuntimeMoniker.Net80)] |
| 10 | +[SimpleJob(RuntimeMoniker.Net10_0)] |
| 11 | +public class ImmDictReflection { |
| 12 | + [Params(0, 300)] |
| 13 | + public int Capacity; |
| 14 | + |
| 15 | + [Params(300, 500)] |
| 16 | + public int Count; |
| 17 | + |
| 18 | + public static Dictionary<string, int> NewDictionary(int capacity, int count) { |
| 19 | + var dict = new Dictionary<string, int>(capacity); |
| 20 | + for (int i = 0; i < count; ++i) { |
| 21 | + dict.Add(Guid.NewGuid().ToString(), i); |
| 22 | + } |
| 23 | + return dict; |
| 24 | + } |
| 25 | + |
| 26 | + public static ImmDict.BuildDict<string, int> MakeFillDict(int count) { |
| 27 | + return (ImmDict.DictBuilder<string, int> dict) => { |
| 28 | + for (int i = 0; i < count; ++i) { |
| 29 | + dict.Add(Guid.NewGuid().ToString(), i); |
| 30 | + } |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + [Benchmark(Baseline = true)] |
| 35 | + public ImmDict<string, int> ImmDict_Build() { |
| 36 | + return ImmDict.Build<string, int>(MakeFillDict(this.Count), capacity: this.Capacity); |
| 37 | + } |
| 38 | + |
| 39 | + static readonly ConstructorInfo CI_NoCopy = typeof(ImmDict<string, int>).GetConstructor( |
| 40 | + BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.ExactBinding, |
| 41 | + binder: null, |
| 42 | + [typeof(Dictionary<string, int>)], |
| 43 | + modifiers: null); |
| 44 | + static readonly MethodInfo MI_Build = typeof(ImmDict).GetMethods() |
| 45 | + .Where(mi => mi.Name == nameof(ImmDict.Build)) |
| 46 | + .Last() |
| 47 | + .MakeGenericMethod(typeof(string), typeof(int)); |
| 48 | + |
| 49 | + static readonly MethodInfo MI_NewDictionary = typeof(ImmDictReflection).GetMethod(nameof(NewDictionary)); |
| 50 | + static readonly MethodInfo MI_MakeFillDict = typeof(ImmDictReflection).GetMethod(nameof(MakeFillDict)); |
| 51 | + |
| 52 | + static readonly Func<int, int, ImmDict<string, int>> newNoCopy = Compile_NewNoCopy(); |
| 53 | + static readonly Func<int, int, ImmDict<string, int>> build = Compile_Build(); |
| 54 | + |
| 55 | + [Benchmark] |
| 56 | + public ImmDict<string, int> DynInvoke_NewNoCopy() { |
| 57 | + return (ImmDict<string, int>)CI_NoCopy.Invoke([NewDictionary(this.Capacity, this.Count)]); |
| 58 | + } |
| 59 | + |
| 60 | + [Benchmark] |
| 61 | + public ImmDict<string, int> NewNoCopy() { |
| 62 | + return new ImmDict<string, int>(NewDictionary(this.Capacity, this.Count)); |
| 63 | + } |
| 64 | + |
| 65 | + static Func<int, int, ImmDict<string, int>> Compile_NewNoCopy() { |
| 66 | + var pi_capacity = Expression.Parameter(typeof(int), "capacity"); |
| 67 | + var pi_count = Expression.Parameter(typeof(int), "count"); |
| 68 | + var newImmSet = |
| 69 | + Expression.New(CI_NoCopy, |
| 70 | + Expression.Call(MI_NewDictionary, pi_capacity, pi_count)); |
| 71 | + var fn = Expression.Lambda<Func<int, int, ImmDict<string, int>>>( |
| 72 | + newImmSet, |
| 73 | + tailCall: false, |
| 74 | + pi_capacity, |
| 75 | + pi_count).Compile(); |
| 76 | + return fn; |
| 77 | + } |
| 78 | + |
| 79 | + static Func<int, int, ImmDict<string, int>> Compile_Build() { |
| 80 | + var pi_capacity = Expression.Parameter(typeof(int), "capacity"); |
| 81 | + var pi_count = Expression.Parameter(typeof(int), "count"); |
| 82 | + var fillSetDelegate = Expression.Call(MI_MakeFillDict, pi_count); |
| 83 | + var newImmSet = Expression.Call(MI_Build, fillSetDelegate, pi_capacity, Expression.Constant(false)); |
| 84 | + var fn = Expression.Lambda<Func<int, int, ImmDict<string, int>>>( |
| 85 | + newImmSet, |
| 86 | + tailCall: false, |
| 87 | + pi_capacity, |
| 88 | + pi_count).Compile(); |
| 89 | + return fn; |
| 90 | + } |
| 91 | + |
| 92 | + [Benchmark] |
| 93 | + public ImmDict<string, int> Expr_NewNoCopy() { |
| 94 | + return newNoCopy(this.Capacity, this.Count); |
| 95 | + } |
| 96 | + |
| 97 | + [Benchmark] |
| 98 | + public ImmDict<string, int> Expr_Build() { |
| 99 | + return build(this.Capacity, this.Count); |
| 100 | + } |
| 101 | +} |
0 commit comments