MaterialDesignExtensions icon indicating copy to clipboard operation
MaterialDesignExtensions copied to clipboard

TextBoxSuggestions in DataGrid causes HUGE memory allocation, blocks main thread

Open WillEhrendreich opened this issue 4 years ago • 4 comments
trafficstars

I've been trying to figure this out for a while now, and I can't seem to solve it.

I have your TextBoxSuggestions in a datagrid cell, and as I'm building the grid, the more rows i have the more it just blocks and freezes, until all rows are in.

in a DG with about 150 rows, I shoot up from 500mb to 1.3gb in memory usage, so I think it's duplicating my source list of 23,000 string possibilities for each of the two cells per row that have the TBS control.

it seems to be automatically running the search function on each one, and that blocks until it searches the contents and completes it.

can this run async?

can it reference the same list instead of duplicating?

how would I change it?

I tried to make a modified version of things from your sourcecode, thinking if i just pulled out a copy into my own project, then changed things to use IAsyncEnumerable<T> instead or Task<T>, I could get it to not block, but alas, upon doing that, The textbox wouldn't show up period, while still allocating heaps on the heap, and blocking the main thread.

does this have to do with the 300ms delay? is that stopping it ?

it seems to go much worse the more rows I have, so it's obviously doing it in series instead of parallel, and I'm not sure if it's something I'm doing that is making it chew through one at a time, or if it's inherent to the control in some way I'm not influencing?

please let me know what your thoughts and recommendations are!

Thank you!!

WillEhrendreich avatar May 13 '21 20:05 WillEhrendreich

@WillEhrendreich I assume that TextBox.Text is set during the UI initialization. That causes a TextChanged event which in turn triggers a search. In your case that is happening several hundred times at the same time. That's pretty much work for any CPU. After a search the UI is the bottleneck, because it can only be changed by the main thread. So paralellization cannot help out here.

I will prevent this initial search, because it seems unnecessary to me. I have the feeling, that it's more the TextBoxSuggestions control's fault than of your code.

spiegelp avatar May 14 '21 16:05 spiegelp

ok.. how do I stop that event from triggering a search right away?

WillEhrendreich avatar May 17 '21 17:05 WillEhrendreich

@WillEhrendreich I need to change the search trigger inside the TextBoxSuggestions control (see commit above).

The 300ms delay is meant to avoid unnecessary searches with each and every key hit. Otherwise five key hits would trigger five searches. With the delay, if you type fast enough, the actual search will only be triggerd once after the fifth key hit.

The source list can be shared for all controls. Consider setting the same ITextBoxSuggestionsSource for every control or let the ITextBoxSuggestionsSource read from the same list.

spiegelp avatar May 21 '21 19:05 spiegelp

@WillEhrendreich Please try the latest pre-release v4.0.0-a01 and check if you get a better performance in your app.

spiegelp avatar May 24 '21 08:05 spiegelp