diff options
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
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.
Diffstat (limited to 'custom/extensions')
-rw-r--r--custom/extensions/email-gravatar.lua16
-rw-r--r--custom/extensions/email-libravatar.lua16
2 files changed, 30 insertions, 2 deletions
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