From 46ee28ce7fa59af4e67eee55b241d51af15cef4b Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Fri, 16 Oct 2026 06:29:21 -1000 Subject: Trim avatar addresses in linear time The trim pattern backtracked over a run of spaces inside an address, which a commit can carry, and a log page runs the filter for every row. Forty thousand spaces took two seconds per row. --- custom/extensions/email-gravatar.lua | 16 +++++++++++++++- custom/extensions/email-libravatar.lua | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) (limited to 'custom/extensions') diff --git a/custom/extensions/email-gravatar.lua b/custom/extensions/email-gravatar.lua index 6eb5376..01db3f9 100644 --- a/custom/extensions/email-gravatar.lua +++ b/custom/extensions/email-gravatar.lua @@ -58,6 +58,20 @@ local function hash_hex(input) return hex end +-- Lua patterns backtrack, so "^%s*(.-)%s*$" would cost the square of a long +-- run of spaces inside the address. Finding each end directly stays linear. +local function trim(text) + local first = text:find("%S") + if first == nil then + return "" + end + local last = #text + while text:find("^%s", last) do + last = last - 1 + end + return text:sub(first, last) +end + -- cgit can hand over the address still wrapped in angle brackets, and the -- service hashes the trimmed lowercase form, so an address that skipped this -- would hash to something the service has never heard of. A missing or empty @@ -70,7 +84,7 @@ local function normalize_email(email) if inner ~= nil then email = inner end - email = (email:gsub("^%s*(.-)%s*$", "%1")):lower() + email = trim(email):lower() if email == "" then return nil end diff --git a/custom/extensions/email-libravatar.lua b/custom/extensions/email-libravatar.lua index 9b3d595..1fb979d 100644 --- a/custom/extensions/email-libravatar.lua +++ b/custom/extensions/email-libravatar.lua @@ -56,6 +56,20 @@ local function hash_hex(input) return hex end +-- Lua patterns backtrack, so "^%s*(.-)%s*$" would cost the square of a long +-- run of spaces inside the address. Finding each end directly stays linear. +local function trim(text) + local first = text:find("%S") + if first == nil then + return "" + end + local last = #text + while text:find("^%s", last) do + last = last - 1 + end + return text:sub(first, last) +end + -- cgit can hand over the address still wrapped in angle brackets, and the -- service hashes the trimmed lowercase form, so an address that skipped this -- would hash to something the service has never heard of. A missing or empty @@ -68,7 +82,7 @@ local function normalize_email(email) if inner ~= nil then email = inner end - email = (email:gsub("^%s*(.-)%s*$", "%1")):lower() + email = trim(email):lower() if email == "" then return nil end -- cgit