Question about getfenv/setfenv and 'impure enviroments'

I have a very basic question, when are optimizations disabled when using getfenv exactly?

local myFunc = function() getfenv() end;

Would the above disable optimizations even when not called? And if not would this disable optimizations:

thread = thread or getfenv(2).thread; --Example

Even when I give the thread’s value? I know “or” never runs the second condition if the first one is true, but I do not know how roblox handles these optimizations exactly.

Thanks!

getfenv(), setfenv() and loadstring() perform dynamic deoptimizations of a function by marking its environment as impure, the deoptimization is performed during runtime not during compilation.

1 Like

What about the or question? Would it mark the environment as impure when I set a condition like that?

The deoptimizations are handled dynamically, in that they will only be performed if one of the aforementioned global functions is actually executed.

1 Like

Thank you so much!