tanishiking / scala-wasm

Experimental WasmGC backend for Scala.js | merging into the upstream Scala.js repo https://github.com/scala-js/scala-js/pull/4988
41 stars 3 forks source link

Separate IDs of Wasm things from their names. #134

Closed sjrd closed 6 months ago

sjrd commented 6 months ago

Previously, we eagerly generated string names for every Wasm thing, and used that name both as their identity and as their debug name.

Now, we clearly separate those concepts: identities are instances of abstract traits such as FunctionID, while debug names are OriginalNames. Original names are only stored at the definition site, while IDs are used to link definition and use sites.

IDs only need to have meaningful equals and hashCode. This allows to define them as case classes that directly embed ClassNames and MethodNames (among others), without having to decode them or serialize them.

Moreover, since OriginalNames are internally represented as UTF-8 strings, we also avoid the cost of decoding and reencoding them when writing binary .wasm files. The text writer gets more work to do, but it is not on the performance-sensitive path, so this is not really an issue.

For struct field IDs, this change also implies merging field names and field indices. Since references to fields can now use FieldIDs as well, it removes a bit of bookkeeping at the ClassInfo level. Both instance field indices and method table entry indices are late-bound in the binary emitter. We do not have to eagerly compute them.