commit c72f7fd

delthas  ·  2022-04-15 19:06:08 +0000 UTC
parent 717ab5a
Optimize URL regex

Following a quick local profiling & benchmark, matching links with
regexes is by far (>50x) the most time-consuming processing we do,
especially on start.

Switching to a URL regex with any scheme is faster by a factor of ~x8,
probably because there are many less "or" cases between a list of 100+
schemes, instead using a simple character class.
1 files changed,  +1, -1
+1, -1
1@@ -118,7 +118,7 @@ func (s StyledString) Truncate(w int, tail StyledString) StyledString {
2 	return sb.StyledString()
3 }
4 
5-var urlRegex = xurls.Relaxed()
6+var urlRegex, _ = xurls.StrictMatchingScheme(xurls.AnyScheme)
7 
8 func (s StyledString) ParseURLs() StyledString {
9 	if !strings.ContainsRune(s.string, '.') {