From 88665b241b21cdf72d3d59cc68e89ec7a58a0691 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 23 Mar 2026 16:21:49 +0000 Subject: [PATCH 1/3] Initial plan From 3d7220c56f495560e370069fbba4b308a0fe1b7e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 23 Mar 2026 16:37:55 +0000 Subject: [PATCH 2/3] fix: resolve performance regression in UnquoteYAMLKey by using single-pass regex replacement The UnquoteYAMLKey function was using ReplaceAllStringFunc with a callback that called re.FindStringSubmatch inside - performing two regex passes per match. This caused a ~103% performance regression in BenchmarkCompileMemoryUsage. Replace with ReplaceAllString using capture group references (${1}${2}key:) for a single-pass O(n) replacement, restoring performance from ~13ms back to ~7.6ms (near historical average of ~6.4ms). Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> Agent-Logs-Url: https://github.com/github/gh-aw/sessions/892822a4-0baa-4043-a447-cb4ead4ac131 --- pkg/workflow/yaml.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/pkg/workflow/yaml.go b/pkg/workflow/yaml.go index 05164eeed84..d7644742c6f 100644 --- a/pkg/workflow/yaml.go +++ b/pkg/workflow/yaml.go @@ -146,17 +146,9 @@ func UnquoteYAMLKey(yamlStr string, key string) string { re = regexp.MustCompile(pattern) unquoteYAMLKeyCache.Store(key, re) } - return re.ReplaceAllStringFunc(yamlStr, func(match string) string { - // Find the submatch groups - submatches := re.FindStringSubmatch(match) - if len(submatches) >= 3 { - // submatches[0] is the full match - // submatches[1] is the line start (^ or \n) - // submatches[2] is the whitespace - return submatches[1] + submatches[2] + key + ":" - } - return match - }) + // Use ReplaceAllString with capture group references for a single-pass replacement. + // ${1} = line start (^ or \n), ${2} = optional whitespace + return re.ReplaceAllString(yamlStr, "${1}${2}"+key+":") } // MarshalWithFieldOrder marshals a map to YAML with fields in a specific order. From 17d774ee9a4bc6b42b6387506b4331367e9f8b05 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 23 Mar 2026 16:44:29 +0000 Subject: [PATCH 3/3] test: add BenchmarkUnquoteYAMLKey to guard against double-pass regex regression Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> Agent-Logs-Url: https://github.com/github/gh-aw/sessions/82ab5ebf-b66a-483e-8ec9-4ab0426ce7bf --- pkg/workflow/yaml_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/workflow/yaml_test.go b/pkg/workflow/yaml_test.go index 622f404bf3c..68cd73bf31b 100644 --- a/pkg/workflow/yaml_test.go +++ b/pkg/workflow/yaml_test.go @@ -407,6 +407,36 @@ func TestExtractTopLevelYAMLSectionWithOrdering(t *testing.T) { } } +// BenchmarkUnquoteYAMLKey measures single-pass regex replacement performance. +// This benchmark guards against regressions where the implementation calls +// FindStringSubmatch inside a ReplaceAllStringFunc callback (double regex pass). +func BenchmarkUnquoteYAMLKey(b *testing.B) { + // A realistic workflow YAML with the "on" key quoted by the marshaler + yamlStr := `"on": + push: + branches: + - main + pull_request: + types: + - opened + - synchronize + issues: + types: + - opened + workflow_dispatch: +jobs: + agent: + runs-on: ubuntu-latest + steps: + - name: Run + run: echo hello +` + b.ReportAllocs() + for b.Loop() { + _ = UnquoteYAMLKey(yamlStr, "on") + } +} + func TestFormatYAMLValue(t *testing.T) { tests := []struct { name string