OmixVisualization / qtjambi

QtJambi is a wrapper for using Qt in Java.
http://www.qtjambi.io
Other
354 stars 40 forks source link

QModelIndex implementation of 5.15.15 slower than 5.15.11 #163

Closed paul35621 closed 1 year ago

paul35621 commented 1 year ago

I had some performance problems with QTreeView and decided to try a newer version of QtJambi.

I updated QtJambi from 5.15.11 to 5.15.15 and updated my code to deal with the new QModelIndex implementation. (by using invalid QModelIndex objects instead of null)

Unfortunately, it runs slower. I have a tree of about 25000 nodes. When doing an "expand all", it takes 0.273s with version 5.15.11 and 0.538s with version 5.15.15 on my computer. I'm using Qt 5.15.2 and Java 17 on Windows 10.

Is it really better to have QModelIndex as a Java wrapper for the native Qt type? Perhaps it makes sense to keep QModelIndex implemented in Java as was done in 5.15.11 / 6.4.1, but in a way that it is compatible with the new 5.15.12 / 6.4.2 interface, so with using invalid QModelIndex objects instead of null and having an isValid method.

Can I do something to improve performance? I'm considering caching QModelIndex objects. Maybe 5.15.15 even outperforms 5.15.11 when using cached QModelIndex objects. Would it be safe to call QAbstractItemModel.createIndex from an other thread?

omix commented 1 year ago

Thank you for your report. I wonder what is java-native / native-java transfers are done for QModelIndex when you expand all. Can you list the used methods?

I did a performance test between 5.15.11 and .15 for QModelIndex native-to-java convertion. While .11 takes about 113 ms for making 50000 QModelIndexes available in Java, .15 takes about 170 ms. By doing more optimizations in the code I am able to reduce it down to 148 ms. If I furthermore disable dependency references it is much slower up to 120 ms. However, the latter is too dangerous.

I think it's not possible to reach the same performance with native QModelIndex as with a lightweight implementation.

omix commented 1 year ago

With another approach I was able to reduce duration to 121 ms.

paul35621 commented 1 year ago

I'm writing some tests for this, I'll come with source code.

paul35621 commented 1 year ago

I wrote a test program in Kotlin with 6 different approaches to several collapse/expand methods: qtreeview-test.zip

I'm going with method 4 (method 6 is slightly faster, but more complicated) and stick with QtJambi 5.15.11 for a while.

For increasing performance I found:

It would be nice if newer versions of QtJambi could also use a lightweight QModelIndex implementation, but I understand that this is more error prone and more work to maintain.

omix commented 1 year ago

Upcoming release QtJambi 6.5.2 will bring a more efficient but native implementation of QModelIndex.