Skip to content

Eager bslib_coalesce/bslib_ternary is slower than the lazy form; transpile ?? and ternary to inline if statements #1985

Open

Description

@TwitchBronBron

#1984 fixes the crash from #1982 by short-circuiting indexed and xml attribute gets. While investigating, we benchmarked the two transpile targets for ?? (and non-assignment ternaries) on a Roku device, and the results suggest the eager bslib_coalesce/bslib_ternary path isn't worth keeping in its current form.

Background

The transpiler currently picks between:

  • eager: bslib_coalesce(a, b) / bslib_ternary(c, a, b), used when neither operand contains a call, callfunc, or dotted/indexed/xml get. Both operands are always evaluated.
  • lazy: a scope-capturing IIFE that only evaluates the branch that's needed.

The eager path exists because it was assumed to be cheaper. It mostly isn't.

Findings

Benchmarks: rokucommunity/bsbench#20 (CoalesceEagerVsLazySkipped, CoalesceEagerVsLazyUsed).

Each cell is how much faster the lazy form is than the eager form on that thread. Bold means lazy was slower.

When the alternate isn't needed (value ?? <alternate> where value is set, the common case):

alternate main thread render thread task thread
variable 10% slower 28% faster 28% faster
string literal 3% slower 31% faster 31% faster
array literal 73% faster 75% faster 75% faster
AA literal 82% faster 83% faster 83% faster
regex literal 91% faster 90% faster 91% faster
template interpolation 89% faster 90% faster 90% faster
indexed get (cfg["key"]) 26% faster 44% faster 43% faster
dotted get (cfg.key) 18% faster 39% faster 39% faster
xml attribute get (xml@id) 28% faster 46% faster 46% faster
binary (count + 1) 5% faster 34% faster 34% faster
unary (-count) 2% slower 30% faster 30% faster

When the alternate is needed (value is invalid, so both forms evaluate it):

alternate main thread render thread task thread
variable 10% slower 28% faster 28% faster
string literal 3% slower 32% faster 32% faster
array literal same 12% faster 14% faster
AA literal same 16% faster 16% faster
regex literal same 9% faster 9% faster
template interpolation 1% slower 7% faster 7% faster
indexed get (cfg["key"]) 10% slower 29% faster 29% faster
dotted get (cfg.key) 10% slower 31% faster 31% faster
xml attribute get (xml@id) 5% slower 30% faster 30% faster
binary (count + 1) 9% slower 38% faster 38% faster
unary (-count) 10% slower 40% faster 40% faster
Raw numbers (µs per operation, main / render / task)

Alternate not needed:

alternate eager lazy
variable 0.48 / 0.77 / 0.78 0.53 / 0.56 / 0.56
string literal 0.48 / 0.76 / 0.76 0.50 / 0.53 / 0.52
array literal 1.81 / 2.08 / 2.08 0.50 / 0.52 / 0.53
AA literal 2.78 / 3.13 / 3.12 0.50 / 0.52 / 0.52
regex literal 5.39 / 5.30 / 5.91 0.50 / 0.52 / 0.53
template interpolation 5.03 / 5.70 / 5.71 0.53 / 0.55 / 0.55
indexed get 0.75 / 1.04 / 1.04 0.56 / 0.59 / 0.59
dotted get 0.68 / 0.96 / 0.97 0.56 / 0.59 / 0.59
xml attribute get 0.79 / 1.08 / 1.08 0.57 / 0.58 / 0.59
binary 0.56 / 0.85 / 0.85 0.53 / 0.56 / 0.56
unary 0.52 / 0.80 / 0.80 0.53 / 0.56 / 0.56

Alternate needed:

alternate eager lazy
variable 0.48 / 0.77 / 0.77 0.53 / 0.56 / 0.56
string literal 0.48 / 0.76 / 0.76 0.49 / 0.52 / 0.52
array literal 1.80 / 2.09 / 2.12 1.80 / 1.83 / 1.82
AA literal 2.06 / 2.46 / 2.44 2.05 / 2.07 / 2.05
regex literal 5.27 / 5.31 / 5.94 5.27 / 4.81 / 5.40
template interpolation 5.08 / 5.93 / 5.90 5.12 / 5.52 / 5.50
indexed get 0.76 / 1.19 / 1.19 0.83 / 0.84 / 0.84
dotted get 0.69 / 1.12 / 1.12 0.76 / 0.77 / 0.77
xml attribute get 0.84 / 1.27 / 1.27 0.88 / 0.89 / 0.89
binary 0.55 / 1.00 / 1.00 0.60 / 0.62 / 0.62
unary 0.52 / 0.96 / 0.96 0.56 / 0.58 / 0.58

