AvaloniaObject_GetValue.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System.Runtime.CompilerServices;
  2. using Avalonia.Data;
  3. using BenchmarkDotNet.Attributes;
  4. #nullable enable
  5. namespace Avalonia.Benchmarks.Base
  6. {
  7. [MemoryDiagnoser]
  8. public class AvaloniaObject_GetValue
  9. {
  10. private BaselineTestClass _baseline = new(){ StringProperty = "foo" };
  11. private TestClass _target = new();
  12. public AvaloniaObject_GetValue()
  13. {
  14. RuntimeHelpers.RunClassConstructor(typeof(TestClass).TypeHandle);
  15. }
  16. [Benchmark(Baseline = true)]
  17. public int GetClrPropertyValues()
  18. {
  19. var target = _baseline;
  20. var result = 0;
  21. for (var i = 0; i < 100; ++i)
  22. {
  23. result += target.StringProperty?.Length ?? 0;
  24. result += target.Struct1Property.Int1;
  25. result += target.Struct2Property.Int1;
  26. result += target.Struct3Property.Int1;
  27. result += target.Struct4Property.Int1;
  28. result += target.Struct5Property.Int1;
  29. result += target.Struct6Property.Int1;
  30. result += target.Struct7Property.Int1;
  31. result += target.Struct8Property.Int1;
  32. }
  33. return result;
  34. }
  35. [Benchmark]
  36. public int GetDefaultValues()
  37. {
  38. var target = _target;
  39. var result = 0;
  40. for (var i = 0; i < 100; ++i)
  41. {
  42. result += target.GetValue(TestClass.StringProperty)?.Length ?? 0;
  43. result += target.GetValue(TestClass.Struct1Property).Int1;
  44. result += target.GetValue(TestClass.Struct2Property).Int1;
  45. result += target.GetValue(TestClass.Struct3Property).Int1;
  46. result += target.GetValue(TestClass.Struct4Property).Int1;
  47. result += target.GetValue(TestClass.Struct5Property).Int1;
  48. result += target.GetValue(TestClass.Struct6Property).Int1;
  49. result += target.GetValue(TestClass.Struct7Property).Int1;
  50. result += target.GetValue(TestClass.Struct8Property).Int1;
  51. }
  52. return result;
  53. }
  54. [GlobalSetup(Target = nameof(Get_Local_Values))]
  55. public void SetupLocalValues()
  56. {
  57. _target.SetValue(TestClass.StringProperty, "foo");
  58. _target.SetValue(TestClass.Struct1Property, new(1));
  59. _target.SetValue(TestClass.Struct2Property, new(1));
  60. _target.SetValue(TestClass.Struct3Property, new(1));
  61. _target.SetValue(TestClass.Struct4Property, new(1));
  62. _target.SetValue(TestClass.Struct5Property, new(1));
  63. _target.SetValue(TestClass.Struct6Property, new(1));
  64. _target.SetValue(TestClass.Struct7Property, new(1));
  65. _target.SetValue(TestClass.Struct8Property, new(1));
  66. }
  67. [Benchmark]
  68. public int Get_Local_Values()
  69. {
  70. var target = _target;
  71. var result = 0;
  72. for (var i = 0; i < 100; ++i)
  73. {
  74. result += target.GetValue(TestClass.StringProperty)?.Length ?? 0;
  75. result += target.GetValue(TestClass.Struct1Property).Int1;
  76. result += target.GetValue(TestClass.Struct2Property).Int1;
  77. result += target.GetValue(TestClass.Struct3Property).Int1;
  78. result += target.GetValue(TestClass.Struct4Property).Int1;
  79. result += target.GetValue(TestClass.Struct5Property).Int1;
  80. result += target.GetValue(TestClass.Struct6Property).Int1;
  81. result += target.GetValue(TestClass.Struct7Property).Int1;
  82. result += target.GetValue(TestClass.Struct8Property).Int1;
  83. }
  84. return result;
  85. }
  86. [GlobalSetup(Target = nameof(Get_Local_Values_With_Style_Values))]
  87. public void SetupLocalValuesAndStyleValues()
  88. {
  89. var target = _target;
  90. target.SetValue(TestClass.StringProperty, "foo");
  91. target.SetValue(TestClass.Struct1Property, new(1));
  92. target.SetValue(TestClass.Struct2Property, new(1));
  93. target.SetValue(TestClass.Struct3Property, new(1));
  94. target.SetValue(TestClass.Struct4Property, new(1));
  95. target.SetValue(TestClass.Struct5Property, new(1));
  96. target.SetValue(TestClass.Struct6Property, new(1));
  97. target.SetValue(TestClass.Struct7Property, new(1));
  98. target.SetValue(TestClass.Struct8Property, new(1));
  99. target.SetValue(TestClass.StringProperty, "bar", BindingPriority.Style);
  100. target.SetValue(TestClass.Struct1Property, new(), BindingPriority.Style);
  101. target.SetValue(TestClass.Struct2Property, new(), BindingPriority.Style);
  102. target.SetValue(TestClass.Struct3Property, new(), BindingPriority.Style);
  103. target.SetValue(TestClass.Struct4Property, new(), BindingPriority.Style);
  104. target.SetValue(TestClass.Struct5Property, new(), BindingPriority.Style);
  105. target.SetValue(TestClass.Struct6Property, new(), BindingPriority.Style);
  106. target.SetValue(TestClass.Struct7Property, new(), BindingPriority.Style);
  107. target.SetValue(TestClass.Struct8Property, new(), BindingPriority.Style);
  108. }
  109. [Benchmark]
  110. public int Get_Local_Values_With_Style_Values()
  111. {
  112. var target = _target;
  113. var result = 0;
  114. for (var i = 0; i < 100; ++i)
  115. {
  116. result += target.GetValue(TestClass.StringProperty)?.Length ?? 0;
  117. result += target.GetValue(TestClass.Struct1Property).Int1;
  118. result += target.GetValue(TestClass.Struct2Property).Int1;
  119. result += target.GetValue(TestClass.Struct3Property).Int1;
  120. result += target.GetValue(TestClass.Struct4Property).Int1;
  121. result += target.GetValue(TestClass.Struct5Property).Int1;
  122. result += target.GetValue(TestClass.Struct6Property).Int1;
  123. result += target.GetValue(TestClass.Struct7Property).Int1;
  124. result += target.GetValue(TestClass.Struct8Property).Int1;
  125. }
  126. return result;
  127. }
  128. private class TestClass : AvaloniaObject
  129. {
  130. public static readonly StyledProperty<string> StringProperty =
  131. AvaloniaProperty.Register<TestClass, string>("String");
  132. public static readonly StyledProperty<Struct1> Struct1Property =
  133. AvaloniaProperty.Register<TestClass, Struct1>("Struct1");
  134. public static readonly StyledProperty<Struct2> Struct2Property =
  135. AvaloniaProperty.Register<TestClass, Struct2>("Struct2");
  136. public static readonly StyledProperty<Struct3> Struct3Property =
  137. AvaloniaProperty.Register<TestClass, Struct3>("Struct3");
  138. public static readonly StyledProperty<Struct4> Struct4Property =
  139. AvaloniaProperty.Register<TestClass, Struct4>("Struct4");
  140. public static readonly StyledProperty<Struct5> Struct5Property =
  141. AvaloniaProperty.Register<TestClass, Struct5>("Struct5");
  142. public static readonly StyledProperty<Struct6> Struct6Property =
  143. AvaloniaProperty.Register<TestClass, Struct6>("Struct6");
  144. public static readonly StyledProperty<Struct7> Struct7Property =
  145. AvaloniaProperty.Register<TestClass, Struct7>("Struct7");
  146. public static readonly StyledProperty<Struct8> Struct8Property =
  147. AvaloniaProperty.Register<TestClass, Struct8>("Struct8");
  148. }
  149. private class BaselineTestClass
  150. {
  151. public string? StringProperty { get; set; }
  152. public Struct1 Struct1Property { get; set; }
  153. public Struct2 Struct2Property { get; set; }
  154. public Struct3 Struct3Property { get; set; }
  155. public Struct4 Struct4Property { get; set; }
  156. public Struct5 Struct5Property { get; set; }
  157. public Struct6 Struct6Property { get; set; }
  158. public Struct7 Struct7Property { get; set; }
  159. public Struct8 Struct8Property { get; set; }
  160. }
  161. }
  162. }