Issue Description
Luau optimisations are present in pure environments, however use of getfenv and setfenv mark an environment as impure and disable optimisations. More information can be found via Luau Lang - Performance.
As Roblox continues to develop Luau, it is important that novice developers do not adopt old and bad habits into new work. There are little to no circumstances where using these functions in production is necessary. New developers should be discouraged from using them and so a warning that using these two functions may result in degraded performance seems appropriate.
Why is getfenv/setfenv disabling performance stuff? There is litterary no replacement and I dont want to redo all my work. there is a debug stuff similar to getfenv however it is buggy and doesnt really do its job
This optimization relies on being able to predict the shape of the environment table for a given function; this is possible due to global sandboxing, however this optimization is invalid in some cases:
loadstring can load additional code that runs in context of the callerâs environment
getfenv / setfenv can directly modify the environment of any function
This is from the article colbert posted. As you can see, in laymans terms, it âdisables performance stuffâ because some optimizations rely on a predictable environment, of which getfenv and setfenv allow direct modification to.
The only âRealâ use case of getfenv is sandboxing loadstringâd code, which generally doesnât need optimizations anyways. Use the debug library if you need it for debugging purposes.
Itâs not like itâs getting removed either. The Roblox engineers know there are people out there that still use getfenv, so they decided to keep it and just remove optimizations for code that uses it.
Marking this documentation request as solved in favour of the Luau RFC on deprecating fenv (this was brought to my attention via the latest model safety announcement). A deprecation would be a stronger and better indicator that it shouldnât be pursued in new work: