using System.Runtime.CompilerServices; using Avalonia.Controls; using Avalonia.PropertyStore; using Avalonia.Styling; using Avalonia.UnitTests; using BenchmarkDotNet.Attributes; #nullable enable namespace Avalonia.Benchmarks.Styling { [MemoryDiagnoser] public class Style_NonActive { private TestClass _target = null!; public Style_NonActive() { RuntimeHelpers.RunClassConstructor(typeof(TestClass).TypeHandle); } [GlobalSetup] public void Setup() { _target = new TestClass(); var style = new Style(x => x.OfType().Class("foo")) { Setters = { new Setter(TestClass.StringProperty, "foo") } }; StyleHelpers.TryAttach(style, _target); _target.SetValue(TestClass.StringProperty, "foo"); _target.SetValue(TestClass.Struct1Property, new(1)); _target.SetValue(TestClass.Struct2Property, new(1)); _target.SetValue(TestClass.Struct3Property, new(1)); _target.SetValue(TestClass.Struct4Property, new(1)); _target.SetValue(TestClass.Struct5Property, new(1)); _target.SetValue(TestClass.Struct6Property, new(1)); _target.SetValue(TestClass.Struct7Property, new(1)); _target.SetValue(TestClass.Struct8Property, new(1)); } [Benchmark] public void Toggle_NonActive_Style_Activation() { for (var i = 0; i < 100; ++i) { _target.Classes.Add("foo"); _target.Classes.Remove("foo"); } } private class TestClass : Control { public static readonly StyledProperty StringProperty = AvaloniaProperty.Register("String"); public static readonly StyledProperty Struct1Property = AvaloniaProperty.Register("Struct1"); public static readonly StyledProperty Struct2Property = AvaloniaProperty.Register("Struct2"); public static readonly StyledProperty Struct3Property = AvaloniaProperty.Register("Struct3"); public static readonly StyledProperty Struct4Property = AvaloniaProperty.Register("Struct4"); public static readonly StyledProperty Struct5Property = AvaloniaProperty.Register("Struct5"); public static readonly StyledProperty Struct6Property = AvaloniaProperty.Register("Struct6"); public static readonly StyledProperty Struct7Property = AvaloniaProperty.Register("Struct7"); public static readonly StyledProperty Struct8Property = AvaloniaProperty.Register("Struct8"); } } }