Bit 32 vs buffers: which one should be faster?

I am currently in doubt. I want to optimize actors messaging for parallel luau, and for making messaging faster I have been using buffers. It works perfect, but now its taking more time to convert the data to bufffers than I would like it to. I have heard of the bit32 library, and i have the following question:

Which one would be more performant based on your experience: bit packing with bit32 or buffers?

1 Like

Performance Comparison:

  • Bit32 (Bit Packing): When using bit32, the operations themselves are generally very fast, as bitwise manipulation is a low-level operation. However, the process of packing and unpacking values can introduce overhead if you’re working with a lot of data or frequently need to pack/unpack.

  • Buffers: While buffers might be slightly slower to convert due to serialization/deserialization (especially with large amounts of data), they tend to be easier to manage and are better optimized for general-purpose use cases like networking or actor messaging. In scenarios where you’re dealing with diverse data types (strings, floats, etc.), buffers can often be more efficient overall, despite the higher conversion cost.

Conclusion:

If you need maximum performance for specific types of data (like small integers or booleans), bit32 might be worth exploring further. However, for more complex scenarios or general messaging, buffers could remain the better choice.

1 Like

The easiest way to get an answer is to do the benchmarking yourself, but usually, bit-level operations are slower than byte-level ones. I’d stick to buffers.

If you’re planning on using some sort of a table-based bit buffer (like rstk’s), then it will definitely be slower than using a buffer, especially with native code generation enabled.

I think reducing the number of write calls you make could net some performance gains. One writeu32 is faster than 4 writeu8 calls, for example. This may mean that using the new buffer.readbits or buffer.writebits functions may not be the best idea (they also do bounds checks); the faster alternative would be to bit pack and use a singular write at the end.