AvaloniaObject_SetValue.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_SetValue
  9. {
  10. private BaselineTestClass _baseline = new();
  11. private TestClass _target = new();
  12. public AvaloniaObject_SetValue()
  13. {
  14. RuntimeHelpers.RunClassConstructor(typeof(TestClass).TypeHandle);
  15. }
  16. [Benchmark(Baseline = true)]
  17. public int SetClrPropertyValues()
  18. {
  19. var target = _baseline;
  20. var result = 0;
  21. for (var i = 0; i < 100; ++i)
  22. {
  23. target.StringProperty = "foo";
  24. target.Struct1Property = new(i + 1);
  25. target.Struct2Property = new(i + 1);
  26. target.Struct3Property = new(i + 1);
  27. target.Struct4Property = new(i + 1);
  28. target.Struct5Property = new(i + 1);
  29. target.Struct6Property = new(i + 1);
  30. target.Struct7Property = new(i + 1);
  31. target.Struct8Property = new(i + 1);
  32. }
  33. return result;
  34. }
  35. [Benchmark]
  36. public void SetValues()
  37. {
  38. var target = _target;
  39. for (var i = 0; i < 100; ++i)
  40. {
  41. target.SetValue(TestClass.StringProperty, "foo");
  42. target.SetValue(TestClass.Struct1Property, new(i + 1));
  43. target.SetValue(TestClass.Struct2Property, new(i + 1));
  44. target.SetValue(TestClass.Struct3Property, new(i + 1));
  45. target.SetValue(TestClass.Struct4Property, new(i + 1));
  46. target.SetValue(TestClass.Struct5Property, new(i + 1));
  47. target.SetValue(TestClass.Struct6Property, new(i + 1));
  48. target.SetValue(TestClass.Struct7Property, new(i + 1));
  49. target.SetValue(TestClass.Struct8Property, new(i + 1));
  50. }
  51. }
  52. [GlobalSetup(Target = nameof(Set_Local_Values_With_Style_Values))]
  53. public void SetupStyleValues()
  54. {
  55. var target = _target;
  56. target.SetValue(TestClass.StringProperty, "foo", BindingPriority.Style);
  57. target.SetValue(TestClass.Struct1Property, new(), BindingPriority.Style);
  58. target.SetValue(TestClass.Struct2Property, new(), BindingPriority.Style);
  59. target.SetValue(TestClass.Struct3Property, new(), BindingPriority.Style);
  60. target.SetValue(TestClass.Struct4Property, new(), BindingPriority.Style);
  61. target.SetValue(TestClass.Struct5Property, new(), BindingPriority.Style);
  62. target.SetValue(TestClass.Struct6Property, new(), BindingPriority.Style);
  63. target.SetValue(TestClass.Struct7Property, new(), BindingPriority.Style);
  64. target.SetValue(TestClass.Struct8Property, new(), BindingPriority.Style);
  65. }
  66. [Benchmark]
  67. public void Set_Local_Values_With_Style_Values()
  68. {
  69. var target = _target;
  70. for (var i = 0; i < 100; ++i)
  71. {
  72. target.SetValue(TestClass.StringProperty, "foo");
  73. target.SetValue(TestClass.Struct1Property, new(i + 1));
  74. target.SetValue(TestClass.Struct2Property, new(i + 1));
  75. target.SetValue(TestClass.Struct3Property, new(i + 1));
  76. target.SetValue(TestClass.Struct4Property, new(i + 1));
  77. target.SetValue(TestClass.Struct5Property, new(i + 1));
  78. target.SetValue(TestClass.Struct6Property, new(i + 1));
  79. target.SetValue(TestClass.Struct7Property, new(i + 1));
  80. target.SetValue(TestClass.Struct8Property, new(i + 1));
  81. }
  82. }
  83. private class TestClass : AvaloniaObject
  84. {
  85. public static readonly StyledProperty<string> StringProperty =
  86. AvaloniaProperty.Register<TestClass, string>("String");
  87. public static readonly StyledProperty<Struct1> Struct1Property =
  88. AvaloniaProperty.Register<TestClass, Struct1>("Struct1");
  89. public static readonly StyledProperty<Struct2> Struct2Property =
  90. AvaloniaProperty.Register<TestClass, Struct2>("Struct2");
  91. public static readonly StyledProperty<Struct3> Struct3Property =
  92. AvaloniaProperty.Register<TestClass, Struct3>("Struct3");
  93. public static readonly StyledProperty<Struct4> Struct4Property =
  94. AvaloniaProperty.Register<TestClass, Struct4>("Struct4");
  95. public static readonly StyledProperty<Struct5> Struct5Property =
  96. AvaloniaProperty.Register<TestClass, Struct5>("Struct5");
  97. public static readonly StyledProperty<Struct6> Struct6Property =
  98. AvaloniaProperty.Register<TestClass, Struct6>("Struct6");
  99. public static readonly StyledProperty<Struct7> Struct7Property =
  100. AvaloniaProperty.Register<TestClass, Struct7>("Struct7");
  101. public static readonly StyledProperty<Struct8> Struct8Property =
  102. AvaloniaProperty.Register<TestClass, Struct8>("Struct8");
  103. }
  104. private class BaselineTestClass
  105. {
  106. public string? StringProperty { get; set; }
  107. public Struct1 Struct1Property { get; set; }
  108. public Struct2 Struct2Property { get; set; }
  109. public Struct3 Struct3Property { get; set; }
  110. public Struct4 Struct4Property { get; set; }
  111. public Struct5 Struct5Property { get; set; }
  112. public Struct6 Struct6Property { get; set; }
  113. public Struct7 Struct7Property { get; set; }
  114. public Struct8 Struct8Property { get; set; }
  115. }
  116. }
  117. }