Skip to content

perf: reduce allocations in the printer and string-list sorting#1476

Open
jbedard wants to merge 1 commit into
bazelbuild:mainfrom
jbedard:no-copy-when-no-nil
Open

perf: reduce allocations in the printer and string-list sorting#1476
jbedard wants to merge 1 commit into
bazelbuild:mainfrom
jbedard:no-copy-when-no-nil

Conversation

@jbedard

@jbedard jbedard commented Jun 10, 2026

Copy link
Copy Markdown

Buildtools PR checklist

  • The code in this PR is covered by unit/integration tests.
  • I have tested these changes and provide testing instructions below.
  • I have either responded to, or resolved all Gemini comments on the PR.
  • I have read Google Eng Practices on Small Changes, this PR either follows these guidelines or the description provides reasoning for why they can not be followed.

Description

Reduce array copying to only when necessary, which seems like the exception and not the norm.

@jbedard jbedard requested a review from a team as a code owner June 10, 2026 04:00
@jbedard jbedard requested review from oreflow and removed request for a team June 10, 2026 04:00

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request optimizes the seq method in build/print.go by only copying the argument slice when a nil element is present. The reviewer suggested a further optimization to avoid a full second pass over the slice by copying the non-nil prefix and iterating only over the remaining elements.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread build/print.go
Comment on lines +1036 to 1048
args := list
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)
if x == nil {
filtered := make([]Expr, 0, len(*list))
for _, y := range *list {
if y != nil {
filtered = append(filtered, y)
}
}
args = &filtered
break
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When a nil element is found, we can optimize the filtering process by avoiding a full second pass over the entire slice. Instead, we can create the filtered slice with a length equal to the index i of the first nil element, use the highly optimized built-in copy function to copy the non-nil prefix, and then only loop over the remaining elements starting from i+1. Ensure that this optimization is consistent with the previous behavior and does not introduce any unintended side effects.

Suggested change
args := list
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)
if x == nil {
filtered := make([]Expr, 0, len(*list))
for _, y := range *list {
if y != nil {
filtered = append(filtered, y)
}
}
args = &filtered
break
}
}
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
}
}
References
  1. When modifying code, ensure that changes are consistent with previous behavior, especially if the previous behavior was intentional.

@jbedard jbedard force-pushed the no-copy-when-no-nil branch from 1216415 to b0c00d5 Compare June 25, 2026 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant