Cache self.ToLuaValue in ScriptTriggers #9978
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(This explanation is probably going to be terrible, so tl;dr: This improves performance.)
When making an Lua function call, any LuaCustomClrObject must be introspected via reflection in order to determine what to expose in Lua code. In OpenRA, we use these for any types that implement IScriptBindable, such as Actor.
Previously, we would need to pay the cost of this reflection for every individual Lua call an Actor used in its ScriptTriggers trait where it passed
self
as a parameter. This would be repeated every time. For performance, we now cache self.ToLuaValue in the trait and use that for all calls so we only pay the reflection cost once on trait construction. This removes a significant overhead in the Lua bridging code.This requires OpenRA/Eluant#1 in order to realize the performance benefits - but this PR doesn't strictly depend on it and can be merged on its own merits as desired.
Alternatively, in pseudocode:
Calling an Lua function and passing an actor looks like this:
This is morally equivalent to:
The linked PR changes this to:
This PR then changes this to:
On the RA shellmap, this reduces the reflection overhead in calling Lua functions from 4.9% of CPU to 0.9%. This is mainly due to
ScriptTriggers.TickIdle
since that passes an actor parameter.