From a5eaa2fef402660414fa2d566530ed0c3939f8a2 Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Tue, 9 Jun 2026 20:30:22 -0700 Subject: [PATCH] perf: reduce allocations in the printer and string-list sorting --- build/print.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/build/print.go b/build/print.go index a3b416bb5..2d909cfd0 100644 --- a/build/print.go +++ b/build/print.go @@ -1032,12 +1032,19 @@ func (p *printer) useCompactMode(start *Position, list *[]Expr, end *End, mode s // If multiLine is true, seq avoids the compact form even // for 0- and 1-element sequences. func (p *printer) seq(brack string, start *Position, list *[]Expr, end *End, mode seqMode, forceCompact, forceMultiLine bool) { - args := &[]Expr{} - for _, x := range *list { - // nil arguments may be added by some linter checks, filter them out because - // they may cause NPE. - if x != nil { - *args = append(*args, x) + // Filter out nil arguments (rare; added by some linter checks) that may cause a NPE, copying only if needed. + args := list + for i, x := range *list { + if x == nil { + filtered := make([]Expr, i, len(*list)) + copy(filtered, (*list)[:i]) + for _, y := range (*list)[i+1:] { + if y != nil { + filtered = append(filtered, y) + } + } + args = &filtered + break } }