From ca621e1082ea66b21b339848147e203695ddcce5 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 24 Oct 2026 04:32:56 -0500 Subject: [PATCH] Trunc helpers: _Trunc/ImTrunc @native, ImTrunc64 its own hoisted function (was an alias of _Trunc: AddRect()/AddRectFilled() called a non-native function per rect), IM_TRUNC/IM_ROUND hoisted + @native; bundle.py: A's local budget counts the hoisted functions (main chunk stays under 200 locals) (demo_all -1.7% instructions; draw data identical) --- imgui_internal.lua | 12 +++++++++++- tools/bundle.py | 9 +++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/imgui_internal.lua b/imgui_internal.lua index dbaf38f..cb02112 100644 --- a/imgui_internal.lua +++ b/imgui_internal.lua @@ -233,18 +233,24 @@ function ImLerp(a, b, t) end --@hoist +@native local function _Trunc(val) if val >= 0 then return mathFloor(val) else return mathCeil(val) end end --- @overload fun(v: float): float --- @overload fun(v: ImVec2): ImVec2 --@hoist +@native function ImTrunc(v) if type(v) == "number" then return _Trunc(v) elseif type(v) == "table" then return ImVec2(_Trunc(v.x), _Trunc(v.y)) end end -ImTrunc64 = _Trunc +-- [Luau] its own (hoisted) function instead of `ImTrunc64 = _Trunc`: calls get inlined (e.g. AddRect()) instead of calling +-- a separate function +--@hoist +@native +function ImTrunc64(val) if val >= 0 then return mathFloor(val) else return mathCeil(val) end end -- Consider using the bit-hack version (search for "0x1p120f"). --- @param f float @@ -264,7 +270,11 @@ function ImIsTruncated(x) return x % 1 == 0 end -- (float)(int)x == x -- Returns true if all parameters are whole numbers. function ImIsTruncated4(x, y, z, w) return x % 1 == 0 and y % 1 == 0 and z % 1 == 0 and w % 1 == 0 end +--@hoist +@native function IM_TRUNC(VAL) return math.floor(VAL) end -- Positive values only! +--@hoist +@native function IM_ROUND(VAL) return math.floor(VAL + 0.5) end -- Positive values only! --@hoist diff --git a/tools/bundle.py b/tools/bundle.py index ec4dc0c..92921a5 100644 --- a/tools/bundle.py +++ b/tools/bundle.py @@ -44,9 +44,10 @@ if "--target" in sys.argv: i = sys.argv.index("--target"); LOVE = sys.argv[i + 1] == "love"; del sys.argv[i:i + 2] sys.argv = [a for a in sys.argv if a != "--no-demo"] OPT = {x: os.environ.get("IMGUI_BUNDLE_NOOPT") != "1" and os.environ.get("IMGUI_BUNDLE_NO_" + x) != "1" for x in "ABCEL"} -# A limits: Luau 200 locals in the main chunk (+ helpers, __FILES, ...), 200 upvalues per function; LuaJIT 60 upvalues -# per function (margin left for nested functions that also capture file locals) -MAX_LOCALS, MAX_UPVALUES = 160, (45 if LOVE else 150) +# A limits: Luau 200 locals in the main chunk (A's hot globals + C's hoisted functions <= MAX_LOCALS, + helpers, __FILES, +# ...), 200 upvalues per function; LuaJIT 60 upvalues per function (margin left for nested functions that also capture +# file locals) +MAX_LOCALS, MAX_UPVALUES = 169, (45 if LOVE else 150) TARGET = "love" if LOVE else "roblox" BACKEND = "imgui_impl_%s.lua" % TARGET BACKEND_DIR = os.path.join(ROOT, "backends", TARGET) # every *.lua in it is bundled (*.luau, e.g. the worker, is not) @@ -281,7 +282,7 @@ if OPT["A"]: used = list(used.values()) taken = [len(u & set(hoisted_names + re.findall(r"\w+", HOIST_DEPS.split("=")[0]))) for u in used] for n in sorted((n for n in count if n not in hoisted_names), key=lambda n: (-count[n], n)): - if len(hot) == MAX_LOCALS: break + if len(hot) + len(hoisted_names) >= MAX_LOCALS: break if all(taken[i] < MAX_UPVALUES for i, u in enumerate(used) if n in u): hot.append(n) taken = [c + (n in u) for c, u in zip(taken, used)]