Takeaways:

  • On the render and task threads (where nearly all SceneGraph code runs), lazy is faster for every operand kind, including plain variables (~30%). Calling a named function like bslib_coalesce appears to cost more there than invoking an anonymous function.
  • Eager only wins on the main thread, and only by up to 10% (about 0.05µs).
  • Anything that builds a value (array, AA, regex, interpolated template) wastes 73–91% of its time when evaluated eagerly and not needed.
  • These benchmarks use an AA for the gets. On a node owned by another thread, an eager get also forces a rendezvous, which costs far more than any of the above.

Code size

Roku packages (including images) must be under 4 MB zipped, so output size matters as much as speed. To measure it, we generated 10,000 unique uses of each form: random identifiers, random operand shapes (dotted chains, indexed gets, calls, xml attributes), and random indentation, so the zip couldn't just compress identical copies.

Two zipped numbers are shown:

  • dense: all 10,000 uses in one file, so the zip can reuse the boilerplate from nearby uses.
  • worst case: each use zipped on its own. This is what you get when uses are spread across files or are more than 32 KB apart, which is how they appear in a real app.

Bytes per use:

form raw zipped (dense) zipped (worst case)
x = bslib_coalesce(a, b) 86 41 79
?? IIFE 378 57 150
?? if statement 156 50 93
x = bslib_ternary(c, a, b) 127 66 115
ternary IIFE 371 82 170
ternary if statement 198 71 123

Compared with today's bslib_* output, each use costs this much more zipped (worst case):

  • if statement: +14 B for ??, +8 B for ternary. For 10,000 uses, that's about 140 KB.
  • IIFE: +71 B for ??, +55 B for ternary. For 10,000 uses, that's about 700 KB, or 17% of the 4 MB budget.

Random identifiers compress worse than real code, so real numbers should land somewhere between the two zipped columns.

What we already do

When the whole right side of an assignment is a ternary or ??, we already turn it into a plain if statement instead of calling a function:

' x = a.b ?? c
x = a.b
if x = invalid then
    x = c
end if
x = c ? a : b x = a ?? b anywhere else, e.g. print foo(a ?? b)
master if statement IIFE or bslib_coalesce IIFE or bslib_*
v1 if statement if statement, but only when an operand has a call or a dotted/indexed/xml get; otherwise bslib_coalesce IIFE or bslib_*

Everywhere else still uses the IIFE or the bslib_* functions measured above.

Suggested solutions

  1. Use if statements everywhere we can (preferred). Extend the existing assignment rewrite to:
    • ?? on master (v1 already does this).
    • Simple operands too. v1 currently sends x = a ?? b to bslib_coalesce, which the numbers above show is slower on render and task threads.
    • Ternary and ?? inside other expressions, by computing the value into a temp variable in the statement just before:
      ' print foo(a ?? b.c)
      __bsCoalesce0 = a
      if __bsCoalesce0 = invalid then
          __bsCoalesce0 = b.c
      end if
      print foo(__bsCoalesce0)
      Caveats: this can change the order things run in when sibling expressions have side effects (foo(bar(), a ?? baz()) would run baz() before bar()). Some positions can't be moved at all, such as while conditions, else if conditions, and the right side of and/or. Those would keep using the IIFE.
      This is the best option on every measure: it's the fastest, it can't crash or rendezvous on a skipped branch, and it's only about 14 B per use larger than bslib_* when zipped.
  2. Always use the IIFE wherever we don't use an if statement. Drop bslib_coalesce/bslib_ternary entirely. It's faster on render and task threads and can't crash or rendezvous, but it costs about 71 B more per use zipped. Across a large app that adds up to a real share of the 4 MB budget, so we should only use the IIFE where an if statement isn't possible.
  3. Keep bslib_* only for plain variables and simple literals (strings, numbers, booleans, invalid). This is the smallest change, but it's still slower on render and task threads.

Recommendation: use if statements wherever possible (option 1). Where they aren't possible, use the IIFE when an operand could crash, rendezvous, or build an object, and bslib_* for plain variables and simple literals, where it's slightly slower but much smaller (option 3).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions