From 72965ada011b2fac942cfb08e09df2ff9b8a5ce5 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 8 Feb 2024 16:48:31 +0300 Subject: [PATCH] perf(utilities): Leave tostring() casting to after debug invocations, not before --- classes/base.lua | 2 +- core/break.lua | 32 ++++++++++++++++---------------- core/settings.lua | 2 +- core/utilities/init.lua | 2 +- packages/math/base-elements.lua | 2 +- pagebuilders/grid.lua | 2 +- typesetters/firstfit.lua | 6 +++--- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/classes/base.lua b/classes/base.lua index b804b5a2f..38b97bd14 100644 --- a/classes/base.lua +++ b/classes/base.lua @@ -272,7 +272,7 @@ end function class:runHooks (category, options) for _, func in ipairs(self.hooks[category]) do - SU.debug("classhooks", "Running hook from", category, options and "with options " .. #options) + SU.debug("classhooks", "Running hook from", category, options and "with options #" .. #options) func(self, options) end end diff --git a/core/break.lua b/core/break.lua index 0894e6e6b..652f8d1c4 100644 --- a/core/break.lua +++ b/core/break.lua @@ -156,7 +156,7 @@ function lineBreak:tryBreak() -- 855 if not node then pi = ejectPenalty; breakType = "hyphenated" elseif node.is_discretionary then breakType = "hyphenated"; pi = param("hyphenPenalty") else breakType = "unhyphenated"; pi = node.penalty or 0 end - if debugging then SU.debug("break", "Trying a ", breakType, "break p =", pi) end + if debugging then SU.debug("break", "Trying a", breakType, "break p =", pi) end self.no_break_yet = true -- We have to store all this state crap in the object, or it's global variables all the way self.prev_prev_r = nil self.prev_r = self.activeListHead @@ -176,7 +176,7 @@ function lineBreak:tryBreak() -- 855 end -- 861 if self.r.lineNumber > self.old_l then - if debugging then SU.debug("break", "Minimum demerits = " .. self.minimumDemerits) end + if debugging then SU.debug("break", "Minimum demerits =", self.minimumDemerits) end if self.minimumDemerits < awful_bad and (self.old_l ~= self.easy_line or self.r == self.activeListHead) then self:createNewActiveNodes(breakType) end @@ -199,16 +199,16 @@ function lineBreak:tryBreak() -- 855 self.lineWidth = self.firstWidth end end - if debugging then SU.debug("break", "line width = " .. tostring(self.lineWidth)) end + if debugging then SU.debug("break", "line width =", self.lineWidth) end end if debugging then - SU.debug("break", " ---> (2) cuaw is " .. tostring(self.curActiveWidth)) - SU.debug("break", " ---> aw is " .. tostring(self.activeWidth)) + SU.debug("break", " ---> (2) cuaw is", self.curActiveWidth) + SU.debug("break", " ---> aw is", self.activeWidth) end self:considerDemerits(pi, breakType) if debugging then - SU.debug("break", " <--- cuaw is " .. tostring(self.curActiveWidth)) - SU.debug("break", " <--- aw is " .. tostring(self.activeWidth)) + SU.debug("break", " <--- cuaw is", self.curActiveWidth) + SU.debug("break", " <--- aw is ", self.activeWidth) end end end @@ -269,7 +269,7 @@ function lineBreak:tryAlternatives(from, to) local ss = shortfall - addWidth -- Warning, assumes abosolute local badness = SU.rateBadness(inf_bad, ss.length.amount, self.curActiveWidth[ss > 0 and "stretch" or "shrink"].length.amount) - if debugging then SU.debug("break", " badness of " .. ss .. " (" .. self.curActiveWidth .. ") is " .. badness) end + if debugging then SU.debug("break", " badness of", ss, "(", self.curActiveWidth, ") is", badness) end if badness < localMinimum then self.r.alternates = alternates self.r.altSelections = combination @@ -293,7 +293,7 @@ function lineBreak:considerDemerits(pi, breakType) -- 877 end shortfall = self.lineWidth - self.curActiveWidth self.badness, self.fitClass = fitclass(self, shortfall) - if debugging then SU.debug("break", self.badness .. " " .. self.fitClass) end + if debugging then SU.debug("break", self.badness, self.fitClass) end if (self.badness > inf_bad or pi == ejectPenalty) then if self.finalpass and self.minimumDemerits == awful_bad and self.r.next == self.activeListHead and self.prev_r == self.activeListHead then self.artificialDemerits = true @@ -382,11 +382,11 @@ function lineBreak:recordFeasible(pi, breakType) -- 881 local demerit = lineBreak:computeDemerits(pi, breakType) if debugging then if self.nodes[self.place] then - SU.debug("break", "@" .. self.nodes[self.place] .. " via @@" .. (self.r.serial or "0") .. " badness=" .. self.badness .. " demerit=".. demerit) -- 882 + SU.debug("break", "@", self.nodes[self.place], "via @@", (self.r.serial or "0"), "badness =", self.badness, "demerit =", demerit) -- 882 else SU.debug("break", "@ \\par via @@") end - SU.debug("break", " fit class = "..self.fitClass) + SU.debug("break", " fit class =", self.fitClass) end demerit = demerit + self.r.totalDemerits if demerit <= self.bestInClass[self.fitClass].minimalDemerits then @@ -422,7 +422,7 @@ function lineBreak:createNewActiveNodes(breakType) -- 862 end place = place + 1 end - if debugging then SU.debug("break", "Value of breakWidth = " .. tostring(self.breakWidth)) end + if debugging then SU.debug("break", "Value of breakWidth =", self.breakWidth) end end -- 869 (Add a new delta node) if self.prev_r.type == "delta" then @@ -432,7 +432,7 @@ function lineBreak:createNewActiveNodes(breakType) -- 862 self.activeWidth = SILE.types.length(self.breakWidth) else local newDelta = { next = self.r, type = "delta", width = self.breakWidth - self.curActiveWidth } - if debugging then SU.debug("break", "Added new delta node = " .. tostring(newDelta.width)) end + if debugging then SU.debug("break", "Added new delta node =", newDelta.width) end self.prev_r.next = newDelta self.prev_prev_r = self.prev_r self.prev_r = newDelta @@ -447,7 +447,7 @@ function lineBreak:createNewActiveNodes(breakType) -- 862 local class = classes[i] local best = self.bestInClass[class] local value = best.minimalDemerits - if debugging then SU.debug("break", "Class is "..class.." Best value here is " .. value) end + if debugging then SU.debug("break", "Class is", class, "Best value here is", value) end if value <= self.minimumDemerits then -- 871: this is what creates new active notes @@ -489,7 +489,7 @@ function lineBreak.dumpBreakNode(_, node) end function lineBreak:describeBreakNode(node) - --print("@@" .. b.serial .. ": line " .. (b.lineNumber -1) .. "." .. b.fitness .. " " .. b.type .. " t=".. b.totalDemerits .. " -> @@ " .. (b.prevBreak and b.prevBreak.serial or "0") ) + --SU.debug("break", "@@", b.serial, ": line", b.lineNumber - 1, ".", b.fitness, b.type, "t=", b.totalDemerits, "-> @@", b.prevBreak and b.prevBreak.serial or "0") if node.sentinel then return node.sentinel end if node.type == "delta" then return "delta "..node.width.."pt" end local before = self.nodes[node.curBreak-1] @@ -582,7 +582,7 @@ function lineBreak:doBreak (nodes, hsize, sideways) end -- 889 while 1 do - if debugging then SU.debug("break", "@" .. self.pass .. "pass") end + if debugging then SU.debug("break", "@", self.pass, "pass") end if self.threshold > inf_bad then self.threshold = inf_bad end if self.pass == "second" then self.nodes = SILE.hyphenate(self.nodes) diff --git a/core/settings.lua b/core/settings.lua index 5f39b0569..eae985a7a 100644 --- a/core/settings.lua +++ b/core/settings.lua @@ -124,7 +124,7 @@ function settings:declare (spec) SU.deprecated("'name' argument of SILE.settings:declare", "'parameter' argument of SILE.settings:declare", "0.10.10", "0.11.0") end if self.declarations[spec.parameter] then - SU.debug("settings", "Attempt to re-declare setting: " .. spec.parameter) + SU.debug("settings", "Attempt to re-declare setting:", spec.parameter) return end self.declarations[spec.parameter] = spec diff --git a/core/utilities/init.lua b/core/utilities/init.lua index 00163de63..8e8d5a2e9 100644 --- a/core/utilities/init.lua +++ b/core/utilities/init.lua @@ -119,7 +119,7 @@ utilities.debug = function (category, ...) for i, input in ipairs(inputs) do if type(input) == "function" then local status, output = pcall(input) - inputs[i] = status and tostring(output) or SU.warn(("Output of %s debug function was an error: %s"):format(category, output)) + inputs[i] = status and output or SU.warn(("Output of %s debug function was an error: %s"):format(category, output)) elseif type(input) ~= "string" then inputs[i] = tostring(input) end diff --git a/packages/math/base-elements.lua b/packages/math/base-elements.lua index 61a558235..fc0f517f7 100644 --- a/packages/math/base-elements.lua +++ b/packages/math/base-elements.lua @@ -955,7 +955,7 @@ function elements.text:stretchyReshape (depth, height) m = diff end end - SU.debug("math", "stretch: closestI =", tostring(closestI)) + SU.debug("math", "stretch: closestI =", closestI) if closest then -- Now we have to re-shape the glyph chain. We will assume there -- is only one glyph. diff --git a/pagebuilders/grid.lua b/pagebuilders/grid.lua index 1659761ec..89bbcf803 100644 --- a/pagebuilders/grid.lua +++ b/pagebuilders/grid.lua @@ -16,7 +16,7 @@ function pagebuilder.findBestBreak (_, options) SU.debug("pagebuilder", "Page builder for frame", SILE.typesetter.frame.id, "called with", #vboxlist, "nodes,", target) if SU.debugging("vboxes") then for j, box in ipairs(vboxlist) do - SU.debug("vboxes", (j == i and " >" or " ") .. j .. ": " .. box) + SU.debug("vboxes", function () return (j == i and " >" or " ") .. j .. ": " .. box end) end end while i < #vboxlist do diff --git a/typesetters/firstfit.lua b/typesetters/firstfit.lua index 8844f37cc..62fc7a382 100644 --- a/typesetters/firstfit.lua +++ b/typesetters/firstfit.lua @@ -8,16 +8,16 @@ function typesetter:breakIntoLines (nl, breakWidth) local length = SILE.types.length() for i = 1,#nl do local n = nl[i] if n.is_box then - SU.debug("break", n .. " " .. tostring(n:lineContribution())) + SU.debug("break", n, function () return n:lineContribution() end) length = length + n:lineContribution() - SU.debug("break", " Length now " .. tostring(length) .. " breakwidth ".. tostring(breakWidth)) + SU.debug("break", " Length now ", length, "breakwidth", breakWidth) end if not n.is_box or n.isHangable then SU.debug("break", n ) if n.is_glue then length = length + n.width:absolute() end - SU.debug("break", " Length now " .. tostring(length) .. " breakwidth " .. tostring(breakWidth)) + SU.debug("break", " Length now ", length, " breakwidth ", breakWidth) -- Can we break? if length:tonumber() >= breakWidth:tonumber() then SU.debug("break", "Breaking!")