Library: fewer calls/lookups per text item, table cell and frame
- Text(): TextV() inlined (one vararg call less per formatted Text); TextEx(): CalcTextSize() inlined - ImFontCalcTextSizeXY / RenderText / CalcWordWrapPosition: GetFontBaked() fast path inlined (LastBaked check) - TableBeginCell(): SetWindowClipRectBeforeSetChannel() inlined; SetCurrentChannel(): skip the TexRef field reads when both commands share the same TexRef (as ImDrawCmd_HeaderCompare already does) - UpdateKeyboardInputs / UpdateKeyRoutingTable: loop-invariant field reads hoisted - `#` on strings replaced by string.len() in ImFontCalcTextSizeXY (`#` is not a native fast path) Headless --roblox: stress+demo 0.98 -> 0.89 ms/frame, empty frame 0.085 -> 0.067 ms; draw data unchanged.
This commit is contained in:
1 parent
d9d8ee4e6b
commit
ca571ee788
4 files changed
+55
-20
No files matched your search
@@ -8989,10 +8989,11 @@ end
|
||||
function ImGui.UpdateKeyRoutingTable(rt)
|
||||
local g = GImGui --[[@as ImGuiContext]]
|
||||
rt.EntriesNext:resize(0)
|
||||
local index, entries_next = rt.Index, rt.EntriesNext -- [Luau] cached field reads (same tables until the swap below)
|
||||
for key = ImGuiKey.NamedKey_BEGIN, ImGuiKey.NamedKey_END - 1 do
|
||||
local new_routing_start_idx = rt.EntriesNext.Size + 1
|
||||
local new_routing_start_idx = entries_next.Size + 1
|
||||
local routing_entry
|
||||
local old_routing_idx = rt.Index[key - ImGuiKey.NamedKey_BEGIN + 1]
|
||||
local old_routing_idx = index[key - ImGuiKey.NamedKey_BEGIN + 1]
|
||||
while old_routing_idx ~= -1 do
|
||||
repeat
|
||||
routing_entry = rt.Entries[old_routing_idx]
|
||||
@@ -9004,7 +9005,7 @@ function ImGui.UpdateKeyRoutingTable(rt)
|
||||
break
|
||||
end
|
||||
|
||||
rt.EntriesNext:push_back(routing_entry) -- Write alive ones into new buffer
|
||||
entries_next:push_back(routing_entry) -- Write alive ones into new buffer
|
||||
|
||||
-- Apply routing to owner if there's no owner already (RoutingCurr == None at this point)
|
||||
-- This is the result of previous frame's SetShortcutRouting() call.
|
||||
@@ -9022,9 +9023,9 @@ function ImGui.UpdateKeyRoutingTable(rt)
|
||||
end
|
||||
|
||||
-- Rewrite linked-list
|
||||
rt.Index[key - ImGuiKey.NamedKey_BEGIN + 1] = (new_routing_start_idx <= rt.EntriesNext.Size and new_routing_start_idx or -1)
|
||||
for n = new_routing_start_idx, rt.EntriesNext.Size do
|
||||
rt.EntriesNext[n].NextEntryIndex = (n + 1 <= rt.EntriesNext.Size) and (n + 1) or -1
|
||||
index[key - ImGuiKey.NamedKey_BEGIN + 1] = (new_routing_start_idx <= entries_next.Size and new_routing_start_idx or -1)
|
||||
for n = new_routing_start_idx, entries_next.Size do
|
||||
entries_next[n].NextEntryIndex = (n + 1 <= entries_next.Size) and (n + 1) or -1
|
||||
end
|
||||
end
|
||||
rt.Entries:swap(rt.EntriesNext) -- Swap new and old indexes
|
||||
@@ -13588,14 +13589,15 @@ function ImGui.UpdateKeyboardInputs()
|
||||
end
|
||||
|
||||
-- Update keys
|
||||
local keys_data, delta_time = io.KeysData, io.DeltaTime -- [Luau] cached field reads
|
||||
for key = ImGuiKey.NamedKey_BEGIN, ImGuiKey.NamedKey_END - 1 do
|
||||
local key_data = io.KeysData[key - ImGuiKey.NamedKey_BEGIN]
|
||||
local key_data = keys_data[key - ImGuiKey.NamedKey_BEGIN]
|
||||
key_data.DownDurationPrev = key_data.DownDuration
|
||||
if key_data.Down then
|
||||
if key_data.DownDuration < 0.0 then
|
||||
key_data.DownDuration = 0.0
|
||||
else
|
||||
key_data.DownDuration = key_data.DownDuration + io.DeltaTime
|
||||
key_data.DownDuration = key_data.DownDuration + delta_time
|
||||
end
|
||||
else
|
||||
key_data.DownDuration = -1.0
|
||||
@@ -13610,9 +13612,10 @@ function ImGui.UpdateKeyboardInputs()
|
||||
end
|
||||
|
||||
-- Update keys/input owner (named keys only): one entry per key
|
||||
local keys_owner_data = g.KeysOwnerData -- [Luau] cached field read
|
||||
for key = ImGuiKey.NamedKey_BEGIN, ImGuiKey.NamedKey_END - 1 do
|
||||
local key_data = io.KeysData[key - ImGuiKey.NamedKey_BEGIN]
|
||||
local owner_data = g.KeysOwnerData[key - ImGuiKey.NamedKey_BEGIN]
|
||||
local key_data = keys_data[key - ImGuiKey.NamedKey_BEGIN]
|
||||
local owner_data = keys_owner_data[key - ImGuiKey.NamedKey_BEGIN]
|
||||
owner_data.OwnerCurr = owner_data.OwnerNext
|
||||
if not key_data.Down then -- Important: ownership is released on the frame after a release. Ensure a 'MouseDown -> CloseWindow -> MouseUp' chain doesn't lead to someone else seeing the MouseUp.
|
||||
owner_data.OwnerNext = ImGuiKeyOwner_NoOwner
|
||||
|
||||
+14
-7
@@ -3320,7 +3320,10 @@ local ImFontCalcWordWrapPositionEx_Uncached
|
||||
local WRAP_MEMO_MAX = 4096
|
||||
|
||||
function ImFontCalcWordWrapPositionEx(font, size, text, pos, text_end, wrap_width, flags)
|
||||
local baked = font:GetFontBaked(size)
|
||||
local baked = font.LastBaked -- [Luau] inline font:GetFontBaked(size) fast path
|
||||
if not (baked and baked.Size == math.floor(size + 0.5) and baked.RasterizerDensity == font.CurrentRasterizerDensity) then
|
||||
baked = font:GetFontBaked(size)
|
||||
end
|
||||
if type(text) ~= "string" then
|
||||
return ImFontCalcWordWrapPositionEx_Uncached(baked, size, text, pos, text_end, wrap_width, flags)
|
||||
end
|
||||
@@ -3509,11 +3512,14 @@ function ImFontCalcTextSizeXY(font, size, max_width, wrap_width, text, text_begi
|
||||
-- Advances of a baked font never change once loaded, so entries stay valid.
|
||||
local cache_key = nil
|
||||
local wrap_cache = nil
|
||||
local text_len1 = text_is_str and #text + 1 or -1
|
||||
local text_len1 = text_is_str and string.len(text) + 1 or -1 -- [Luau] string.len(): `#` on a string isn't a native fast path
|
||||
local baked = font.LastBaked -- [Luau] inline font:GetFontBaked(size) fast path (same baked as the calls below in upstream)
|
||||
if not (baked and baked.Size == math.floor(size + 0.5) and baked.RasterizerDensity == font.CurrentRasterizerDensity) then
|
||||
baked = font:GetFontBaked(size)
|
||||
end
|
||||
local cacheable = text_is_str and text_begin == 1 and out_offset == nil and max_width >= FLT_MAX
|
||||
and (text_end == nil or text_end == text_len1) and (text_end_display == nil or text_end_display == text_len1) and (flags == nil or bit32.band(flags, ImDrawTextFlags.StopOnNewLine) == 0)
|
||||
if cacheable and wrap_width <= 0.0 then
|
||||
local baked = font:GetFontBaked(size)
|
||||
local cache = baked._TextSizeCache
|
||||
if cache == nil or baked._TextSizeCacheN >= TEXT_SIZE_CACHE_MAX then
|
||||
cache = {}; baked._TextSizeCache = cache; baked._TextSizeCacheN = 0
|
||||
@@ -3525,7 +3531,6 @@ function ImFontCalcTextSizeXY(font, size, max_width, wrap_width, text, text_begi
|
||||
cache_key = text
|
||||
elseif cacheable and wrap_width > 0.0 then
|
||||
-- [Luau] same for wrapped text (TextWrapped() measures every line every frame): exact results per (size, wrap width, text)
|
||||
local baked = font:GetFontBaked(size)
|
||||
local cache = baked._TextSizeCache
|
||||
if cache == nil or baked._TextSizeCacheN >= TEXT_SIZE_CACHE_MAX then
|
||||
cache = {}; baked._TextSizeCache = cache; baked._TextSizeCacheN = 0
|
||||
@@ -3547,7 +3552,6 @@ function ImFontCalcTextSizeXY(font, size, max_width, wrap_width, text, text_begi
|
||||
text_end_display = text_end
|
||||
end
|
||||
|
||||
local baked = font:GetFontBaked(size)
|
||||
local line_height = size
|
||||
local scale = line_height / baked.Size
|
||||
|
||||
@@ -4760,7 +4764,7 @@ function MT.ImDrawListSplitter:SetCurrentChannel(draw_list, idx)
|
||||
curr_cmd.TexRef = header.TexRef
|
||||
curr_cmd.VtxOffset = header.VtxOffset
|
||||
elseif ccr.x ~= hcr.x or ccr.y ~= hcr.y or ccr.z ~= hcr.z or ccr.w ~= hcr.w
|
||||
or curr_cmd.TexRef._TexData ~= header.TexRef._TexData or curr_cmd.TexRef._TexID ~= header.TexRef._TexID
|
||||
or (curr_cmd.TexRef ~= header.TexRef and (curr_cmd.TexRef._TexData ~= header.TexRef._TexData or curr_cmd.TexRef._TexID ~= header.TexRef._TexID))
|
||||
or curr_cmd.VtxOffset ~= header.VtxOffset then
|
||||
draw_list:AddDrawCmd()
|
||||
end
|
||||
@@ -7316,7 +7320,10 @@ function MT.ImFont:RenderText(draw_list, size, pos, col, clip_rect, text, text_b
|
||||
|
||||
|
||||
local line_height = size
|
||||
local baked = self:GetFontBaked(size)
|
||||
local baked = self.LastBaked -- [Luau] inline self:GetFontBaked(size) fast path
|
||||
if not (baked and baked.Size == math.floor(size + 0.5) and baked.RasterizerDensity == self.CurrentRasterizerDensity) then
|
||||
baked = self:GetFontBaked(size)
|
||||
end
|
||||
|
||||
local scale = size / baked.Size
|
||||
local origin_x = x
|
||||
|
||||
+15
-1
@@ -2613,7 +2613,21 @@ function ImGui.TableBeginCell(table_, column_n)
|
||||
--IM_ASSERT(table.DrawSplitter._Current == TABLE_DRAW_CHANNEL_NOCLIP)
|
||||
else
|
||||
-- FIXME-TABLE: Could avoid this if draw channel is dummy channel?
|
||||
ImGui.SetWindowClipRectBeforeSetChannel(window, column.ClipRect)
|
||||
-- [Luau] inline ImGui.SetWindowClipRectBeforeSetChannel(window, column.ClipRect)
|
||||
local cr = column.ClipRect
|
||||
local x1, y1, x2, y2 = cr.Min.x, cr.Min.y, cr.Max.x, cr.Max.y
|
||||
local wcr = window.ClipRect
|
||||
wcr.Min.x = x1; wcr.Min.y = y1; wcr.Max.x = x2; wcr.Max.y = y2
|
||||
local draw_list = window.DrawList
|
||||
local hcr = draw_list._CmdHeader.ClipRect
|
||||
hcr.x = x1; hcr.y = y1; hcr.z = x2; hcr.w = y2
|
||||
local stack = draw_list._ClipRectStack
|
||||
local top = stack.Data[stack.Size]
|
||||
if top ~= nil then
|
||||
top.x = x1; top.y = y1; top.z = x2; top.w = y2
|
||||
else
|
||||
stack.Data[stack.Size] = ImVec4(x1, y1, x2, y2)
|
||||
end
|
||||
local channel = column.DrawChannelCurrent
|
||||
if splitter._Current ~= channel then
|
||||
splitter:SetCurrentChannel(window.DrawList, channel)
|
||||
|
||||
+13
-2
@@ -110,7 +110,12 @@ function ImGui.TextEx(text, text_end, flags)
|
||||
-- [Luau] scalar text_pos, scratch bb (ItemAdd() copies it, RenderTextWrapped() only reads it)
|
||||
local text_pos_x, text_pos_y = dc.CursorPos.x, dc.CursorPos.y + dc.CurrLineTextBaseOffset
|
||||
local wrap_width = wrap_enabled and ImGui.CalcWrapWidthForPos(dc.CursorPos, wrap_pos_x) or 0.0
|
||||
local text_size_x, text_size_y = ImGui.CalcTextSizeXY(text, 1, text_end, false, wrap_width)
|
||||
-- [Luau] inline ImGui.CalcTextSizeXY(text, 1, text_end, false, wrap_width)
|
||||
local text_size_x, text_size_y = 0.0, g.FontSize
|
||||
if text ~= "" and text_end > 1 then
|
||||
text_size_x, text_size_y = ImFontCalcTextSizeXY(g.Font, text_size_y, FLT_MAX, wrap_width, text, 1, text_end, text_end, nil, ImDrawTextFlags.None)
|
||||
text_size_x = math.ceil(text_size_x) -- Round (see CalcTextSize())
|
||||
end
|
||||
|
||||
local bb = TextEx_bb
|
||||
local bb_min, bb_max = bb.Min, bb.Max
|
||||
@@ -226,7 +231,13 @@ end
|
||||
@native
|
||||
function ImGui.Text(fmt, ...)
|
||||
if select('#', ...) > 0 then
|
||||
ImGui.TextV(fmt, ...)
|
||||
-- [Luau] inline ImGui.TextV(fmt, ...) (saves a vararg call per Text())
|
||||
local window = GImGui.CurrentWindow -- [Luau] inline GetCurrentWindow()
|
||||
window.WriteAccessed = true
|
||||
if window.SkipItems then
|
||||
return
|
||||
end
|
||||
ImGui.TextEx(ImFormatString(fmt, ...), nil, ImGuiTextFlags.NoWidthForLargeClippedText)
|
||||
else
|
||||
ImGui.TextEx(fmt)
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user