Faster SelectFromList

Hi guys,

TL;DR
SelectFromList can be improved by 71% (3.5x faster) on large lists

Time to share my findings. And I’ve create a github branch commit.
Do check this yourself (or I can share a simple script to show if needed)

Case:
My list contains 2200 items which I want to search / select from using forms.SelectFromList.

Problems:

  1. Loading time was roughly 7 seconds from click till show popup.
  2. Using the search field was horribly slow and typing in that field even worse.

So I challenged myself to improve it.

First my own scripting.

  1. Changed loading a csv file (or .txt with tabs) into a json file
  2. Changed loading not csvReader obviously but json … which was slow … into orjson which was … a bit faster. But still slow.

Then pyrevit.
The loading was mainly located in rendering the long list.
I didn’t touch the SelectFromList Class / scripting … took that for granted as it wasn’t the main issue for me.

My findigns: the xaml took too long to render.

Turned out

<ItemsPanelTemplate x:Key="ItemsPanelTemplate">
             <StackPanel />
</ItemsPanelTemplate>

is slow. Very slow. Not noticable on small lists, but on large lists …

Fix for SelectFromList.xaml
replace
<StackPanel/> for <VirtualizingStackPanel />
(stackpanel renders everything at once. virtual renders only visible.)

and add to <ListView x:Name="list_lb"

VirtualizingStackPanel.IsVirtualizing="True <!-- Ensures items are created only when needed.-->
VirtualizingStackPanel.VirtualizationMode="Recycling" <!-- Reuses item containers instead of destroying/recreating them.-->
ScrollViewer.CanContentScroll="True" <!-- Allows scrolling without loading all items at once

Curious what you think of it!

Cheers

@sanzoghenzo of course I’m sharing improvements! Sharing is caring :heart:

6 Likes