xterm.js: initTable is slow, can we lazily load parts as they're needed?

On my fairly fast gaming desktop it’s over a frame:

image

It looks like initTable could be made so only load chunks of table as necessary, splitting it into 32 parts (66636/32=2048 codepoints) would probably make it have minimal impact on frames following a load.

https://github.com/xtermjs/xterm.js/blob/cb97ab35cb753f66e79a3b3eaa9442fbd3747c4d/src/CharWidth.ts#L126-L131

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 29 (29 by maintainers)

Most upvoted comments

@skprabhanjan Another approach - programmatically walk the 0 and 2 width codepoints to fill the table. Instead of repeatingly doing the bisect and isWideBMP eval for every single codepoint from 0 to 65536 we can simply create the lookup table cheaper by this:

function iTableProgrammed() {
  table.fill(1);
  table[0] = opts.nul;
  // control chars
  for (let i = 1; i < 32; ++i) {
    table[i] = opts.control;
  }
  for (let i = 0x7f; i < 0xa0; ++i) {
    table[i] = opts.control;
  }
  // combining 0
  for (let r = 0; r < COMBINING_BMP.length; ++r) {
    for (let i = COMBINING_BMP[r][0]; i < COMBINING_BMP[r][1]; ++i) {
      table[i] = 0;
    }
  }
  // wide chars
  for (let i = 0x1100; i <= 0x115f; ++i) {
    table[i] = 2;
  }
  table[0x2329] = 2;
  table[0x232a] = 2;
  for (let i = 0x2e80; i <= 0xa4cf; ++i) {
    table[i] = 2;
  }
  table[0x303f] = 1;  // wrongly added in loop before
  for (let i = 0xac00; i <= 0xd7a3; ++i) {
    table[i] = 2;
  }
  for (let i = 0xac00; i <= 0xd7a3; ++i) {
    table[i] = 2;
  }
  for (let i = 0xf900; i <= 0xfaff; ++i) {
    table[i] = 2;
  }
  for (let i = 0xfe10; i <= 0xfe19; ++i) {
    table[i] = 2;
  }
  for (let i = 0xfe30; i <= 0xfe6f; ++i) {
    table[i] = 2;
  }
  for (let i = 0xff00; i <= 0xff60; ++i) {
    table[i] = 2;
  }
  for (let i = 0xffe0; i <= 0xffe6; ++i) {
    table[i] = 2;
  }
}
iTableProgrammed();

This lowers the creation cost down to 1.5 ms for the whole table and can be done during lib initialization. This coupled with the slightly more expensive 64k table and we have really nice speedup at initialization and during runtime. 😸

@Tyriar Mission accomplished?

Edit: With .subarray instead of the loops the above table creation runs in 0.2 ms, lol.