From a9496526f8f8734aab5b0a3c26eb040415ca736c Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Tue, 10 Nov 2026 14:11:39 +0000 Subject: [PATCH] Compile structpath reference regexp on first use The Unicode letter/number classes in pureReference make it expensive to compile (~0.3 ms), and it was compiled on every CLI invocation. Wrap it in sync.OnceValue. Co-authored-by: Isaac --- libs/structs/structpath/path.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libs/structs/structpath/path.go b/libs/structs/structpath/path.go index adf2be51331..cae058b66fe 100644 --- a/libs/structs/structpath/path.go +++ b/libs/structs/structpath/path.go @@ -6,6 +6,7 @@ import ( "regexp" "strconv" "strings" + "sync" ) // baseVarDef matches a path segment of a ${...} reference. It is the grammar of @@ -14,7 +15,9 @@ import ( const baseVarDef = `_*\p{L}+([-_]*[\p{L}\p{N}]+)*` // pureReference matches a string that is a single ${...} reference. -var pureReference = regexp.MustCompile(`^\$\{(` + baseVarDef + `(\.` + baseVarDef + `(\[[0-9]+\])*)*(\[[0-9]+\])*)\}$`) +var pureReference = sync.OnceValue(func() *regexp.Regexp { + return regexp.MustCompile(`^\$\{(` + baseVarDef + `(\.` + baseVarDef + `(\[[0-9]+\])*)*(\[[0-9]+\])*)\}$`) +}) const ( // Encodes wildcard after a dot: foo.* @@ -714,7 +717,7 @@ func isValidField(s string) bool { // PureReferenceToPath returns a PathNode if s is a pure variable reference, otherwise false. // This function is similar to dynvar.PureReferenceToPath but returns a *PathNode instead of dyn.Path. func PureReferenceToPath(s string) (*PathNode, bool) { - m := pureReference.FindStringSubmatch(s) + m := pureReference().FindStringSubmatch(s) if m == nil { return nil, false }