Skip to content

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

Closed
@benhoyt

Description

@benhoyt

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions