Skip to content

Commit 8d3591d

Browse files
authored
Reduce ExtractWorkflowNameFromFile overhead in the title-scan path (#31598)
1 parent d130cf2 commit 8d3591d

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

pkg/cli/commands_utils_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package cli
44

55
import (
6+
"bufio"
67
"os"
78
"path/filepath"
89
"strings"
@@ -113,6 +114,25 @@ func TestExtractWorkflowNameFromFile_NonExistentFile(t *testing.T) {
113114
}
114115
}
115116

117+
func TestExtractWorkflowNameFromFile_LargeFrontmatterLine(t *testing.T) {
118+
tmpDir := testutil.TempDir(t, "test-*")
119+
filePath := filepath.Join(tmpDir, "large-frontmatter.md")
120+
content := "---\nblob: " + strings.Repeat("x", bufio.MaxScanTokenSize+1) + "\n---\n\n# Large Frontmatter Workflow\n"
121+
122+
err := os.WriteFile(filePath, []byte(content), 0644)
123+
if err != nil {
124+
t.Fatalf("Failed to create test file: %v", err)
125+
}
126+
127+
result, err := extractWorkflowNameFromFile(filePath)
128+
if err != nil {
129+
t.Fatalf("Unexpected error: %v", err)
130+
}
131+
if result != "Large Frontmatter Workflow" {
132+
t.Fatalf("Expected %q, got %q", "Large Frontmatter Workflow", result)
133+
}
134+
}
135+
116136
func TestIsGitRepo(t *testing.T) {
117137
// Test in current directory (should be a git repo based on project setup)
118138
result := isGitRepo()

pkg/cli/workflows.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"path/filepath"
1212
"strconv"
1313
"strings"
14+
"sync"
1415

1516
"github.com/github/gh-aw/pkg/console"
1617
"github.com/github/gh-aw/pkg/constants"
@@ -23,6 +24,14 @@ import (
2324

2425
var workflowsLog = logger.New("cli:workflows")
2526

27+
const workflowTitleScannerBufferSize = 4 * 1024
28+
29+
var workflowTitleScannerBufferPool = sync.Pool{
30+
New: func() any {
31+
return make([]byte, workflowTitleScannerBufferSize)
32+
},
33+
}
34+
2635
func getWorkflowsDir() string {
2736
return ".github/workflows"
2837
}
@@ -335,11 +344,16 @@ func filterMarkdownFilesWithFrontmatter(mdFiles []string) ([]string, error) {
335344
// Returns an error if frontmatter is opened but never closed.
336345
func fastParseTitleFromReader(r io.Reader) (string, error) {
337346
scanner := bufio.NewScanner(r)
338-
// Use a small initial buffer (4 KB, matching the default scanner allocation)
339-
// but allow growth up to 1 MB to handle files with large frontmatter values
340-
// or long base64-encoded lines. Keeping the initial size small avoids a
341-
// costly 64 KB heap allocation on every call.
342-
scanner.Buffer(make([]byte, 4*1024), 1024*1024)
347+
// Reuse the small initial scanner buffer across calls while still allowing
348+
// growth up to 1 MB for large frontmatter values or long base64-encoded lines.
349+
scannerBuffer := workflowTitleScannerBufferPool.Get().([]byte)
350+
if cap(scannerBuffer) != workflowTitleScannerBufferSize {
351+
scannerBuffer = make([]byte, workflowTitleScannerBufferSize)
352+
} else {
353+
scannerBuffer = scannerBuffer[:workflowTitleScannerBufferSize]
354+
}
355+
defer workflowTitleScannerBufferPool.Put(scannerBuffer)
356+
scanner.Buffer(scannerBuffer, 1024*1024)
343357
firstLine := true
344358
inFrontmatter := false
345359
for scanner.Scan() {

0 commit comments

Comments
 (0)