Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bytes, strings: speed up TrimSpace 4-5x for common ASCII cases #29122

Closed
benhoyt opened this issue Dec 6, 2018 · 2 comments
Closed

bytes, strings: speed up TrimSpace 4-5x for common ASCII cases #29122

benhoyt opened this issue Dec 6, 2018 · 2 comments

Comments

@benhoyt
Copy link
Contributor

benhoyt commented Dec 6, 2018

Per thread on golang-dev, I recently found that strings.TrimSpace is quite slow on a lot of common inputs, because it doesn't have a fast path for ASCII strings.

Adding a fast path for ASCII speeds it up 4-5x for ASCII cases, speeds it up somewhat for partially-ASCII cases, and doesn't (noticeably) slow it down for all-non-ASCII cases. So a pretty nice win for heavy users of TrimSpace like GoAWK. :-)

Thanks @josharian for helping me simplify and improve my initial version, getting it from 2-3x as fast to 4-5x as fast. The change is at: https://go-review.googlesource.com/c/go/+/152917

This is similar to #17856, where a similar ASCII fast path was introduced for strings.Fields and bytes.Fields.

For the record, I'm using go version go1.11.2 darwin/amd64.

@gopherbot
Copy link

Change https://golang.org/cl/152917 mentions this issue: Speed up TrimSpace 4-5x for common ASCII cases

@bcmills bcmills added this to the Go1.13 milestone Dec 6, 2018
@gopherbot
Copy link

Change https://golang.org/cl/169518 mentions this issue: bytes: Fix regression so TrimSpace again returns nil on all-space input

gopherbot pushed a commit that referenced this issue Mar 27, 2019
Issue #29122 introduced a subtle regression due to the way that
TrimFuncLeft is written: previously TrimSpace returned nil when given
an input of all whitespace, but with the #29122 changes it returned an
empty slice on all-space input.

This change adds a special case to the new, optimized TrimSpace to go
back to that behavior. While it is odd behavior and people shouldn't be
relying on these functions returning a nil slice in practice, it's not
worth the breakage of code that does.

This tweak doesn't change the TrimSpace benchmarks significantly.

Fixes #31038

Change-Id: Idb495d02b474054d2b2f593c2e318a7a6625688a
Reviewed-on: https://go-review.googlesource.com/c/go/+/169518
Reviewed-by: Ian Lance Taylor <iant@golang.org>
@golang golang locked and limited conversation to collaborators Mar 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants