I recently coded this FNV-1a hash function in Lua. Are there any apparent performance improvements that could be implemented?
local bit = require(game.ServerStorage["luabit-0.4"].bit)
-- External library
local hex = require(game.ServerStorage["luabit-0.4"].hex)
-- External library
local FNV_Primes = {
[32]=2^24+2^8+0x93,[64]=2^40+2^8+0xb3,[128]=2^88+2^8+0x3b,[256]=2^168+2^8+0x63,[512]=2^344+2^8+0x57,[1024]=2^680+2^8+0x8d
}
local FNV_Offset_Basis = {
[32]=2166136261,[64]=14695981039346656037,[128]=144066263297769815596495629667062367629,[256]=100029257958052580907070968620625704837092796014241193945225284501741471925557,[512]=9659303129496669498009435400716310466090418745672637896108374329434462657994582932197716438449813051892206539805784495328239340083876191928701583869517785,[1024]=14197795064947621068722070641403218320880622795441933960878474914617582723252296732303717722150864096521202355549365628174669108571814760471015076148029755969804077320157692458563003215304957150157403644460363550505412711285966361610267868082893823963790439336411086884584107735010676915
}
local function FNV_Hash(bitTable,bitLen)
-- bitTable = data to be hashed.
local curPrime = FNV_Primes[bitLen] or FNV_Primes[32]
-- Sets the FNV_Prime based on bitLen (i.e., for a 32-bit hash, bitLen = 32)
local hash = FNV_Offset_Basis[bitLen] or FNV_Offset_Basis[32]
local bitTableS = table.concat(bitTable)
-- String of the concatenated bitTable
for i = 1,math.ceil(#bitTable/8) do
-- for each octet_of_data to be hashed
hash = bit.bxor(hash,tonumber(bitTableS:sub(i*8-7,i*8),2))
-- hash = hash XOR octet_of_data
hash = hash*curPrime
--hash = hash × FNV_prime
end
return hex.to_hex(hash)
end
518and1028indexes are typos. \$\endgroup\$