-
Will chains like this be optimized in luau somehow? function update(state)
if state.player.name.display then
if state.player.name.display.color then
setColor(state.player.name.display.color)
end
end
end In normal "naive" lua I would try to optimize like this function update(state)
local display = state.player.name.display
if display then
local color = display.color
if color then
setColor(color)
end
end
end I wonder how necessary this kind of manual optimization is in Luau? This example might be a bit exaggerated, but I see this pattern in my code all the time. It would be very nice if the style of code chosen here is more of an aesthetic preference instead of trying to help lua with table access. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Because of metatables, optimising this would be unsound. If the |
Beta Was this translation helpful? Give feedback.
-
We currently don't optimize this pattern, so indeed using locals to cache the intermediate lookups can be beneficial for performance. We do plan to look into optimizing cases like this because they are surprisingly prevalent; doing this is likely going to require some subtle changes to the semantics of the language, so it's not something we can do quickly or easily as we'd need to evaluate the impact on compatibility. |
Beta Was this translation helpful? Give feedback.
We currently don't optimize this pattern, so indeed using locals to cache the intermediate lookups can be beneficial for performance. We do plan to look into optimizing cases like this because they are surprisingly prevalent; doing this is likely going to require some subtle changes to the semantics of the language, so it's not something we can do quickly or easily as we'd need to evaluate the impact on compatibility.