Skip to content

Instantly share code, notes, and snippets.

@TheGreatSageEqualToHeaven
Created March 28, 2024 01:34
Show Gist options
  • Save TheGreatSageEqualToHeaven/c2d9bfc886f9ff845b17f2aed7cd70c2 to your computer and use it in GitHub Desktop.
Save TheGreatSageEqualToHeaven/c2d9bfc886f9ff845b17f2aed7cd70c2 to your computer and use it in GitHub Desktop.
Getting the Luau optimize level at runtime safely
local function getOptimizeLevel()
local function dupclosure()
return function() end
end
local O0 = dupclosure() ~= dupclosure()
local function inlinefunction()
return debug.info(1, "f")
end
local f = debug.info(1, "f")
local O2 = f == inlinefunction()
return if O0 then "O0" else (O2 and "O2" or "O1")
end;
print(getOptimizeLevel())
@TheGreatSageEqualToHeaven
Copy link
Author

Alternative that does not use debug.

local i = 0
local function it()
    if i == 0 then
    i = i + 1 
    pcall(it)
    else 
    i = i + 1 
    it()
    end
end
local function depth()
    it()
end
depth()

local function getOptimizeLevel()
    if i == 19998 then
    return "O2"
    end

    local function foo() return function() end end
    
    if foo() == foo() then
    return "O1"
    end

    return "O0"
end;

print(getOptimizeLevel())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment