using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using Avalonia.Controls; using BenchmarkDotNet.Attributes; #nullable enable namespace Avalonia.Benchmarks.Base { [MemoryDiagnoser] public class AvaloniaObject_GetValueInherited { private TestClass _root = null!; private TestClass _target = null!; public AvaloniaObject_GetValueInherited() { RuntimeHelpers.RunClassConstructor(typeof(TestClass).TypeHandle); } [Params(1, 2, 10, 50, 100, 200)] public int Depth { get; set; } [GlobalSetup] public void Setup() { _root = new(); _root.SetValue(TestClass.StringProperty, "foo"); _root.SetValue(TestClass.Struct1Property, new(1)); _root.SetValue(TestClass.Struct2Property, new(1)); _root.SetValue(TestClass.Struct3Property, new(1)); _root.SetValue(TestClass.Struct4Property, new(1)); _root.SetValue(TestClass.Struct5Property, new(1)); _root.SetValue(TestClass.Struct6Property, new(1)); _root.SetValue(TestClass.Struct7Property, new(1)); _root.SetValue(TestClass.Struct8Property, new(1)); var parent = _root; for (var i = 0; i < Depth; ++i) { var c = new TestClass(); ((ISetLogicalParent)c).SetParent(parent); parent = c; } _target = parent; } [Benchmark] public int GetInheritedValues() { var target = _target; var result = 0; for (var i = 0; i < 100; ++i) { result += target.GetValue(TestClass.StringProperty)?.Length ?? 0; result += target.GetValue(TestClass.Struct1Property).Int1; result += target.GetValue(TestClass.Struct2Property).Int1; result += target.GetValue(TestClass.Struct3Property).Int1; result += target.GetValue(TestClass.Struct4Property).Int1; result += target.GetValue(TestClass.Struct5Property).Int1; result += target.GetValue(TestClass.Struct6Property).Int1; result += target.GetValue(TestClass.Struct7Property).Int1; result += target.GetValue(TestClass.Struct8Property).Int1; } return result; } private class TestClass : Control { public static readonly StyledProperty StringProperty = AvaloniaProperty.Register("String", inherits: true); public static readonly StyledProperty Struct1Property = AvaloniaProperty.Register("Struct1", inherits: true); public static readonly StyledProperty Struct2Property = AvaloniaProperty.Register("Struct2", inherits: true); public static readonly StyledProperty Struct3Property = AvaloniaProperty.Register("Struct3", inherits: true); public static readonly StyledProperty Struct4Property = AvaloniaProperty.Register("Struct4", inherits: true); public static readonly StyledProperty Struct5Property = AvaloniaProperty.Register("Struct5", inherits: true); public static readonly StyledProperty Struct6Property = AvaloniaProperty.Register("Struct6", inherits: true); public static readonly StyledProperty Struct7Property = AvaloniaProperty.Register("Struct7", inherits: true); public static readonly StyledProperty Struct8Property = AvaloniaProperty.Register("Struct8", inherits: true); } } }