You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
Binary encoding of value type codes with heap type indices #337
We haven't chosen the final binary encoding for value type codes introduced in this proposal.
One issue is that decoding value types is a very hot operation in in-place interpretation, particularly the declaration of local variables as well as block types (though block types are just decoded and ignored). Until this proposal, all value types are decodable as a single byte (or single LEB--we've been inconsistent on that so far). With this proposal, some value type codes have a heaptype immediate. Thus an in-place interpreter can no longer simply decode (or skip) a single LEB.
I've resorted to doing a table lookup to see if the value type code indicates that a heaptype index should follow (ref or ref null). But with one tweak to the assignment of type codes, it's possible to turn that into a bit-test.
It turns out that we have encoding space to make the 6th bit the determinant as to whether a heaptype immediate follows. We just need to make ref and ref null < -32. That makes the 6th bit 0 in case of ref, ref null; empty block still needs a separate check. (Perhaps it is still possible to rearrange so that only one test is necessary).
That constrains the "short encoding" space to 32 entries if we want to preserve the bit test property in future additions.
Don't forget the codes for structured types (func, struct, array), which already occupy the range -32..-34. They are supposed to share a single code space with value types, since we may have a future need to allow them in the same place, e.g., consider nested structs.
Also, it's not just the number of code space entries to consider. This optimisation is based on the implicit assumption that no other immediates will ever be added to value types. I highly doubt that is a valid assumption, it is rather easy to predict new immediates like attribute bytes etc. And we don't have enough space in a byte to afford a bit each time, so this scheme would not scale.
Don't forget the codes for structured types (func, struct, array), which already occupy the range -32..-34. They are supposed to share a single code space with value types, since we may have a future need to allow them in the same place, e.g., consider nested structs.
I think we should probably escape those ones in particular in nested structs, since i don't think we them to appear other places that a value type can occur, like a local. Fields and array elements are already classified as storage types, so I don't see an issue; an interpreter doesn't look at those. Storage types have a mutability byte, which can serve as a flags byte in the future.
And we don't have enough space in a byte to afford a bit each time, so this scheme would not scale.
The bit only has to distinguish between one-byte value types and multi-byte value types.
I think we should probably escape those ones in particular in nested structs
Yes, that would work.
The bit only has to distinguish between one-byte value types and multi-byte value types.
I don't feel strongly, but we haven't done any such nano-optimisations of opcode bit patterns elsewhere in Wasm, so it feels a bit over the top. What do other folks think?
I don't feel strongly either way. I would be fine optimizing the numbering now, but would be unhappy if in the future we had to consider whether potential extensions would be compatible with the numbering optimization scheme we end up with.
@tlively That's fair. I agree it's a nano-optimization. At this juncture it doesn't cost us anything, and I'd be fine to give up on it the minute it makes a design problem harder in the future.
Coming back to this, introducing overlap between different type constructor spaces has a potential price that is easily higher than what's to be gained from this nano-optimisation, the possibility of escaping notwithstanding. I think we should refrain from doing that if we can help it.
That said, I think we can enable the same optimisation without relying on bit fiddling: if we sort the nullary constructors first and then the unary ones (ignoring the special void case), then you can simply compare with the lowest non-nullary code for the same effect.
So I would propose e.g. (the * ones we cannot change):
Thought about this, and this is ordering is fine. AFAICT, it will require two comparisons (and will always) because it requires an upper-bit check for an extended LEB and then a comparison (unsigned >= if 1-byte) against the nullary range.
Activity
rossberg commentedon Nov 9, 2022
Don't forget the codes for structured types (func, struct, array), which already occupy the range -32..-34. They are supposed to share a single code space with value types, since we may have a future need to allow them in the same place, e.g., consider nested structs.
Also, it's not just the number of code space entries to consider. This optimisation is based on the implicit assumption that no other immediates will ever be added to value types. I highly doubt that is a valid assumption, it is rather easy to predict new immediates like attribute bytes etc. And we don't have enough space in a byte to afford a bit each time, so this scheme would not scale.
titzer commentedon Nov 9, 2022
I think we should probably escape those ones in particular in nested structs, since i don't think we them to appear other places that a value type can occur, like a local. Fields and array elements are already classified as storage types, so I don't see an issue; an interpreter doesn't look at those. Storage types have a mutability byte, which can serve as a flags byte in the future.
The bit only has to distinguish between one-byte value types and multi-byte value types.
rossberg commentedon Nov 22, 2022
Yes, that would work.
I don't feel strongly, but we haven't done any such nano-optimisations of opcode bit patterns elsewhere in Wasm, so it feels a bit over the top. What do other folks think?
tlively commentedon Nov 22, 2022
I don't feel strongly either way. I would be fine optimizing the numbering now, but would be unhappy if in the future we had to consider whether potential extensions would be compatible with the numbering optimization scheme we end up with.
titzer commentedon Nov 22, 2022
@tlively That's fair. I agree it's a nano-optimization. At this juncture it doesn't cost us anything, and I'd be fine to give up on it the minute it makes a design problem harder in the future.
rossberg commentedon May 2, 2023
Coming back to this, introducing overlap between different type constructor spaces has a potential price that is easily higher than what's to be gained from this nano-optimisation, the possibility of escaping notwithstanding. I think we should refrain from doing that if we can help it.
That said, I think we can enable the same optimisation without relying on bit fiddling: if we sort the nullary constructors first and then the unary ones (ignoring the special void case), then you can simply compare with the lowest non-nullary code for the same effect.
So I would propose e.g. (the * ones we cannot change):
This way, you just compare opcode < -28 to test if it has immediate(s). FUNC, STRUCT, ARRAY actually fit nicely in this pattern.
(We could also move some of the reference type constructors before FUNCREF if we want to make more space for non-nullaries.)
titzer commentedon May 2, 2023
Ok, I have to think about if that comparison works with a 1-byte LEB (i.e. the upper bit will be 0 and not compare as signed).
titzer commentedon May 10, 2023
Thought about this, and this is ordering is fine. AFAICT, it will require two comparisons (and will always) because it requires an upper-bit check for an extended LEB and then a comparison (unsigned >= if 1-byte) against the nullary range.
tlively commentedon Jun 27, 2023
Is there anything left to decide here? What needs to be done to close this?
rossberg commentedon Jun 27, 2023
This would be closed by #372. But since @titzer said it's fine, I think we can close it now.