Page 1 of 1
Fast hash in lua
Posted: Tue May 05, 2026 2:43 pm
by wanne
Is there a way to get a fast hash of something? SE has it's own implementation of CRC32. But doing something in a lua5.2 interpreter feels really wrong.
And there has to be a native hash function for the tables. I just do not see a way to access it.
Re: Fast hash in lua
Posted: Tue May 05, 2026 4:42 pm
by eugenekay
Lua's standard library does not include common Hash functions.
Bringing your own seems to be the standard practice, rather than patching-in a C runtime extension to the engine.
Re: Fast hash in lua
Posted: Tue May 05, 2026 5:22 pm
by hgschmie
What are you trying to hash? With entity unit numbers being unique and tables taking string keys, I rarely found a reason to hash anything. Curious what you are trying to do.
Re: Fast hash in lua
Posted: Tue May 05, 2026 8:38 pm
by wanne
They are either horribly slow or require luajit 5.3. Even so bit32 lib speeds up things a lot.
For comparison this is a variant that supports 5.3:
https://github.com/Egor-Skriptunoff/pur ... /README.md
Code: Select all
time sha256sum ~/factorio/bin/x64/factorio
4ca01d9e7f92a390f706c355f1dfaf68b19ef5c4e0e3a4b43f5e547ed20d71bd /home/steam/Programme/factorio-2-aeng/bin/x64/factorio
real 0m0.124s
time lua5.2 sha2eugenekay.lua ~/factorio/bin/x64/factorio
4ca01d9e7f92a390f706c355f1dfaf68b19ef5c4e0e3a4b43f5e547ed20d71bd
real 2m28.467s
time lua5.2 sha2.lua ~/factorio/bin/x64/factorio
4ca01d9e7f92a390f706c355f1dfaf68b19ef5c4e0e3a4b43f5e547ed20d71bd
real 1m55.778s
time lua5.4 sha2.lua ~/factorio/bin/x64/factorio
4ca01d9e7f92a390f706c355f1dfaf68b19ef5c4e0e3a4b43f5e547ed20d71bd
real 0m25.166s
time lua5.4 luac.out ~/factorio/bin/x64/factorio
4ca01d9e7f92a390f706c355f1dfaf68b19ef5c4e0e3a4b43f5e547ed20d71bd
real 0m26.609s
time luajit sha2.lua ~/factorio/bin/x64/factorio
4ca01d9e7f92a390f706c355f1dfaf68b19ef5c4e0e3a4b43f5e547ed20d71bd
real 0m0.826s
To be honest I do not need the performance. And I certainly do not need SHA2. djb2 is more than enough. And I do not have problems between the factor 7 for luajit vs. C. But the factor 1000 for 5.2 hurts my feelings.
Re: Fast hash in lua
Posted: Tue May 05, 2026 8:47 pm
by wanne
hgschmie wrote: Tue May 05, 2026 5:22 pmWhat are you trying to hash? With entity unit numbers being unique and tables taking string keys, I rarely found a reason to hash anything. Curious what you are trying to do.
I am generating categories and have to find them again later in the data.raw. So yes. I am using the hash as an string. base64 or just "" .. does the trick.
In theory I could use random names and keep track of them in a lua table. But a stable hashing method seems nicer and is transferable between mods and stages.
Re: Fast hash in lua
Posted: Wed May 06, 2026 4:41 am
by hgschmie
If you need to move data between prototype and runtime stage, consider using ModData (
https://lua-api.factorio.com/latest/pro ... dData.html). If you are just managing data during the prototype stage, the performance does not really matter, concatenating with e.g. ':' or '|' and leaving the hashing to the actual table (which will hash the string as a key) should be good enough.
Re: Fast hash in lua
Posted: Wed May 06, 2026 6:18 am
by wanne
hgschmie wrote: Wed May 06, 2026 4:41 amIf you are just managing data during the prototype stage, the performance does not really matter,
Like I said. In reality it is not a problem. I just do not like it.
hgschmie wrote: Wed May 06, 2026 4:41 amconcatenating with e.g. ':' or '|' and leaving the hashing to the actual table (which will hash the string as a key) should be good enough.
I do that but categories are shown in game even so only on tooltips. And while random numbers are wired, big blobs of lua/json code would be an other kind of strange. It is also limited to 200 characters.
Like I said: I could just use a random name and keep track of it. I even do not need to access it on runtime stage. Only between mods and data-update/data-final-fixes phase. But it is so much less elegant and I really do not want to search into how randomness will affect multiplayer and reloads with other mods.