commit bfc54b9

delthas  ·  2022-02-18 16:16:22 +0000 UTC
parent d0f189b
Optimize URL parsing performance

According to a CPU profiling I meade, the regex applied on each incoming
message took a substantial part of the CPU time. The slowdown it caused
was noticable at startup.

This optimizes the URL parsing by eliminating fast-path cases where no
dot appears to avoid parsing the line with a regex in those cases.
1 files changed,  +5, -0
+5, -0
 1@@ -120,6 +120,11 @@ func (s StyledString) Truncate(w int, tail StyledString) StyledString {
 2 var urlRegex = xurls.Relaxed()
 3 
 4 func (s StyledString) ParseURLs() StyledString {
 5+	if !strings.ContainsRune(s.string, '.') {
 6+		// fast path: no dot means no URL
 7+		return s
 8+	}
 9+
10 	styles := make([]rangedStyle, 0, len(s.styles))
11 
12 	urls := urlRegex.FindAllStringIndex(s.string, -1)