xiaoyifang / goldendict-ng

The Next Generation GoldenDict
https://xiaoyifang.github.io/goldendict-ng/
Other
1.71k stars 95 forks source link

`WordFinder::updateResults` invokes intensive case folding every second? #1943

Closed shenlebantongying closed 1 week ago

shenlebantongying commented 1 week ago

I was playing the XCode's Instruments.app and find that WordFinder::updateResults is potentially slowing down searching.

The data is recorded for a running debug build GD and do a few word searches.

There is a ~1.5s region marked as "hang" and it turns out to be all case folding.

I am not sure, but the code says updateResults which contains the foldCase appears to be called every 1s.

https://github.com/xiaoyifang/goldendict-ng/blob/453948155a2df5ce4bbe989eab60c05f6ae6dbd1/src/wordfinder.cc#L23-L26

P1: a region of 1.5s marked as hang, with mostly case folding

P2: No idea why would the computer switch CPU core (they are in the main thread).

But in this 1.5s, CPU core 4's result in backtrace form

CPU core 6's result in flamegrahe


Above measurement could be heavily affected by the fact that my computer was doing lots of staff at that time, hence got marked as hang.

Though, I can consistently observe it may range from 0.1~0.5sec in the main thread (No idea why main thread would be named as "CrBrowserMain" btw).


Also btw, the findMatches appears to be doing duplicated folding?

shenlebantongying commented 1 week ago

Question, for a 1000 to 2500 cases switch statement, would it be faster if it gets loaded as a hash map? Would compiler do something about it?

I suppose that it will become a jump table.

Edit: the switch is insanely good for simple one to one mapping at -O2 or -O3.

shenlebantongying commented 1 week ago

QChar::toCaseFolded cannot be used -> https://github.com/xiaoyifang/goldendict-ng/pull/1945#discussion_r1839081300

I will just close this because this becomes refactoring the core part again.