如何理解 WebAssembly 中的 "align" 属性?
How to understand the "align" attribute in WebAssembly?
为什么 "load" 和 "store" 运算符需要 "align" 属性,它如何与内存对齐一起使用?
顺便说一句,为什么我们需要这个运算符,因为底层系统会自动为我们进行内存对齐?
对齐的使用详见规范:
The alignment memarg.
in load and store instructions does not affect the semantics. It is an indication that the offset ea
at which the memory is accessed is intended to satisfy the property ea mod 2^memarg.=0
. A WebAssembly implementation can use this hint to optimize for the intended use. Unaligned access violating that property is still allowed and must succeed regardless of the annotation. However, it may be substantially slower on some hardware.
对齐没有其他语义效果;未对齐和未对齐的加载和存储仍然表现正常,这只是性能优化。
这是对 VM 的承诺,(baseAddr + memarg.offset) mod 2^memarg.align == 0
其中 baseAddr
是参数形式堆栈。
换句话说,我们实际上将我们的内存分成大小为 2^memarg.align
字节的块,并向 VM 保证我们的实际地址 (baseAddr + memarg.offset
) 将位于任何块的开头,而不是在中间.
因为 memarg.align
的最大值是 3 块的大小(以字节为单位)可以是 {1, 2, 4, 8} (1 = 2^0, .., 8 = 2^3).
另外,你可以找到一个很好的详细解释here。
为什么 "load" 和 "store" 运算符需要 "align" 属性,它如何与内存对齐一起使用?
顺便说一句,为什么我们需要这个运算符,因为底层系统会自动为我们进行内存对齐?
对齐的使用详见规范:
The alignment
memarg.
in load and store instructions does not affect the semantics. It is an indication that the offsetea
at which the memory is accessed is intended to satisfy the propertyea mod 2^memarg.=0
. A WebAssembly implementation can use this hint to optimize for the intended use. Unaligned access violating that property is still allowed and must succeed regardless of the annotation. However, it may be substantially slower on some hardware.
对齐没有其他语义效果;未对齐和未对齐的加载和存储仍然表现正常,这只是性能优化。
这是对 VM 的承诺,(baseAddr + memarg.offset) mod 2^memarg.align == 0
其中 baseAddr
是参数形式堆栈。
换句话说,我们实际上将我们的内存分成大小为 2^memarg.align
字节的块,并向 VM 保证我们的实际地址 (baseAddr + memarg.offset
) 将位于任何块的开头,而不是在中间.
因为 memarg.align
的最大值是 3 块的大小(以字节为单位)可以是 {1, 2, 4, 8} (1 = 2^0, .., 8 = 2^3).
另外,你可以找到一个很好的详细解释here。