Skip to content

Commit b0c00d5

Browse files
committed
perf: reduce allocations in the printer and string-list sorting
1 parent 77b9b38 commit b0c00d5

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

build/print.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,12 +1032,19 @@ func (p *printer) useCompactMode(start *Position, list *[]Expr, end *End, mode s
10321032
// If multiLine is true, seq avoids the compact form even
10331033
// for 0- and 1-element sequences.
10341034
func (p *printer) seq(brack string, start *Position, list *[]Expr, end *End, mode seqMode, forceCompact, forceMultiLine bool) {
1035-
args := &[]Expr{}
1036-
for _, x := range *list {
1037-
// nil arguments may be added by some linter checks, filter them out because
1038-
// they may cause NPE.
1039-
if x != nil {
1040-
*args = append(*args, x)
1035+
// Filter out nil arguments (rare; added by some linter checks) that may cause a NPE, copying only if needed.
1036+
args := list
1037+
for i, x := range *list {
1038+
if x == nil {
1039+
filtered := make([]Expr, i, len(*list))
1040+
copy(filtered, (*list)[:i])
1041+
for _, y := range (*list)[i+1:] {
1042+
if y != nil {
1043+
filtered = append(filtered, y)
1044+
}
1045+
}
1046+
args = &filtered
1047+
break
10411048
}
10421049
}
10431050

0 commit comments

Comments
 (0)