From 303d8bbb5c5ca34079704a75ed9bfd862d6f3ce3 Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Tue, 2 Mar 2021 12:47:31 -0500 Subject: [PATCH] gopls/internal/hooks: compile URL regexp once The URL regexp is apparently enormous and we spend 13 CPU-seconds compiling it in the gopls version of the LSP tests. Change-Id: Id291ce0f104aa02b5e38aad43fff8647ce1b5b98 Reviewed-on: https://go-review.googlesource.com/c/tools/+/297876 Trust: Heschi Kreinick Run-TryBot: Heschi Kreinick gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Robert Findley --- gopls/internal/hooks/hooks.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/gopls/internal/hooks/hooks.go b/gopls/internal/hooks/hooks.go index 50e8f71f26..390967d5f6 100644 --- a/gopls/internal/hooks/hooks.go +++ b/gopls/internal/hooks/hooks.go @@ -21,16 +21,17 @@ func Options(options *source.Options) { if options.GoDiff { options.ComputeEdits = ComputeEdits } - options.URLRegexp = urlRegexp() + options.URLRegexp = relaxedFullWord options.GofumptFormat = func(ctx context.Context, src []byte) ([]byte, error) { return format.Source(src, format.Options{}) } updateAnalyzers(options) } -func urlRegexp() *regexp.Regexp { - // Ensure links are matched as full words, not anywhere. - re := regexp.MustCompile(`\b(` + xurls.Relaxed().String() + `)\b`) - re.Longest() - return re +var relaxedFullWord *regexp.Regexp + +// Ensure links are matched as full words, not anywhere. +func init() { + relaxedFullWord = regexp.MustCompile(`\b(` + xurls.Relaxed().String() + `)\b`) + relaxedFullWord.Longest() }