I’ve always been curious if pre-hashing actually makes a difference, so I made a test project where a script sends x number of messages per update() to a script that either pre-hashes message ids, or doesn’t.
No hash script:
function on_message(self, message_id, message, sender)
if message_id == hash("one") then
-- print("one")
elseif message_id == hash("two") then
-- print("two")
elseif message_id == hash("three") then
-- print("three")
elseif message_id == hash("four") then
-- print("four")
elseif message_id == hash("five") then
-- print("five")
end
end
Pre-hashed script:
local ONE = hash("one")
local TWO = hash("two")
local THREE = hash("three")
local FOUR = hash("four")
local FIVE = hash("five")
function on_message(self, message_id, message, sender)
if message_id == ONE then
-- print("one")
elseif message_id == TWO then
-- print("two")
elseif message_id == THREE then
-- print("three")
elseif message_id == FOUR then
-- print("four")
elseif message_id == FIVE then
-- print("five")
end
end
Sending 1000 messages per update() meant the no hash script took, on my machine, 3-5 ms to process:
When sending the same number of messages to the pre-hashed script, it was more like 1 ms:
Yes, pre-hashing matters!
Because execution will stop when the correct message id is hit, sending the message “one” makes a huge difference vs sending the message “five” (default in this test). This further confirms that pre-hashing matters, as the more hashing you have to do, the longer it takes.
Project if you want to check it out for yourself:
(see follow up post)