Library: tables without per-instance temporaries in BeginTableEx()/TableUpdateLayout()/TableMergeDrawChannels() (scratch rects, scalar outer size); ImDrawListSplitter::Merge() keeps the command it merges away past Size for reuse (was 6 new ImDrawCmd per table per frame): 435 -> 247 tables/frame allocated for 10 small tables in a window

Draw data unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-21 01:14:31 -05:00
1 parent d1b8a2242a
commit e636e2d8be
2 files changed
+18 -8

No files matched your search

+5 -1
View File
@@ -4691,7 +4691,11 @@ function MT.ImDrawListSplitter:Merge(draw_list)
-- Merge previous channel last draw command with current channel first draw command if matching. -- Merge previous channel last draw command with current channel first draw command if matching.
last_cmd.ElemCount = last_cmd.ElemCount + next_cmd.ElemCount last_cmd.ElemCount = last_cmd.ElemCount + next_cmd.ElemCount
idx_offset = idx_offset + next_cmd.ElemCount idx_offset = idx_offset + next_cmd.ElemCount
cmds:erase(1) -- FIXME-OPT: Improve for multiple merges. -- [Luau] cmds:erase(1), keeping the erased command object past Size for AddDrawCmd() to reuse
local d = cmds.Data
for k = 1, cmds.Size - 1 do d[k] = d[k + 1] end
d[cmds.Size] = next_cmd
cmds.Size = cmds.Size - 1 -- FIXME-OPT: Improve for multiple merges.
end end
end end
if cmds.Size > 0 then if cmds.Size > 0 then
+13 -7
View File
@@ -355,6 +355,8 @@ end
ImGuiTableColumnFlags.NoDirectResize_ = ImGuiTableColumnFlags.NoDirectResize_ or lshift(1, 30) -- [Internal] Disable user resizing this column directly (it may however we resized indirectly from its left edge) ImGuiTableColumnFlags.NoDirectResize_ = ImGuiTableColumnFlags.NoDirectResize_ or lshift(1, 30) -- [Internal] Disable user resizing this column directly (it may however we resized indirectly from its left edge)
local function Rect(r) return ImRect(r.Min.x, r.Min.y, r.Max.x, r.Max.y) end local function Rect(r) return ImRect(r.Min.x, r.Min.y, r.Max.x, r.Max.y) end
-- [Luau] scratch rects for TableUpdateLayout()/TableMergeDrawChannels() locals that never escape
local TableUpdateLayout_work_rect, TableUpdateLayout_host_clip_rect, TableUpdateLayout_mouse_hit_rect, TableMergeDrawChannels_merge_clip_rect = ImRect(), ImRect(), ImRect(), ImRect()
local function V2(v) return ImVec2(v.x, v.y) end local function V2(v) return ImVec2(v.x, v.y) end
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@@ -452,9 +454,10 @@ function ImGui.BeginTable(str_id, columns_count, flags, outer_size, inner_width)
return ImGui.BeginTableEx(str_id, id, columns_count, flags, outer_size, inner_width) return ImGui.BeginTableEx(str_id, id, columns_count, flags, outer_size, inner_width)
end end
local BeginTableEx_outer_size_default = ImVec2(0.0, 0.0) -- [Luau] shared read-only default (only read below)
function ImGui.BeginTableEx(name, id, columns_count, flags, outer_size, inner_width) function ImGui.BeginTableEx(name, id, columns_count, flags, outer_size, inner_width)
if flags == nil then flags = 0 end if flags == nil then flags = 0 end
if outer_size == nil then outer_size = ImVec2(0.0, 0.0) end if outer_size == nil then outer_size = BeginTableEx_outer_size_default end
if inner_width == nil then inner_width = 0.0 end if inner_width == nil then inner_width = 0.0 end
local g = GImGui local g = GImGui
@@ -478,8 +481,10 @@ function ImGui.BeginTableEx(name, id, columns_count, flags, outer_size, inner_wi
-- - always performing the GetOrAddByKey() O(log N) query in g.Tables.Map[]. -- - always performing the GetOrAddByKey() O(log N) query in g.Tables.Map[].
local use_child_window = band(flags, bor(ImGuiTableFlags.ScrollX, ImGuiTableFlags.ScrollY)) ~= 0 local use_child_window = band(flags, bor(ImGuiTableFlags.ScrollX, ImGuiTableFlags.ScrollY)) ~= 0
local avail_size = ImGui.GetContentRegionAvail() local avail_size = ImGui.GetContentRegionAvail()
local actual_outer_size = ImTrunc(ImGui.CalcItemSize(outer_size, mathMax(avail_size.x, IMGUI_WINDOW_HARD_MIN_SIZE), use_child_window and mathMax(avail_size.y, IMGUI_WINDOW_HARD_MIN_SIZE) or 0.0)) local actual_outer_size_x, actual_outer_size_y = ImGui._CalcItemSizeXY(outer_size.x, outer_size.y, mathMax(avail_size.x, IMGUI_WINDOW_HARD_MIN_SIZE), use_child_window and mathMax(avail_size.y, IMGUI_WINDOW_HARD_MIN_SIZE) or 0.0)
local outer_rect = ImRect(outer_window.DC.CursorPos, outer_window.DC.CursorPos + actual_outer_size) actual_outer_size_x, actual_outer_size_y = ImTrunc(actual_outer_size_x), ImTrunc(actual_outer_size_y) -- [Luau] scalar ImTrunc(CalcItemSize(...))
local outer_cursor = outer_window.DC.CursorPos
local outer_rect = ImRect(outer_cursor.x, outer_cursor.y, outer_cursor.x + actual_outer_size_x, outer_cursor.y + actual_outer_size_y) -- [Luau] scalar form: no temporary ImVec2
local outer_window_is_measuring_size = (outer_window.AutoFitFramesX > 0) or (outer_window.AutoFitFramesY > 0) -- Doesn't apply to AlwaysAutoResize windows! local outer_window_is_measuring_size = (outer_window.AutoFitFramesX > 0) or (outer_window.AutoFitFramesY > 0) -- Doesn't apply to AlwaysAutoResize windows!
if use_child_window and ImGui.IsClippedEx(outer_rect, 0) and not outer_window_is_measuring_size then if use_child_window and ImGui.IsClippedEx(outer_rect, 0) and not outer_window_is_measuring_size then
ImGui.ItemSize(outer_rect) ImGui.ItemSize(outer_rect)
@@ -1231,7 +1236,7 @@ function ImGui.TableUpdateLayout(table_)
table_.ColumnsStretchSumWeights = stretch_sum_weights table_.ColumnsStretchSumWeights = stretch_sum_weights
-- [Part 4] Apply final widths based on requested widths -- [Part 4] Apply final widths based on requested widths
local work_rect = Rect(table_.WorkRect) local work_rect = TableUpdateLayout_work_rect; ImRect_Copy(work_rect, table_.WorkRect) -- [Luau] scratch copy
local width_spacings = (table_.OuterPaddingX * 2.0) + (table_.CellSpacingX1 + table_.CellSpacingX2) * (table_.ColumnsEnabledCount - 1) local width_spacings = (table_.OuterPaddingX * 2.0) + (table_.CellSpacingX1 + table_.CellSpacingX2) * (table_.ColumnsEnabledCount - 1)
local width_removed = (table_.HasScrollbarYPrev and not table_.InnerWindow.ScrollbarY) and g.Style.ScrollbarSize or 0.0 -- To synchronize decoration width of synced tables with mismatching scrollbar state (#5920) local width_removed = (table_.HasScrollbarYPrev and not table_.InnerWindow.ScrollbarY) and g.Style.ScrollbarSize or 0.0 -- To synchronize decoration width of synced tables with mismatching scrollbar state (#5920)
local width_avail = mathMax(1.0, ((band(table_.Flags, ImGuiTableFlags.ScrollX) ~= 0 and table_.InnerWidth == 0.0) and table_.InnerClipRect:GetWidth() or work_rect:GetWidth()) - width_removed) local width_avail = mathMax(1.0, ((band(table_.Flags, ImGuiTableFlags.ScrollX) ~= 0 and table_.InnerWidth == 0.0) and table_.InnerClipRect:GetWidth() or work_rect:GetWidth()) - width_removed)
@@ -1287,7 +1292,8 @@ function ImGui.TableUpdateLayout(table_)
table_instance.HoveredRowLast = table_instance.HoveredRowNext table_instance.HoveredRowLast = table_instance.HoveredRowNext
table_instance.HoveredRowNext = -1 table_instance.HoveredRowNext = -1
table_.HoveredColumnBody = -1; table_.HoveredColumnBorder = -1 table_.HoveredColumnBody = -1; table_.HoveredColumnBorder = -1
local mouse_hit_rect = ImRect(table_.OuterRect.Min.x, table_.OuterRect.Min.y, table_.OuterRect.Max.x, mathMax(table_.OuterRect.Max.y, table_.OuterRect.Min.y + table_instance.LastOuterHeight)) local mouse_hit_rect = TableUpdateLayout_mouse_hit_rect -- [Luau] scratch, ItemHoverable() only reads it
mouse_hit_rect.Min.x, mouse_hit_rect.Min.y, mouse_hit_rect.Max.x, mouse_hit_rect.Max.y = table_.OuterRect.Min.x, table_.OuterRect.Min.y, table_.OuterRect.Max.x, mathMax(table_.OuterRect.Max.y, table_.OuterRect.Min.y + table_instance.LastOuterHeight)
local backup_active_id = g.ActiveId local backup_active_id = g.ActiveId
g.ActiveId = 0 g.ActiveId = 0
local is_hovering_table = ImGui.ItemHoverable(mouse_hit_rect, 0, ImGuiItemFlags.None) local is_hovering_table = ImGui.ItemHoverable(mouse_hit_rect, 0, ImGuiItemFlags.None)
@@ -1306,7 +1312,7 @@ function ImGui.TableUpdateLayout(table_)
local has_at_least_one_column_requesting_output = false local has_at_least_one_column_requesting_output = false
local offset_x_frozen = (table_.FreezeColumnsCount > 0) local offset_x_frozen = (table_.FreezeColumnsCount > 0)
local offset_x = ((table_.FreezeColumnsCount > 0) and table_.OuterRect.Min.x or work_rect.Min.x) + table_.OuterPaddingX - table_.CellSpacingX1 local offset_x = ((table_.FreezeColumnsCount > 0) and table_.OuterRect.Min.x or work_rect.Min.x) + table_.OuterPaddingX - table_.CellSpacingX1
local host_clip_rect = Rect(table_.InnerClipRect) local host_clip_rect = TableUpdateLayout_host_clip_rect; ImRect_Copy(host_clip_rect, table_.InnerClipRect) -- [Luau] scratch copy
--host_clip_rect.Max.x += table.CellPaddingX + table.CellSpacingX2 --host_clip_rect.Max.x += table.CellPaddingX + table.CellSpacingX2
BitArrayClearAll(table_.VisibleMaskByIndex) BitArrayClearAll(table_.VisibleMaskByIndex)
for order_n = 0, columns_count - 1 do for order_n = 0, columns_count - 1 do
@@ -3133,7 +3139,7 @@ function ImGui.TableMergeDrawChannels(table_)
local merge_channels_count = merge_groups[merge_group_n].ChannelsCount local merge_channels_count = merge_groups[merge_group_n].ChannelsCount
if merge_channels_count ~= 0 then if merge_channels_count ~= 0 then
local merge_group = merge_groups[merge_group_n] local merge_group = merge_groups[merge_group_n]
local merge_clip_rect = Rect(merge_group.ClipRect) local merge_clip_rect = TableMergeDrawChannels_merge_clip_rect; ImRect_Copy(merge_clip_rect, merge_group.ClipRect) -- [Luau] scratch copy
-- Extend outer-most clip limits to match those of host, so draw calls can be merged even if -- Extend outer-most clip limits to match those of host, so draw calls can be merged even if
-- outer-most columns have some outer padding offsetting them from their parent ClipRect. -- outer-most columns have some outer padding offsetting them from their parent ClipRect.