From 7d1bc574f620e636d6ddee5b65e60fbc88be84c2 Mon Sep 17 00:00:00 2001 From: Wei-Lun Li Date: Sun, 22 Nov 2026 10:06:24 +0000 Subject: [PATCH] perf(markup): skip mention, email and commit regexps for text without "@" (#40471) Post-processing runs the mention, email and cross-repo commit reference regexps on every text node of rendered markdown, issue titles and commit messages. All three patterns need an `@`, but none starts with a literal the regexp engine can search for, so they took most of the post-processing time even on text without one. Checking for `@` first skips them with the same results. Local Go benchmarks, i7-9700 on Windows 11, base and patched run alternately 6 times, medians: | | before | after | |---|---|---| | Markdown render of README + CONTRIBUTING + 3 docs (45 KB) | 16.1 ms | 11.4 ms | | `PostProcessCommitMessageSubject` on the last 200 commit subjects of this repo | 5.14 ms | 3.53 ms | | `PostProcessIssueTitle` on the same 200 lines | 3.57 ms | 2.59 ms | Assisted-by: Claude Code:claude-opus-5-5 --- modules/markup/html_email.go | 3 +++ modules/references/references.go | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/modules/markup/html_email.go b/modules/markup/html_email.go index cf18e99d982..17cc56c3d7c 100644 --- a/modules/markup/html_email.go +++ b/modules/markup/html_email.go @@ -13,6 +13,9 @@ import ( func emailAddressProcessor(ctx *RenderContext, node *html.Node) { next := node.NextSibling for node != nil && node != next { + if !strings.Contains(node.Data, "@") { // the regexp is slow, skip texts which can't match + return + } m := globalVars().emailRegex.FindStringSubmatchIndex(node.Data) if m == nil { return diff --git a/modules/references/references.go b/modules/references/references.go index 22a534f33a9..1a69233f807 100644 --- a/modules/references/references.go +++ b/modules/references/references.go @@ -250,6 +250,9 @@ func FindAllMentionsBytes(content []byte) []RefSpan { // FindFirstMentionBytes matches the first mention in then given content // and returns the location of the unvalidated username, including the @ prefix. func FindFirstMentionBytes(content []byte) (bool, RefSpan) { + if bytes.IndexByte(content, '@') == -1 { // the regexp is slow, skip texts which can't match + return false, RefSpan{} + } mention := globalVars().mentionPattern.FindSubmatchIndex(content) if mention == nil { return false, RefSpan{} @@ -402,6 +405,9 @@ func FindRenderizableReferenceNumeric(content string, prOnly, crossLinkOnly bool // FindRenderizableCommitCrossReference returns the first unvalidated commit cross reference found in a string. func FindRenderizableCommitCrossReference(content string) (bool, *RenderizableReference) { + if !strings.Contains(content, "@") { // the regexp is slow, skip texts which can't match + return false, nil + } m := globalVars().crossReferenceCommitPattern.FindStringSubmatchIndex(content) if len(m) < 8 { return false, nil