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
This commit is contained in:
Wei-Lun Li authored and GitHub committed 2026-11-22 10:06:24 +00:00
1 parent 890effbc91
commit 7d1bc574f6
2 files changed
+9

No files matched your search

+3
View File
@@ -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
+6
View File
@@ -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