Description
Currently, all class fields are mutable at the Wasm level: the genFunctionID.newDefault
function creates a class instance with fields initialized to their zero values, and then the constructor initializes them (by Assign
at the IR level).
Fields being mutable means that accessing a field is an impure operation at the Wasm level, causing us to miss some optimization opportunities.
In fact, making itables immutable has improved performance in some benchmarks, as seen in #5038.
We may want to "merge" the genFunctionID.newDefault
function with the constructor: The fused constructor would:
- Set up the vtable (virtual method table) and itable (interface table)
- Initialize the fields
- Instead of filling fields with zero values, the
wasmemitter
would transform the RHS of fieldAssign
nodes to become the initial values of the fields.
- Instead of filling fields with zero values, the
- Execute
struct.new
for instantiation (asnewDefault
currently does)
Alternatively, should we modify the IR structure somehow? It appears that these Assign
operations in the constructor originate from Scala compiler, not from GenJSCode
🤷. In this case, making changes at the IR level seems introduce more complications than it solves.