wow-rp-addons / LibMSP

The “Mary Sue Protocol” (“MSP” for short) is a simple challenge/response protocol for RP UI add-ons within WoW to communicate with each other and publish text information to other clients (such as RP character names and descriptions).
Creative Commons Zero v1.0 Universal
2 stars 1 forks source link

Improve CRC32 performance #33

Closed Meorawr closed 3 years ago

Meorawr commented 3 years ago

This changeset improves the performance of the CRC32 hash function by approximately ~10% (increasing with larger payload sizes) by unrolling loop to reduce the number of string.byte calls to deal with bytes in blocks of 8.

Additionally the tohex function has been simplified to use string.format with bit.arshift, which works around the overflow issue it was trying to avoid by ensuring that inputs to string.format are signed when above 2^31-1.

Unfortunately I lost the specific test data I was using, but the before/after should show below quite well. The test strings used for the benchmark were strings of mixed length (0, 10, 25, 50, 200, and 1000 in-order). The tests were run 10 times without and with the changes:

image

With changes applied:

image

While the zero-byte string case gets worse, I think it's likely that there aren't going to be many zero byte strings being hashed. The gains from everything larger than that should outweigh the hits taken by very small strings.