Skip to content

let vs var generates a lot more instructions #800

Open
@Cellule

Description

@Cellule

Sorry in advance I haven't been able to get a simpler repro case at this time.
My test case is the following

const n = 500;
function getTest(name) {
  var fn;
  eval(`fn = function memcopy_${name}(a, b, start, end) {for (let i = start; i < end; i++) { b[i] = a[i]; }}`);
  return fn
}
var foo = getTest("Int8")
var src = new Int8Array(n);
src.fill(1);
var dst = new Int8Array(n);
foo(src, dst, 0, 250);
foo(src, dst, 250, n);

This code generates the following IR

Function Entry
    s7.var          =  NewBlockScope                                          #0000 
    s15(s7->i)<?,,--,s?,s?>.var = InitLetFld  0xXXXXXXXX (null)[PrimitiveOrObject].var #0002 
Every uses of `i`
    s19.var         =  LdSlotArr      s17(s7[2]).var                          #0017 
    s8.var          =  LdSlot         s20(s19[0])<?,,--,s?,s?>.var            #0017 

This IR makes it hard to make proper optimizations (in this case Array optimizations).

If we change the let keyword for a var, i becomes a regular variable.
Is it possible to make let variables behave exactly the same as a regular var if there is no redefinition inside the function ?
Should this be done at Bytecode level, IR Builder or Globopt ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions