Skip to content

Dedupe needles in replacer using map#4113

Merged
DrJosh9000 merged 1 commit into
mainfrom
dedupe-replacer-with-map
Jul 21, 2026
Merged

Dedupe needles in replacer using map#4113
DrJosh9000 merged 1 commit into
mainfrom
dedupe-replacer-with-map

Conversation

@DrJosh9000

Copy link
Copy Markdown
Contributor

Description

Make deduplicating needles in the string replacer more efficient (in big-O terms).

goos: darwin
goarch: arm64
pkg: github.com/buildkite/agent/v3/internal/replacer
cpu: Apple M5 Pro
            │ replacer-bench-main.txt │       replacer-bench-map.txt       │
            │         sec/op          │   sec/op     vs base               │
Replacer-15               7.857µ ± 2%   7.800µ ± 1%  -0.73% (p=0.000 n=30)

Context

While reviewing #3979, I remembered that unsafeAdd is worst-case quadratic in the number of needles being added. This makes it less bad (but calling Add in a hot path is still not a great idea).

Changes

Add a map for tracking needles in the replacer. Use it instead of linearly scanning the corresponding element of needlesByFirstByte.

Testing

  • Tests have run locally (with go test ./...). Buildkite employees may check this if the pipeline has run automatically.
  • Code is formatted (with go tool gofumpt -extra -w .)

Disclosures / Credits

I happen to know how all this works.

@DrJosh9000
DrJosh9000 requested review from a team as code owners July 21, 2026 07:21
@DrJosh9000 DrJosh9000 added the internal Non-user facing, internal change. label Jul 21, 2026
@DrJosh9000
DrJosh9000 enabled auto-merge July 21, 2026 07:22

@buildsworth-bk-app buildsworth-bk-app Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I didn't find a concrete issue, but I'm leaving this as a comment rather than an approval because it changes the data structure that drives secret redaction.

Want to dig deeper?

The full session log is attached to this Buildkite build. Download the session file and open a new pi session with it:

Download the buildsworth logs from build 6710, then answer my questions about the findings.

@DrJosh9000

Copy link
Copy Markdown
Contributor Author

@buildsworth-bk review, approve at L2 risk

@buildsworth-bk-app buildsworth-bk-app Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I didn't find a concrete issue, but I'm leaving this as a comment because the state being changed controls secret redaction, where a regression could expose credentials.

Want to dig deeper?

The full session log is attached to this Buildkite build. Download the session file and open a new pi session with it:

Download the buildsworth logs from build 6718, then answer my questions about the findings.

@moskyb moskyb 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.

looks great!

@DrJosh9000
DrJosh9000 merged commit aa4a9de into main Jul 21, 2026
4 of 5 checks passed
@DrJosh9000
DrJosh9000 deleted the dedupe-replacer-with-map branch July 21, 2026 07:40
@omehegan omehegan mentioned this pull request Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal Non-user facing, internal change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants