Skip to content

feat: use strings.Replacer #150

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

Merged
merged 1 commit into from
Oct 19, 2018
Merged

feat: use strings.Replacer #150

merged 1 commit into from
Oct 19, 2018

Conversation

orisano
Copy link
Contributor

@orisano orisano commented Oct 19, 2018

Signed-off-by: Nao YONASHIRO [email protected]

improve performance.

name               old time/op  new time/op  delta
Create-4           13.1µs ± 2%   8.7µs ±10%  -33.99%  (p=0.000 n=9+10)
CreateBuildInfo-4   920ns ± 1%   422ns ±10%  -54.12%  (p=0.000 n=9+10)

Signed-off-by: Nao YONASHIRO <[email protected]>
@beorn7
Copy link
Member

beorn7 commented Oct 19, 2018

The problem with the strings.Replacer is that it causes allocs, see https://golang.org/src/strings/strings.go?s=22223:22269#L840

The runtime gain is significant, but allocations can bite us in different ways.

I think, however, most of the speed gain happens because most strings don't need escaping at all and could just be copied directly. Thus, we could try to use the same strategy as in strings.Replacer, i.e. first check if escaping is needed at all, and if not, just call w.WriteString directly. That should have an even better effect than the one in this PR.

@beorn7
Copy link
Member

beorn7 commented Oct 19, 2018

Sorry, misread Replace vs. Replacer.

I now think this is doing exactly the right thing. Will just test for allocs to be sure onec I'm at my development computer.

@beorn7
Copy link
Member

beorn7 commented Oct 19, 2018

Confirmed allocation-free-ness. This is exactly what I was looking for but failed to spot in the scrings package. Many thanks for this.

Compared to the original benchmark in #148, this is a reduction in runtime of 54% for Create and 67% for CreateBuildInfo`. Really nice.

@beorn7 beorn7 merged commit 16b4535 into prometheus:master Oct 19, 2018
alanprot pushed a commit to alanprot/common that referenced this pull request Mar 15, 2023
server: use -server.path-prefix for metrics and debug endpoints
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.

2 participants