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

Add fast path for noopTransformer #37

Merged
merged 1 commit into from
Apr 29, 2022
Merged

Conversation

lithammer
Copy link
Owner

The transform.Nop makes a copy of the input causing allocations. We optimise this by creating our own nop transformer that does nothing and give it a fast path that just returns the original string.

This means that the regular Match(source, target) case we go from 2 to 0 allocations which comes with quite significant speed improvements.

BenchmarkMatch-8           6939124   170.5 ns/op  512 B/op      2 allocs/op
BenchmarkMatchBigLate-8    621099     1796 ns/op  512 B/op      2 allocs/op
BenchmarkMatchBigEarly-8   2873862   413.7 ns/op  512 B/op      2 allocs/op
BenchmarkMatch-8           58240472  20.66 ns/op    0 B/op      0 allocs/op
BenchmarkMatchBigLate-8    896416     1298 ns/op    0 B/op      0 allocs/op
BenchmarkMatchBigEarly-8   72513230  16.62 ns/op    0 B/op      0 allocs/op

Fixes #36

The `transform.Nop` makes a copy of the input causing allocations. We
optimise this by creating our own nop transformer that does nothing and
give it a fast path that just returns the original string.

This means that the regular `Match(source, target)` case we go from 2 to
0 allocations which comes with quite significant speed improvements.

v1.1.3:

BenchmarkMatch-8           6939124   170.5 ns/op  512 B/op      2 allocs/op
BenchmarkMatchBigLate-8    621099     1796 ns/op  512 B/op      2 allocs/op
BenchmarkMatchBigEarly-8   2873862   413.7 ns/op  512 B/op      2 allocs/op

HEAD:

BenchmarkMatch-8           58240472  20.66 ns/op    0 B/op      0 allocs/op
BenchmarkMatchBigLate-8    896416     1298 ns/op    0 B/op      0 allocs/op
BenchmarkMatchBigEarly-8   72513230  16.62 ns/op    0 B/op      0 allocs/op
@lithammer
Copy link
Owner Author

@harrison3000 👆

@lithammer lithammer merged commit 9b4074e into master Apr 29, 2022
@lithammer lithammer deleted the no-alloc-transformer branch April 29, 2022 08:27
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.

Unecessary alocations due to transformer.Nop
1 participant