Search code examples
webassembly

Why is there no byte data type in WebAssembly?


Given how useful byte data types are for stings and binary data why does WebAssembly not have them? Since it would require bit operations to access individual bytes, is there an overhead to using byte data over storing each byte in its own 32 bit int?


Solution

  • WebAssembly locals & globals are strongly mapped to today’s hardware registers. That’s why they aren’t byte-sized.

    You can still work with bytes using i32.load8_u, v128.load8x8_u and so on. These instructions more closely match what a real CPU does. Introducing a "byte type" would add syntactic sugar & complexity for no real benefit, because CPUs always use 32/64 bit registers in the end.

    In addition, byte-sized locals would completely muck up the alignment of the stack after a push.

    To the second question: there is no difference. i32.load is the same speed as i32.load8_u on modern CPUs. There aren't any explicit “bit operations" going on, it's a single instruction (e.g. mov al, [mem]). The byte is moved into the lower 8 bits of the register, that’s all.

    In fact, it's possible that an i32.load can be slower than an i32.load8_u if its address isn't aligned properly. (Bytes are always aligned because they're not big enough to cross an alignment boundary).