Perf: memoize ImHashStr per seed, inline glyph lookup in ImFont:RenderText

This commit is contained in:
4DBug
2026-10-08 10:21:16 -05:00
parent 623dc5b936
commit cdf72ede17
4 changed files with 70 additions and 9 deletions
+1 -1
View File
@@ -1 +1 @@
[[12, "imgui.lua"], [13741, "imgui_demo.lua"], [14674, "imgui_demo_2.lua"], [17749, "imgui_demo_apps.lua"], [19008, "imgui_demo_tools.lua"], [20334, "imgui_demo_widgets.lua"], [22258, "imgui_docking.lua"], [25386, "imgui_dragdrop.lua"], [25678, "imgui_draw.lua"], [30900, "imgui_h.lua"], [33066, "imgui_impl_roblox.lua"], [33890, "imgui_impl_roblox_fonts.lua"], [40293, "imgui_internal.lua"], [43449, "imgui_tables.lua"], [47796, "imgui_tabs.lua"], [48963, "imgui_widgets.lua"], [57487, "imstb_rectpack.lua"], [57933, "imstb_textedit.lua"], [59174, "imstb_truetype.lua"], [61642, "imstd_minstdio.lua"]]
[[12, "imgui.lua"], [13761, "imgui_demo.lua"], [14694, "imgui_demo_2.lua"], [17769, "imgui_demo_apps.lua"], [19028, "imgui_demo_tools.lua"], [20354, "imgui_demo_widgets.lua"], [22278, "imgui_docking.lua"], [25406, "imgui_dragdrop.lua"], [25698, "imgui_draw.lua"], [30930, "imgui_h.lua"], [33096, "imgui_impl_roblox.lua"], [33920, "imgui_impl_roblox_fonts.lua"], [40323, "imgui_internal.lua"], [43479, "imgui_tables.lua"], [47826, "imgui_tabs.lua"], [48993, "imgui_widgets.lua"], [57517, "imstb_rectpack.lua"], [57963, "imstb_textedit.lua"], [59204, "imstb_truetype.lua"], [61672, "imstd_minstdio.lua"]]
+34 -3
View File
@@ -353,9 +353,29 @@ end
--- @param size int?
--- @param seed int?
--- @return int
-- [Luau] memo for whole-string hashes: labels are re-hashed with the same seed every frame.
-- ponytail: whole-memo reset at HASH_MEMO_MAX entries, LRU if ID churn makes it thrash
local hash_memo, hash_memo_n, HASH_MEMO_MAX = {}, 0, 16384
local ImHashStrRaw
function ImHashStr(str, size, seed)
if size == nil then size = #str end
if seed == nil then seed = 0 end
if size == nil or size == #str then
local m = hash_memo[seed]
if m == nil then m = {}; hash_memo[seed] = m end
local h = m[str]
if h == nil then
if hash_memo_n >= HASH_MEMO_MAX then hash_memo = { [seed] = m }; table.clear(m); hash_memo_n = 0 end
h = ImHashStrRaw(str, #str, seed)
m[str] = h
hash_memo_n += 1
end
return h
end
return ImHashStrRaw(str, size, seed)
end
function ImHashStrRaw(str, size, seed)
if str == "" or size == 0 then
return seed -- need to match cpp code edge case behavior while using FNV ourselves
@@ -30318,6 +30338,8 @@ function MT.ImFont:RenderText(draw_list, size, pos, col, clip_rect, text, text_b
local word_wrap_eol
local stop = false
local text_is_str = type(text) == "string"
local lookup, lookup_n, glyphs = baked.IndexLookup.Data, baked.IndexLookup.Size, baked.Glyphs.Data
while s < text_end do
repeat
@@ -30339,7 +30361,8 @@ function MT.ImFont:RenderText(draw_list, size, pos, col, clip_rect, text, text_b
end
end
local c = ImStrByte(text, s)
local c
if text_is_str then c = string.byte(text, s) else c = text[s] end
if c < 0x80 then
s = s + 1
else
@@ -30364,7 +30387,14 @@ function MT.ImFont:RenderText(draw_list, size, pos, col, clip_rect, text, text_b
end
end
local glyph = baked:FindGlyph(c)
local glyph
local gi = (c < lookup_n) and lookup[c + 1] or nil
if gi ~= nil and gi ~= IM_FONTGLYPH_INDEX_NOT_FOUND and gi ~= IM_FONTGLYPH_INDEX_UNUSED then
glyph = glyphs[gi]
else
glyph = baked:FindGlyph(c) -- may load the glyph and grow the tables
lookup, lookup_n, glyphs = baked.IndexLookup.Data, baked.IndexLookup.Size, baked.Glyphs.Data
end
local char_width = glyph.AdvanceX * scale
if glyph.Visible then
@@ -62261,6 +62291,7 @@ return {
ImGui_ImplRoblox_UpdateTexture = ImGui_ImplRoblox_UpdateTexture,
ImHashData = ImHashData,
ImHashStr = ImHashStr,
ImHashStrRaw = ImHashStrRaw,
ImIsPowerOfTwo = ImIsPowerOfTwo,
ImLengthSqr = ImLengthSqr,
ImLerp = ImLerp,
+21 -1
View File
@@ -342,9 +342,29 @@ end
--- @param size int?
--- @param seed int?
--- @return int
-- [Luau] memo for whole-string hashes: labels are re-hashed with the same seed every frame.
-- ponytail: whole-memo reset at HASH_MEMO_MAX entries, LRU if ID churn makes it thrash
local hash_memo, hash_memo_n, HASH_MEMO_MAX = {}, 0, 16384
local ImHashStrRaw
function ImHashStr(str, size, seed)
if size == nil then size = #str end
if seed == nil then seed = 0 end
if size == nil or size == #str then
local m = hash_memo[seed]
if m == nil then m = {}; hash_memo[seed] = m end
local h = m[str]
if h == nil then
if hash_memo_n >= HASH_MEMO_MAX then hash_memo = { [seed] = m }; table.clear(m); hash_memo_n = 0 end
h = ImHashStrRaw(str, #str, seed)
m[str] = h
hash_memo_n += 1
end
return h
end
return ImHashStrRaw(str, size, seed)
end
function ImHashStrRaw(str, size, seed)
if str == "" or size == 0 then
return seed -- need to match cpp code edge case behavior while using FNV ourselves
+12 -2
View File
@@ -4641,6 +4641,8 @@ function MT.ImFont:RenderText(draw_list, size, pos, col, clip_rect, text, text_b
local word_wrap_eol
local stop = false
local text_is_str = type(text) == "string"
local lookup, lookup_n, glyphs = baked.IndexLookup.Data, baked.IndexLookup.Size, baked.Glyphs.Data
while s < text_end do
repeat
@@ -4662,7 +4664,8 @@ function MT.ImFont:RenderText(draw_list, size, pos, col, clip_rect, text, text_b
end
end
local c = ImStrByte(text, s)
local c
if text_is_str then c = string.byte(text, s) else c = text[s] end
if c < 0x80 then
s = s + 1
else
@@ -4687,7 +4690,14 @@ function MT.ImFont:RenderText(draw_list, size, pos, col, clip_rect, text, text_b
end
end
local glyph = baked:FindGlyph(c)
local glyph
local gi = (c < lookup_n) and lookup[c + 1] or nil
if gi ~= nil and gi ~= IM_FONTGLYPH_INDEX_NOT_FOUND and gi ~= IM_FONTGLYPH_INDEX_UNUSED then
glyph = glyphs[gi]
else
glyph = baked:FindGlyph(c) -- may load the glyph and grow the tables
lookup, lookup_n, glyphs = baked.IndexLookup.Data, baked.IndexLookup.Size, baked.Glyphs.Data
end
local char_width = glyph.AdvanceX * scale
if glyph.Visible then