Completely disabling import optimization for a global variable? #852
Replies: 5 comments 6 replies
-
Yeah generally speaking we expected mutableGlobals to be used for this use case. Is there a way that the host API can be changed so that instead of changing the |
Beta Was this translation helpful? Give feedback.
-
Here's an idea: replace mutableGlobals in compiler options with a callback that gets the three parts of an import as strings? Make a global variable mutable: Make all fields (and all subfields) of a table mutable: Make a field in a table mutable: Make all global variables starting with a prefix mutable: etc. |
Beta Was this translation helpful? Give feedback.
-
Actually, having had a look through the code, there appear to be two distinct problems here:
So adding |
Beta Was this translation helpful? Give feedback.
-
@davidgiven are you sure your code actually uses the fast paths in the VM (imports, fastcalls, safe-envs etc.)? In my projects where I share a single environment for all scripts, I have to patch the imports after loading the scripts (using some custom code that scans the bytecode and traverses the Lua heap). Since scripts are loaded one by one and imports are cached into the constant table for each function proto at load time, any globals whose values have not been set yet do not end up in the constant table. Therefore the VM takes slow path in GETIMPORT opcode and has to look up values for those imports dynamically. |
Beta Was this translation helpful? Give feedback.
-
...aaaand I've found out what's upsetting the typechecker. It's code like this:
It sees the reference to |
Beta Was this translation helpful? Give feedback.
-
Would it be possible to somehow completely disable the import optimization for a certain global variable? I'm trying to port an older game to run on Luau and I'm having troubles because the codebase heavily uses a few global variables which are very often read but only occasionally mutated.
Without getting too deep in the details, let's say there's a global table variable "level" which contains dynamic data for the current level in the game. If I register the variable in the mutable globals array in compile options, imports are correctly disabled for every access to the fields inside "level" as expected. However, reading the value of "level" still causes the compiler to emit GETIMPORT, so mutating the value of "level" does not work.
So ideally I'm looking for a way to still have the safe-env feature enabled but somehow disable the import optimization completely for "level" and all its fields. -O0 or turning off safe-env are not really great options in this case because of the major performance impact. Same for marking the entire global env as mutable (although I'm not sure if that's even possible).
Generally speaking it would be great to have more control over imports, but not sure how exactly...
Beta Was this translation helpful? Give feedback.
All reactions