timdream / jszhuyin

JS 注音:JavaScript 自動選字注音輸入法;"Smart" Chinese Zhuyin Input Method in JavaScript.
https://jszhuyin.timdream.org/
MIT License
223 stars 19 forks source link

Optimize `JSZhuyinDataPackStorage#_getIncompleteMatchedSingleCodesInBlock` #24

Open timdream opened 8 years ago

timdream commented 8 years ago

Chrome DevTools Profiler has identified JSZhuyinDataPackStorage#_getIncompleteMatchedSingleCodesInBlock as the top offender -- the for loop should be optimized if possible.

timdream commented 7 years ago

Fixing this and #25 probably means implementing the properly B-tree data structure...

timdream commented 7 years ago

The current approach can be found in no-linear-search branch. The commits removes the linear for loop when search each of the block (i.e. the _getIncompleteMatchedSingleCodesInBlock function stated on the title of the issue), and search all the partial matching sounds by generate a list of matched completed sounds first.

Unfortunately, per benchmark test, that actually makes it slower by ~35%.

The next thing to try would be try to reduce the list of completed sounds, since there are 4095 combinations yet in our database per my last investigation there are only around 1001 sounds exists. Implement that might reduce 76% of our call to the _searchBlock() functions.

B-tree probably won't help here since it only re-orders the values within the linear array, not actually change how we search things within it. But that could still be the next thing to try if the previous attempt proves unhelpful.