richie0866 / rbxm-suite

Tool designed for exploiting with a Rojo-based workflow
MIT License
22 stars 3 forks source link

`setfenv` disables several Luau optimizations #6

Closed EpixScripts closed 2 years ago

EpixScripts commented 2 years ago

There are two very helpful optimizations in Luau that can increase the speed of many scripts: imports and fastcalls. Imports are global chains that can be evaluated at script load time (e.g. math.huge) rather than evaluating them every time they're used at run time; the values are placed in the constant table. Fastcalls are special versions of builtin functions that don't require things like setting up a new stack and all the hassle for calling functions.

However, these optimizations fall when getfenv/setfenv is used. The safeenv property of the functions is turned to false because it is no longer reliable to expect that the values that were evaluated at runtime are the same as they are now with a modified environment.

This project uses setfenv to give some globals to virtual scripts, which completely disables both of these optimizations. https://luau-lang.org/performance#importing-global-access-chains https://luau-lang.org/performance#specialized-builtin-function-calls

I was wondering if there could be a different way of setting these globals, other than using getfenv/setfenv, since these will make scripts run slower.

EpixScripts commented 2 years ago

I just realized that loadstring also disables these optimizations, so unless this is possible to do without it, it might not be possible.

richie0866 commented 2 years ago

This is a bit out of scope, so I'm closing it for now. Bundling your code with remodel is a better solution if you need these optimizations.