From fc75d90af73c021af228719bc5b4aa7eb9a6d189 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 14 Nov 2026 15:16:59 +0800 Subject: [PATCH] yaml.v3: its two regexps are compiled on first use, not at launch 0caa09b3d vendored gopkg.in/yaml.v3 into third_party/yaml.v3, inside magpie's tree, and TestNoEagerPackageRegexps, which walks the tree, has been red on main since: third_party/yaml.v3/encode.go:307:19: a package-level regexp is compiled at launch; use lazyre.New third_party/yaml.v3/resolve.go:124:22: (the same) yaml.v3 is its own module and can't import internal/lazyre, and its go.mod has no go line (Go 1.16 language, so no sync.OnceValue). base60float and yamlStyleFloat are now functions that compile their expression once behind a sync.Once; the patterns are as upstream's. MAGPIE.md lists this as the second change from v3.0.1. Seen red in the full suite of the OpenMausBot sessions change (mote, at 13cb07fde + that change), which doesn't touch yaml. Run (mote): gofmt -l clean; go vet in third_party/yaml.v3; go vet ./..., GOOS=linux -tags nogui and GOOS=windows go vet ./...; go test -count=1 -timeout 45m ./... at 59c5b59ea (on df2b819c6): 100 packages ok, TestNoEagerPackageRegexps among them. Red before: the two lines above, on 13cb07fde. After a rebase onto ea6470cb7 (01c3efc4f's library change and a GUI test): vet as above, and go test . ./internal/sessions ./internal/library ./internal/catalog ok. After another onto 79a30eb9c (edit/toml.go and provider/remote_follow.go perf): go vet ./..., go test . ./internal/sessions ./internal/edit ./internal/provider ok, at the pushed tree. Not run: upstream yaml.v3's own tests (not copied here, need gopkg.in/check.v1). --- third_party/yaml.v3/MAGPIE.md | 7 ++++++- third_party/yaml.v3/encode.go | 15 +++++++++++++-- third_party/yaml.v3/resolve.go | 15 +++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/third_party/yaml.v3/MAGPIE.md b/third_party/yaml.v3/MAGPIE.md index a32b5cfb0..5d1a0f716 100644 --- a/third_party/yaml.v3/MAGPIE.md +++ b/third_party/yaml.v3/MAGPIE.md @@ -1,7 +1,7 @@ # yaml.v3 v3.0.1, as magpie builds it gopkg.in/yaml.v3 v3.0.1 (MIT and Apache-2.0, LICENSE and NOTICE here), -its non-test files as released (gofmt run over them), with one change: the emitter's event +its non-test files as released (gofmt run over them), with two changes. The emitter's event queue starts again once every event in it is written (`yaml_emitter_emit` in emitterc.go). Upstream only appends to it, so an encode held every event of the document until it ended: ~24 MB of @@ -9,6 +9,11 @@ allocations when magpie writes dsh's model routes, on the first start of each version and on every catalog change. go.yaml.in/yaml/v3 v3.0.5 has the same queue. +And its two package-level regular expressions (base60float in encode.go, +yamlStyleFloat in resolve.go) are compiled the first time they are used, +behind a sync.Once, not when magpie starts: magpie's +TestNoEagerPackageRegexps holds every package in its tree to that. + go.mod here names the module only: upstream's requires gopkg.in/check.v1 for its tests, which are not copied. go.mod's replace points here. diff --git a/third_party/yaml.v3/encode.go b/third_party/yaml.v3/encode.go index de9e72a3e..d3762567c 100644 --- a/third_party/yaml.v3/encode.go +++ b/third_party/yaml.v3/encode.go @@ -24,6 +24,7 @@ import ( "sort" "strconv" "strings" + "sync" "time" "unicode/utf8" ) @@ -299,12 +300,22 @@ func isBase60Float(s string) (result bool) { return false } // Do the full match. - return base60float.MatchString(s) + return base60float().MatchString(s) } // From http://yaml.org/type/float.html, except the regular expression there // is bogus. In practice parsers do not enforce the "\.[0-9_]*" suffix. -var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`) +var ( + base60floatOnce sync.Once + base60floatRE *regexp.Regexp +) + +// base60float is compiled the first time it is used, not when a program +// using yaml starts. +func base60float() *regexp.Regexp { + base60floatOnce.Do(func() { base60floatRE = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`) }) + return base60floatRE +} // isOldBool returns whether s is bool notation as defined in YAML 1.1. // diff --git a/third_party/yaml.v3/resolve.go b/third_party/yaml.v3/resolve.go index 64ae88805..79c0601f0 100644 --- a/third_party/yaml.v3/resolve.go +++ b/third_party/yaml.v3/resolve.go @@ -21,6 +21,7 @@ import ( "regexp" "strconv" "strings" + "sync" "time" ) @@ -121,7 +122,17 @@ func resolvableTag(tag string) bool { return false } -var yamlStyleFloat = regexp.MustCompile(`^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$`) +var ( + yamlStyleFloatOnce sync.Once + yamlStyleFloatRE *regexp.Regexp +) + +// yamlStyleFloat is compiled the first time it is used, not when a program +// using yaml starts. +func yamlStyleFloat() *regexp.Regexp { + yamlStyleFloatOnce.Do(func() { yamlStyleFloatRE = regexp.MustCompile(`^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$`) }) + return yamlStyleFloatRE +} func resolve(tag string, in string) (rtag string, out interface{}) { tag = shortTag(tag) @@ -201,7 +212,7 @@ func resolve(tag string, in string) (rtag string, out interface{}) { if err == nil { return intTag, uintv } - if yamlStyleFloat.MatchString(plain) { + if yamlStyleFloat().MatchString(plain) { floatv, err := strconv.ParseFloat(plain, 64) if err == nil { return floatTag, floatv