AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.3k stars 2.19k forks source link

Avalonia DataGrid sorting performance issue on a large datasets #7241

Open flarive opened 2 years ago

flarive commented 2 years ago

Hi,

I'm using Avalonia DataGrid 0.10.10 with .net 6.0 on vs2022.

I'm building a log reader app and when i fill the datagrid with millions of lines (4.000.000 for example), the column sorting is very slow and is blocking the UI during something like 10 seconds.

I'm using a DataGridCollecrtionView and simple types as DataGrid column SortMemberPath, such as int, string, enums... I have not implemented my own IComparable logic, so i can deduce than the slow sorting performance is comming from Avalonia Datagrid itself.

I think i know where the problem is. The reflection done from SortMemberPath value on millions of lines is probably taking a lot of time.

Is there a way to easily improve sorting performance on huge datasets ? WPF datagrid column sorting is much faster. I would also be a good a idea to display a mouse wait cursor while sorting and remove it when sorting is finished.

Is there a way to bypass reflection by implementing my own DataGridCollectionView sorting logic in code behind without using SortMemberPath property ?

Thanks a lot for your help !

I love Avalonia :)

Symbai commented 2 years ago

I would also be a good a idea to display a mouse wait cursor while sorting and remove it when sorting is finished

This is something the developer has to do if its required (although a waiting dialog is much better user experience). Especially when you say sorting takes several seconds. If the user tabs outside your app and do something else instead there will always be the waiting cursor, very annoying.

I have not implemented my own IComparable logic

Why not? Its only a few lines of code and this would be very interesting to know if it solves your issue, at least the difference between both.

flarive commented 2 years ago

Hi Symbai,

I would be happy to implement myself the waiting cursor when clicking on a column header (i have tried) but Avalonia DataGrid has only one event called OnSorting, it's not enough.. Without SortingStart and StartingEnd events, there is no way to do that properly and to remove the wait cursor when the sorting is finished.

The other problem is that the sorting is blocking UI, it means that the OnSorting is called after UI was being blocked by sorting logic. I gave tried to set the wait cursor in this Sorting event, but the wait cursors is displayed when sorting is finished and not before it starts :(

Why do you want me to implement my own IComparable ? I don't need it, i'm sorting int columns, int already implements IComparable. I'm trying to explain you that the DataGrid internal sorting is slow on huge datasets because of sort property reflection is done millions of times when you display millions of items.

Symbai commented 2 years ago

Why do you want me to implement my own IComparable ?

sign because you already said yourself you dont know if this is faster + you already asked how to avoid (reflection on) SortMemberPath. Implementing your own compare logic would avoid this and you could tell us if its faster or not. You didn't provide any example project. Do you want us to create our own project just for you and create our own compare logic just for you, so we can answer both of your questions? Because this won't happen, sorry.

flarive commented 2 years ago

Thank you for your anwser... Sorry, you're right, i don't give any code so it's not easy for you to answer, i agree :)

Humm... I don't think implementing my own comparer would change anything. The reflection would be made even with my own comparer implementation. I'm looking for a way to sort data in code behind without using SortMemberPath (SortMemberPath = reflection = evil).

Symbai commented 2 years ago

Your own Comparer wouldn't use any reflection, its nothing else than (simplified):

public class BlaComp : IComparer<Bla>
{
    public int Compare(Bla x, Bla y)
    {
        return x.Property.CompareTo(y.Property);
    }
}

Again no example project, no code shown = less chance you get the help you are looking for. If you don't want to provide that thats absolutely fine but then you should, no you have to, make your own tests etc.