A while ago I started a project to build a streamlined GameAnalytics module but due to outside work and the release of the official module I ended up putting the project on the back burner.
I did however finish optimizing the SHA module and now with the release of bit32 I decided to release the module along with the comparison between the old module and using the bit32 functions.
I am sure someone might find it usefull or even make additional improvements.
Benchmark results (disable Lua debugger in studo setting)
The difference in performance, or lack thereof, between a custom Lua implementation of bit32 and the new one built into Lua is really surprising to me.
Or am I missing something… is the built-in bit32 just for convenience, and not on the C side?
The bit module uses caching of values but that was my same reaction. It might be usefull to create a cache for the bit operations with a weak table. I would need to do some additional testing to find this out.
Im really not sure on how the new bit32 lib is implemented o.o
I’m not sure why your implementation is so slow, though looking at the code I suspect it’s probably because it’s quite over-engineered. Given you’re porting it from a Lockbox, that’s not surprising though (as you yourself said, it’s designed to be easy to read and not be fast).
To give you an idea of how slow it is comparatively, I ran a few iterations of my implementation along side yours using the same tests:
What I meant was that yours should be seeing a much more noticeable improvement by using the native bit32 lib. As @Dekkonot stated, it seems over-engineered and that’s likely what’s taking away from what you gain by using bit32.
I found out that LuaUsers implemented a SHA256 algorithm using the bit32 library in pure lua (adapted from the Wikipedia page for SHA2). This ends up being much faster than most of those tested and can literally just be copy pasted into Roblox. http://lua-users.org/wiki/SecureHashAlgorithm
@Hexcede@Dekkonot@Muoshuu Yep it really is. It uses tables to store the data before each round so it can support both strings and byte inputs. It is also not very smart when dealing with chunks as it would still place them in the table just to move them over to the digest.
I am glad to see that there are better solutions available in Lua. Please keep posting them