From 2904491ed68ba77c826ade07debf1d0a50811e3f Mon Sep 17 00:00:00 2001 From: Fidarit Mullayanov Date: Tue, 23 Dec 2025 01:57:54 +0500 Subject: [PATCH 1/2] using PolySharp instead of using own polyfills --- Avalonia.Controls.TreeDataGrid.sln | 11 +- .../Avalonia.Controls.TreeDataGrid.csproj | 8 + .../Primitives/TreeDataGridColumnHeader.cs | 2 +- .../StandardExtensions/BitOperations.cs | 43 +-- .../StandardExtensions/Double.cs | 5 +- .../StandardExtensions/IsExternalInit.cs | 7 - .../StandardExtensions/NullableAttributes.cs | 138 --------- .../StandardExtensions/Range.cs | 271 ------------------ ...nia.Controls.TreeDataGrid.Benchmark.csproj | 29 ++ .../Benchmarks.cs | 116 ++++++++ .../Program.cs | 18 ++ 11 files changed, 187 insertions(+), 461 deletions(-) delete mode 100644 src/Avalonia.Controls.TreeDataGrid/StandardExtensions/IsExternalInit.cs delete mode 100644 src/Avalonia.Controls.TreeDataGrid/StandardExtensions/NullableAttributes.cs delete mode 100644 src/Avalonia.Controls.TreeDataGrid/StandardExtensions/Range.cs create mode 100644 tests/Avalonia.Controls.TreeDataGrid.Benchmark/Avalonia.Controls.TreeDataGrid.Benchmark.csproj create mode 100644 tests/Avalonia.Controls.TreeDataGrid.Benchmark/Benchmarks.cs create mode 100644 tests/Avalonia.Controls.TreeDataGrid.Benchmark/Program.cs diff --git a/Avalonia.Controls.TreeDataGrid.sln b/Avalonia.Controls.TreeDataGrid.sln index 767b644e..861182a2 100644 --- a/Avalonia.Controls.TreeDataGrid.sln +++ b/Avalonia.Controls.TreeDataGrid.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.14.36603.0 d17.14 +# Visual Studio Version 18 +VisualStudioVersion = 18.1.11312.151 d18.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{00673DCE-6218-4BAC-9026-F9BA18D241AE}" EndProject @@ -38,6 +38,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{754FC069-D docs\selection.md = docs\selection.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avalonia.Controls.TreeDataGrid.Benchmark", "tests\Avalonia.Controls.TreeDataGrid.Benchmark\Avalonia.Controls.TreeDataGrid.Benchmark.csproj", "{72C8B10B-A8A6-9B7E-C798-B5C392FDB7F1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -58,6 +60,10 @@ Global {9A36E37E-2C03-4B5A-B7EE-A91DC95C3E4A}.Release|Any CPU.Build.0 = Release|Any CPU {D45C7B46-A12C-4412-8397-B51B75A09999}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D45C7B46-A12C-4412-8397-B51B75A09999}.Release|Any CPU.ActiveCfg = Release|Any CPU + {72C8B10B-A8A6-9B7E-C798-B5C392FDB7F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {72C8B10B-A8A6-9B7E-C798-B5C392FDB7F1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {72C8B10B-A8A6-9B7E-C798-B5C392FDB7F1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {72C8B10B-A8A6-9B7E-C798-B5C392FDB7F1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -66,6 +72,7 @@ Global {90F93B90-3622-4332-9E7A-E6D7062422C1} = {00673DCE-6218-4BAC-9026-F9BA18D241AE} {9A36E37E-2C03-4B5A-B7EE-A91DC95C3E4A} = {A8CF7AE6-C46F-4930-85AF-13126EAC5E6E} {D45C7B46-A12C-4412-8397-B51B75A09999} = {AE911386-1B6E-4148-AE8F-9D27325349AF} + {72C8B10B-A8A6-9B7E-C798-B5C392FDB7F1} = {00673DCE-6218-4BAC-9026-F9BA18D241AE} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {6C219532-B67B-4A0F-BFC0-A22CBE46DFF4} diff --git a/src/Avalonia.Controls.TreeDataGrid/Avalonia.Controls.TreeDataGrid.csproj b/src/Avalonia.Controls.TreeDataGrid/Avalonia.Controls.TreeDataGrid.csproj index 5afe089f..07fa28b3 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Avalonia.Controls.TreeDataGrid.csproj +++ b/src/Avalonia.Controls.TreeDataGrid/Avalonia.Controls.TreeDataGrid.csproj @@ -19,6 +19,14 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeader.cs b/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeader.cs index bb3301fb..3a53aaed 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeader.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeader.cs @@ -167,7 +167,7 @@ private void ResizerDragDelta(object? sender, VectorEventArgs e) var pixelWidth = _model.Width.IsAbsolute ? _model.Width.Value : Bounds.Width; - if (double.IsNaN(pixelWidth) || double.IsInfinity(pixelWidth) || pixelWidth + e.Vector.X < 0) + if (!Double.IsFinite(pixelWidth) || pixelWidth + e.Vector.X < 0) return; var width = new GridLength(pixelWidth + e.Vector.X, GridUnitType.Pixel); diff --git a/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/BitOperations.cs b/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/BitOperations.cs index 4a44232c..9f708c82 100644 --- a/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/BitOperations.cs +++ b/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/BitOperations.cs @@ -43,41 +43,6 @@ public static int Log2(uint value) return Log2SoftwareFallback(value); } - /// - /// Returns the integer (floor) log of the specified value, base 2. - /// Note that by convention, input value 0 returns 0 since log(0) is undefined. - /// - /// The value. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int Log2(ulong value) - { - value |= 1; - - uint hi = (uint)(value >> 32); - - if (hi == 0) - { - return Log2((uint)value); - } - - return 32 + Log2(hi); - } - - /// - /// Returns the integer (floor) log of the specified value, base 2. - /// Note that by convention, input value 0 returns 0 since log(0) is undefined. - /// - /// The value. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int Log2(nuint value) - { -#if TARGET_64BIT - return Log2((ulong)value); -#else - return Log2((uint)value); -#endif - } - /// /// Returns the integer (floor) log of the specified value, base 2. /// Note that by convention, input value 0 returns 0 since Log(0) is undefined. @@ -96,12 +61,8 @@ private static int Log2SoftwareFallback(uint value) value |= value >> 08; value |= value >> 16; - // uint.MaxValue >> 27 is always in range [0 - 31] so we use Unsafe.AddByteOffset to avoid bounds check - return Unsafe.AddByteOffset( - // Using deBruijn sequence, k=2, n=5 (2^5=32) : 0b_0000_0111_1100_0100_1010_1100_1101_1101u - ref MemoryMarshal.GetReference(Log2DeBruijn), - // uint|long -> IntPtr cast on 32-bit platforms does expensive overflow checks not needed here - (IntPtr)(int)((value * 0x07C4ACDDu) >> 27)); + // Using deBruijn sequence, k=2, n=5 (2^5=32) : 0b_0000_0111_1100_0100_1010_1100_1101_1101u + return Log2DeBruijn[(int)((value * 0x07C4ACDDu) >> 27)]; } } } diff --git a/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/Double.cs b/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/Double.cs index 40a4d399..a465956c 100644 --- a/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/Double.cs +++ b/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/Double.cs @@ -1,8 +1,11 @@ -namespace Avalonia; +using System.Runtime.CompilerServices; + +namespace Avalonia; internal static class Double { + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsFinite(double value) { return !double.IsNaN(value) && !double.IsInfinity(value); diff --git a/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/IsExternalInit.cs b/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/IsExternalInit.cs deleted file mode 100644 index 2822f9d1..00000000 --- a/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/IsExternalInit.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace System.Runtime.CompilerServices; - -/// -/// A class which allows to use C# 9's init and record features -/// in older target frameworks like .NET Standard 2.0 -/// -internal static class IsExternalInit { } diff --git a/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/NullableAttributes.cs b/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/NullableAttributes.cs deleted file mode 100644 index c206dff2..00000000 --- a/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/NullableAttributes.cs +++ /dev/null @@ -1,138 +0,0 @@ -#pragma warning disable MA0048 // File name must match type name -#define INTERNAL_NULLABLE_ATTRIBUTES - -// https://github.com/dotnet/corefx/blob/48363ac826ccf66fbe31a5dcb1dc2aab9a7dd768/src/Common/src/CoreLib/System/Diagnostics/CodeAnalysis/NullableAttributes.cs - -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -namespace System.Diagnostics.CodeAnalysis -{ - /// Specifies that null is allowed as an input even if the corresponding type disallows it. - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] -#if INTERNAL_NULLABLE_ATTRIBUTES - internal -#else - public -#endif - sealed class AllowNullAttribute : Attribute - { } - - /// Specifies that null is disallowed as an input even if the corresponding type allows it. - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] -#if INTERNAL_NULLABLE_ATTRIBUTES - internal -#else - public -#endif - sealed class DisallowNullAttribute : Attribute - { } - - /// Specifies that an output may be null even if the corresponding type disallows it. - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] -#if INTERNAL_NULLABLE_ATTRIBUTES - internal -#else - public -#endif - sealed class MaybeNullAttribute : Attribute - { } - - /// Specifies that an output will not be null even if the corresponding type allows it. - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] -#if INTERNAL_NULLABLE_ATTRIBUTES - internal -#else - public -#endif - sealed class NotNullAttribute : Attribute - { } - - /// Specifies that when a method returns , the parameter may be null even if the corresponding type disallows it. - [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] -#if INTERNAL_NULLABLE_ATTRIBUTES - internal -#else - public -#endif - sealed class MaybeNullWhenAttribute : Attribute - { - /// Initializes the attribute with the specified return value condition. - /// - /// The return value condition. If the method returns this value, the associated parameter may be null. - /// - public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; - - /// Gets the return value condition. - public bool ReturnValue { get; } - } - - /// Specifies that when a method returns , the parameter will not be null even if the corresponding type allows it. - [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] -#if INTERNAL_NULLABLE_ATTRIBUTES - internal -#else - public -#endif - sealed class NotNullWhenAttribute : Attribute - { - /// Initializes the attribute with the specified return value condition. - /// - /// The return value condition. If the method returns this value, the associated parameter will not be null. - /// - public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; - - /// Gets the return value condition. - public bool ReturnValue { get; } - } - - /// Specifies that the output will be non-null if the named parameter is non-null. - [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] -#if INTERNAL_NULLABLE_ATTRIBUTES - internal -#else - public -#endif - sealed class NotNullIfNotNullAttribute : Attribute - { - /// Initializes the attribute with the associated parameter name. - /// - /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null. - /// - public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName; - - /// Gets the associated parameter name. - public string ParameterName { get; } - } - - /// Applied to a method that will never return under any circumstance. - [AttributeUsage(AttributeTargets.Method, Inherited = false)] -#if INTERNAL_NULLABLE_ATTRIBUTES - internal -#else - public -#endif - sealed class DoesNotReturnAttribute : Attribute - { } - - /// Specifies that the method will not return if the associated Boolean parameter is passed the specified value. - [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] -#if INTERNAL_NULLABLE_ATTRIBUTES - internal -#else - public -#endif - sealed class DoesNotReturnIfAttribute : Attribute - { - /// Initializes the attribute with the specified parameter value. - /// - /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to - /// the associated parameter matches this value. - /// - public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue; - - /// Gets the condition parameter value. - public bool ParameterValue { get; } - } -} diff --git a/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/Range.cs b/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/Range.cs deleted file mode 100644 index 35a9bdcb..00000000 --- a/src/Avalonia.Controls.TreeDataGrid/StandardExtensions/Range.cs +++ /dev/null @@ -1,271 +0,0 @@ -using System.Runtime.CompilerServices; - -namespace System -{ - /// Represent a type can be used to index a collection either from the start or the end. - /// - /// Index is used by the C# compiler to support the new index syntax - /// - /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 } ; - /// int lastElement = someArray[^1]; // lastElement = 5 - /// - /// - internal readonly struct Index : IEquatable - { - private readonly int _value; - - /// Construct an Index using a value and indicating if the index is from the start or from the end. - /// The index value. it has to be zero or positive number. - /// Indicating if the index is from the start or from the end. - /// - /// If the Index constructed from the end, index value 1 means pointing at the last element and index value 0 means pointing at beyond last element. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Index(int value, bool fromEnd = false) - { - if (value < 0) - { - throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); - } - - if (fromEnd) - _value = ~value; - else - _value = value; - } - - // The following private constructors mainly created for perf reason to avoid the checks - private Index(int value) - { - _value = value; - } - - /// Create an Index pointing at first element. - public static Index Start => new Index(0); - - /// Create an Index pointing at beyond last element. - public static Index End => new Index(~0); - - /// Create an Index from the start at the position indicated by the value. - /// The index value from the start. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Index FromStart(int value) - { - if (value < 0) - { - throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); - } - - return new Index(value); - } - - /// Create an Index from the end at the position indicated by the value. - /// The index value from the end. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Index FromEnd(int value) - { - if (value < 0) - { - throw new ArgumentOutOfRangeException(nameof(value), "value must be non-negative"); - } - - return new Index(~value); - } - - /// Returns the index value. - public int Value - { - get - { - if (_value < 0) - { - return ~_value; - } - else - { - return _value; - } - } - } - - /// Indicates whether the index is from the start or the end. - public bool IsFromEnd => _value < 0; - - /// Calculate the offset from the start using the giving collection length. - /// The length of the collection that the Index will be used with. length has to be a positive value - /// - /// For performance reason, we don't validate the input length parameter and the returned offset value against negative values. - /// we don't validate either the returned offset is greater than the input length. - /// It is expected Index will be used with collections which always have non negative length/count. If the returned offset is negative and - /// then used to index a collection will get out of range exception which will be same affect as the validation. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int GetOffset(int length) - { - var offset = _value; - if (IsFromEnd) - { - // offset = length - (~value) - // offset = length + (~(~value) + 1) - // offset = length + value + 1 - - offset += length + 1; - } - return offset; - } - - /// Indicates whether the current Index object is equal to another object of the same type. - /// An object to compare with this object - public override bool Equals(object? value) => value is Index && _value == ((Index)value)._value; - - /// Indicates whether the current Index object is equal to another Index object. - /// An object to compare with this object - public bool Equals(Index other) => _value == other._value; - - /// Returns the hash code for this instance. - public override int GetHashCode() => _value; - - /// Converts integer number to an Index. - public static implicit operator Index(int value) => FromStart(value); - - /// Converts the value of the current Index object to its equivalent string representation. - public override string ToString() - { - if (IsFromEnd) - return "^" + ((uint)Value).ToString(); - - return ((uint)Value).ToString(); - } - } - - /// Represent a range has start and end indexes. - /// - /// Range is used by the C# compiler to support the range syntax. - /// - /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 }; - /// int[] subArray1 = someArray[0..2]; // { 1, 2 } - /// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 } - /// - /// - internal readonly struct Range : IEquatable - { - /// Represent the inclusive start index of the Range. - public Index Start { get; } - - /// Represent the exclusive end index of the Range. - public Index End { get; } - - /// Construct a Range object using the start and end indexes. - /// Represent the inclusive start index of the range. - /// Represent the exclusive end index of the range. - public Range(Index start, Index end) - { - Start = start; - End = end; - } - - /// Indicates whether the current Range object is equal to another object of the same type. - /// An object to compare with this object - public override bool Equals(object? value) => - value is Range r && - r.Start.Equals(Start) && - r.End.Equals(End); - - /// Indicates whether the current Range object is equal to another Range object. - /// An object to compare with this object - public bool Equals(Range other) => other.Start.Equals(Start) && other.End.Equals(End); - - /// Returns the hash code for this instance. - public override int GetHashCode() - { - return Start.GetHashCode() * 31 + End.GetHashCode(); - } - - /// Converts the value of the current Range object to its equivalent string representation. - public override string ToString() - { - return Start + ".." + End; - } - - /// Create a Range object starting from start index to the end of the collection. - public static Range StartAt(Index start) => new Range(start, Index.End); - - /// Create a Range object starting from first element in the collection to the end Index. - public static Range EndAt(Index end) => new Range(Index.Start, end); - - /// Create a Range object starting from first element to the end. - public static Range All => new Range(Index.Start, Index.End); - - /// Calculate the start offset and length of range object using a collection length. - /// The length of the collection that the range will be used with. length has to be a positive value. - /// - /// For performance reason, we don't validate the input length parameter against negative values. - /// It is expected Range will be used with collections which always have non negative length/count. - /// We validate the range is inside the length scope though. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int Offset, int Length) GetOffsetAndLength(int length) - { - int start; - var startIndex = Start; - if (startIndex.IsFromEnd) - start = length - startIndex.Value; - else - start = startIndex.Value; - - int end; - var endIndex = End; - if (endIndex.IsFromEnd) - end = length - endIndex.Value; - else - end = endIndex.Value; - - if ((uint)end > (uint)length || (uint)start > (uint)end) - { - throw new ArgumentOutOfRangeException(nameof(length)); - } - - return (start, end - start); - } - } -} - -namespace System.Runtime.CompilerServices -{ - internal static class RuntimeHelpers - { - /// - /// Slices the specified array using the specified range. - /// - public static T[] GetSubArray(T[] array, Range range) - { - if (array == null) - { - throw new ArgumentNullException(nameof(array)); - } - - (int offset, int length) = range.GetOffsetAndLength(array.Length); - - if (default(T) != null || typeof(T[]) == array.GetType()) - { - // We know the type of the array to be exactly T[]. - - if (length == 0) - { - return []; - } - - var dest = new T[length]; - Array.Copy(array, offset, dest, 0, length); - return dest; - } - else - { - // The array is actually a U[] where U:T. - var dest = (T[])Array.CreateInstance(array.GetType().GetElementType(), length); - Array.Copy(array, offset, dest, 0, length); - return dest; - } - } - } -} diff --git a/tests/Avalonia.Controls.TreeDataGrid.Benchmark/Avalonia.Controls.TreeDataGrid.Benchmark.csproj b/tests/Avalonia.Controls.TreeDataGrid.Benchmark/Avalonia.Controls.TreeDataGrid.Benchmark.csproj new file mode 100644 index 00000000..61b5d8ac --- /dev/null +++ b/tests/Avalonia.Controls.TreeDataGrid.Benchmark/Avalonia.Controls.TreeDataGrid.Benchmark.csproj @@ -0,0 +1,29 @@ + + + + net48;net10.0 + Exe + Avalonia.Controls.TreeDataGridBenchmark + + + + + + + + + + + + + + + TargetFramework=net48 + + + + + + + + diff --git a/tests/Avalonia.Controls.TreeDataGrid.Benchmark/Benchmarks.cs b/tests/Avalonia.Controls.TreeDataGrid.Benchmark/Benchmarks.cs new file mode 100644 index 00000000..2e0e6590 --- /dev/null +++ b/tests/Avalonia.Controls.TreeDataGrid.Benchmark/Benchmarks.cs @@ -0,0 +1,116 @@ +using System.Collections.Generic; +using System.Linq; +using Avalonia.Collections; +using Avalonia.Controls.Models; +using Avalonia.Controls.Models.TreeDataGrid; +using Avalonia.Controls.Primitives; +using Avalonia.Controls.TreeDataGridTests; +using Avalonia.Styling; +using Avalonia.Threading; +using BenchmarkDotNet.Attributes; + +namespace Avalonia.Controls.TreeDataGridBenchmark; + +public class Benchmarks +{ + private const int RowCount = 500; + private const int ColumnCount = 40; + + private TreeDataGrid? _target; + + [GlobalSetup] + public void Setup() + { + TestApplication.BuildAvaloniaApp().SetupWithoutStarting(); + _target = CreateTarget(); + } + + [Benchmark] + public void Scrolling_Down() + { + for (int i = 0; i < RowCount; i++) + { + _target.Scroll!.Offset = new Vector(0, 10 * i); + Layout(_target); + } + + for (int i = RowCount - 1; i >= 0; i--) + { + _target.Scroll!.Offset = new Vector(0, 10 * i); + Layout(_target); + } + } + + private static TreeDataGrid CreateTarget() + { + var items = new AvaloniaList([.. CreateModels("Item 0-", RowCount)]); + + var source = new FlatTreeDataGridSource(items); + source.Columns.Add(new TextColumn("ID", x => x.Id)); + + for (int i = 0; i < ColumnCount; i++) + { + source.Columns.Add(new TextColumn("Title", x => x.Title)); + } + + var target = new TreeDataGrid + { + Template = TestTemplates.TreeDataGridTemplate(), + Source = source, + }; + + var root = new TestWindow(target) + { + Styles = + { + TestTemplates.TreeDataGridExpanderCellStyle, + TestTemplates.TreeDataGridTemplateCellStyle, + new Style(x => x.Is()) + { + Setters = + { + new Setter(TreeDataGridRow.TemplateProperty, TestTemplates.TreeDataGridRowTemplate()), + } + }, + new Style(x => x.Is()) + { + Setters = + { + new Setter(TreeDataGridCell.HeightProperty, 10.0), + } + } + } + }; + + root.UpdateLayout(); + Dispatcher.UIThread.RunJobs(); + + return target; + } + + private static void Layout(TreeDataGrid target) + { + target.UpdateLayout(); + Dispatcher.UIThread.RunJobs(); + } + + private static IEnumerable CreateModels( + string titlePrefix, + int count, + int firstIndex = 0, + int firstId = 100) + { + return Enumerable.Range(0, count).Select(x => + new Model + { + Id = firstId + firstIndex + x, + Title = titlePrefix + (firstIndex + x), + }); + } + + private class Model : NotifyingBase + { + public int Id { get; set; } + public string? Title { get; set; } + } +} diff --git a/tests/Avalonia.Controls.TreeDataGrid.Benchmark/Program.cs b/tests/Avalonia.Controls.TreeDataGrid.Benchmark/Program.cs new file mode 100644 index 00000000..c5f7c5b7 --- /dev/null +++ b/tests/Avalonia.Controls.TreeDataGrid.Benchmark/Program.cs @@ -0,0 +1,18 @@ +using BenchmarkDotNet.Configs; +using BenchmarkDotNet.Diagnosers; +using BenchmarkDotNet.Environments; +using BenchmarkDotNet.Jobs; +using BenchmarkDotNet.Running; + +namespace Avalonia.Controls.TreeDataGridBenchmark; + +internal class Program +{ + private static void Main(string[] args) + { + var _ = BenchmarkRunner.Run(typeof(Program).Assembly, DefaultConfig.Instance + .AddJob(Job.Default.WithRuntime(ClrRuntime.Net48)) + .AddJob(Job.Default.WithRuntime(CoreRuntime.Core10_0)) + .AddDiagnoser(MemoryDiagnoser.Default)); + } +} From 4a7623fa16861c777b86a1d3b8602295d4bda04f Mon Sep 17 00:00:00 2001 From: Fidarit Mullayanov Date: Sat, 14 Mar 2026 19:19:43 +0500 Subject: [PATCH 2/2] optim: Block inlining of IntroSort --- src/Avalonia.Controls.TreeDataGrid/Utils/SortHelper.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Avalonia.Controls.TreeDataGrid/Utils/SortHelper.cs b/src/Avalonia.Controls.TreeDataGrid/Utils/SortHelper.cs index a7d8cfd8..b68b7dcf 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Utils/SortHelper.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Utils/SortHelper.cs @@ -105,6 +105,9 @@ private static void IntrospectiveSort(Span keys, Comparison comparer) } } + // IntroSort is recursive; block it from being inlined into itself as + // this is currenly not profitable. + [MethodImpl(MethodImplOptions.NoInlining)] private static void IntroSort(Span keys, int depthLimit, Comparison comparer) { Debug.Assert(!keys.IsEmpty);