From bfc54b97b1ae0e5cc3b8032a0558dbc8079d5d52 Mon Sep 17 00:00:00 2001 From: delthas Date: Fri, 18 Feb 2022 17:16:22 +0100 Subject: 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. --- ui/style.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'ui') diff --git a/ui/style.go b/ui/style.go index 91cd2e1..4552020 100644 --- a/ui/style.go +++ b/ui/style.go @@ -120,6 +120,11 @@ func (s StyledString) Truncate(w int, tail StyledString) StyledString { var urlRegex = xurls.Relaxed() func (s StyledString) ParseURLs() StyledString { + if !strings.ContainsRune(s.string, '.') { + // fast path: no dot means no URL + return s + } + styles := make([]rangedStyle, 0, len(s.styles)) urls := urlRegex.FindAllStringIndex(s.string, -1) -- cgit v1.2.3