Passing enums efficiently (string atoms) #520
Replies: 2 comments 9 replies
-
The atoms give you an opportunity to only compute the association between string and enum once though. Exposing the hash still requires a hash based lookup, whereas with atoms you can simply index into an enum vector using the atom index. I'd recommend using the atoms - the extra overhead is generally negligible in our experience. If it needs to be mitigated then for example atom callback can filter strings based on their length as min/max enum length is known apriori. fwiw in Roblox we represent enums as userdata that's part of a global table, e.g. Enum.Foo.Bar; this is where imports are super useful because the cost of the lookup evaporates at runtime. |
Beta Was this translation helpful? Give feedback.
-
Just a quick note that 0.536 changes useratom callback behavior (behind a flag for now) to be called when the atom is requested, instead of when the string is created. The atom is still cached, but this usually results in a significant reduction in useratom calls since only strings that are eventually used for reflection purposes by the host get the atom computed. |
Beta Was this translation helpful? Give feedback.
-
I'm thinking about how to efficiently pass enums from Lua to C++. The best option seems to be string atoms:
I'm worried about step 2 as that will increase the cost of all string creations, which could negate most of the gains of this optimization, especially in apps that create (lots of) new strings at runtime.
It feels redundant that both Luau and the useratom callback compute a hash for new strings, so I'm wondering could the internal string hash be exposed as an argument to the callback instead? The host would have no way to use the hash for other purposes, so exposing the string hash should be safe I think.
Another option would be to expose string hashes fully to the host:
This would be even faster: the useratom callback overhead would be gone completely and the host could just compute the hashes of all possible enum values upfront and use the incoming string hash as search key in step 3.
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions