faster wcwidth with lookup table#798
faster wcwidth with lookup table#798Tyriar merged 11 commits intoxtermjs:masterfrom jerch:faster_wcwidth
Conversation
src/InputHandler.ts
Outdated
| } | ||
| return function (num) { | ||
| num = num | 0; | ||
| return (num < 65536) ? (table[num >> 2] >> ((num & 3) * 2)) & 3 : wcwidthHigh(num); |
There was a problem hiding this comment.
Should this have an early return for ascii characters as they are the vast majority, or would that not boost perf much at this point?
There was a problem hiding this comment.
Could do with a comment explaining the bit shifting part
There was a problem hiding this comment.
@Tyriar Well it gives 5% speedup to exclude the ascii range from the shifting & lookup. So yeah gonna change it.
src/InputHandler.ts
Outdated
| return 1; | ||
| } | ||
| let table = new Uint8Array(16384); | ||
| for (let i = 0; i < 16384; ++i) { |
There was a problem hiding this comment.
Does this increase startup in a noticeable way?
There was a problem hiding this comment.
Is that 10-16ms when the first terminal is launched or when the script is loaded?
There was a problem hiding this comment.
When the script loads, it is done during import.
src/InputHandler.ts
Outdated
| } | ||
| return 1; | ||
| } | ||
| let table = new Uint8Array(16384); |
There was a problem hiding this comment.
A named variable/comment explaining 16384 would be good ((65536-4)/4+1?).
There was a problem hiding this comment.
Also this needs to be conditional and fallback to a regular array for browsers without support
- commenting the bit shifts - switching to Uint32Array - conditional for TypedArray - fixing Math.floor slowdown in bisearch
|
@Tyriar Changed the table to |
|
@jerch this would mean doing it on either terminal creation ( |
|
Addressed the lazy loading with the last commit, the lookup table is created at the first call with a char > 127. Benchmarks with node V8 on my machine are now:
|
src/InputHandler.test.ts
Outdated
| })({nul: 0, control: 0}); // configurable options | ||
|
|
||
| describe('wcwidth', () => { | ||
| it('same as old implementation for BMP and individual higher', () => { |
There was a problem hiding this comment.
Can't get more confident about no functional changes than this 😉
|
@jerch CI failed on macOS: Might need to increase the timeout of that test? https://travis-ci.org/sourcelair/xterm.js/jobs/254546569 |
|
Hmm yeah seems the travis OSX boxes are pretty busy from time to time and tend timeout longer running tests. The test runs in under a second here... |
This PR optimizes
wcwidthby using a lookup table for BMP characters. For a BMP char it boils down to a simple table lookup with some shifting magic to pack the data into 16k bytes. Speedup is at least 10 times. Equality tests included.