Measure.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections.Generic;
  2. using System.Runtime.CompilerServices;
  3. using Avalonia.Controls;
  4. using Avalonia.Layout;
  5. using Avalonia.UnitTests;
  6. using BenchmarkDotNet.Attributes;
  7. namespace Avalonia.Benchmarks.Layout
  8. {
  9. [MemoryDiagnoser]
  10. public class Measure
  11. {
  12. private readonly TestRoot _root;
  13. private readonly List<Control> _controls = new List<Control>();
  14. public Measure()
  15. {
  16. var panel = new StackPanel();
  17. _root = new TestRoot
  18. {
  19. Child = panel,
  20. Renderer = new NullRenderer()
  21. };
  22. _controls.Add(panel);
  23. _controls = ControlHierarchyCreator.CreateChildren(_controls, panel, 3, 5, 5);
  24. _root.LayoutManager.ExecuteInitialLayoutPass();
  25. }
  26. [Benchmark, MethodImpl(MethodImplOptions.NoInlining)]
  27. public void Remeasure()
  28. {
  29. _root.InvalidateMeasure();
  30. foreach (var control in _controls)
  31. {
  32. // Use an unsafe accessor instead of InvalidateMeasure, otherwise a lot of time is spent invalidating
  33. // controls, which we don't want: this benchmark is supposed to be focused on Measure/Arrange.
  34. SetIsMeasureValid(control, false);
  35. SetIsArrangeValid(control, false);
  36. }
  37. _root.LayoutManager.ExecuteLayoutPass();
  38. }
  39. [UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_" + nameof(Layoutable.IsMeasureValid))]
  40. private static extern void SetIsMeasureValid(Layoutable layoutable, bool value);
  41. [UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_" + nameof(Layoutable.IsArrangeValid))]
  42. private static extern void SetIsArrangeValid(Layoutable layoutable, bool value);
  43. }
  44. }