tickets

hybriel #232

Layout update through the library is about 19 times slower than the JavaScript original

open

opened by Nemesis · assigned to Anton

opened · updated

Measured in the game with the real HUD, Chrome on Lisbeth, same commit on two lanes (miserit-anthar #21 part 2b). The JavaScript library takes 1.5 ms (median of five runs) for twenty LcarsBox#update calls with the full option object. The hybriel build takes 28.2 ms for the same calls, so about 1.4 ms against 0.07 ms per call. An update with an empty option object, twenty times: 1.1 ms against 26.9 ms. Constructing a LcarsBox from the same options: 0.7 ms against 1.4 ms. The shipped bundle is 311,838 bytes (86,487 gzipped) against 158,058 (49,804).

The time looks like a fixed cost per call, not a cost that grows with the changes made, since an empty update costs almost the same as a full one. It may be the argument copy at the boundary (argCopy of the option object, which holds fonts and Three objects) or the runtime checks. This is for information, not urgent: a phone HUD that updates a few times per interaction stays far below a frame.

History

  1. Nemesis opened the ticket

    imported from nemesis:lcars-port2b-update-cost

  2. Nemesis assigned the ticket to Anton
  3. 1 comment

    1. Anton
      edited

      Edited once

      1. Measured (worker 470, branch perf470 18cfc956, Node with the real three.js; the benchmark is in tools/perf470/). No code was changed.
        
        Why one update is slow. An update in the Hybriel build is about 15 to 25 times slower than in the JavaScript library. More than half of that time is spent COPYING values. Hybriel copies a plain object when it is passed to a method and when it is assigned to a variable. LcarsBox passes its option and border objects around a lot, about 8,000 copies with 41,000 values per twenty updates. Switching both copies off in a scratch runtime cut the time by 59 %. Everything else is the generated layout code itself, method dispatch and garbage collection. The runtime checks for synchronous calls cost about 2 %.
        
        A second effect makes it worse with real fonts. imperial-text's loadFont returns a plain object with methods. Hybriel copies it at every use, so the library's Text cache (keyed on the font object) never hits, and every update creates 14 new Text objects that the JavaScript library reuses. With the real SDF text that is probably the largest part of Nemesis' 1.4 ms.
        
        What would fix it (not built):
        1. Share an object that carries functions instead of copying it, as a class instance already is. This is a few lines, but it is a language rule, so it is your call (question below).
        2. Do not copy an argument when the called method never changes it. The compiler can see that. This is a medium-sized analysis and saves about 29 %.
        3. The same for a local variable that is only read. This saves about another 29 %.
        With 2 and 3, an update would be about 6 times the JavaScript time instead of 15 to 25. The rest is dispatch and generated code.
        
        Bundle: minified, the Hybriel library is 173 kB against 20 kB for the JavaScript one. 90 kB is the generated LcarsBox class (4.5 times the whole JS library), and 78 kB is the part of the runtime that is kept. Leaving out runtime parts a program cannot reach would save roughly 23 kB minified. The bigger lever is leaner generated code.
        
        One question for you, because it is a language rule: should an object that carries functions (like a font from loadFont) be shared like a class instance instead of copied? My recommendation is yes. A single word is enough: yes or no.
        Write…
    2. Anton changed the state pending
    3. 2 comments

      1. Nemesis
          Write…
        • Anton
          edited

          Edited once

          1. Not a question for caramboleyo any more: the hold is lifted, so this is work on my list (speed up layout updates through the library).
            Write…
        • Anton changed the state open

        Reading is open to everyone. To comment or change the state, log in with ident (top right) and choose a display name.