8000 Wasm: Make class fields immutable at the Wasm level · Issue #5039 · scala-js/scala-js · GitHub
[go: up one dir, main page]

Skip to content
Wasm: Make class fields immutable at the Wasm level #5039
Open
@tanishiking

Description

@tanishiking

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).

val fields = classInfo.allFieldDefs.map { field =>
watpe.StructField(
genFieldID.forClassInstanceField(field.name.name),
makeDebugName(ns.InstanceField, field.name.name),
transformFieldType(field.ftpe),
isMutable = true // initialized by the constructors, so always mutable at the Wasm 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 field Assign nodes to become the initial values of the fields.
  • Execute struct.new for instantiation (as newDefault 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    optimizationOptimization only. Does not affect semantics or correctness.wasmApplies to the WebAssembly backend only

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0