Skip to content

Avoid repeated string copies when stripping character runs - #919

Open
vinsymoyya wants to merge 1 commit into
google:masterfrom
vinsymoyya:perf/iterative-strip-chars
Open

vinsymoyya wants to merge 1 commit into
google:masterfrom
vinsymoyya:perf/iterative-strip-chars

Conversation

@vinsymoyya

Copy link
Copy Markdown

Fixes #918.

Scan matching prefix/suffix runes iteratively and copy the remaining text once. The recursive implementation rebuilt the entire remaining string after every removed character, causing quadratic copying. Keep the existing membership checks and behavior for Unicode, array-valued character sets, and empty inputs.

With the included benchmark copied onto the original production code, a 10,000-space run allocates about 269 MB/op; the fix allocates about 400 KB/op with the same Go 1.24.13 Linux/amd64 single-CPU setup. These are allocation measurements for this workload, not a whole-program speed claim.

Validation: functional regressions, BenchmarkStripCharsLongRuns, and full go test -p=1 -buildvcs=false ./... pass from the exact commit archive; gofmt and git diff --check pass. The test/benchmark file is registered in Bazel. Bazel and C/Python integration checks were not run locally.

This branch has not been deployed

No deployments
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.

Character stripping repeatedly copies the remaining string

1 participant