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

optimize slackutilsx.EscapeMessage function #1041

Merged
merged 1 commit into from Mar 11, 2022

Conversation

brainexe
Copy link
Contributor

@brainexe brainexe commented Mar 9, 2022

This change is it performance related....but quite small TBH. Just found if while checking some code.
The underlaying string.Replacer is optimized to buildup the lookup tables only once (but only if really needed!).

➜  slackutilsx $ go test -bench=. --benchmem               
goos: linux
goarch: amd64
pkg: github.com/slack-go/slack/slackutilsx
cpu: Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz
BenchmarkEscapeMessage-8      	  991929	      1230 ns/op	    6792 B/op	       9 allocs/op
BenchmarkEscapeMessageNew-8   	22310755	        58.94 ns/op	      32 B/op	       2 allocs/op

-> ~20 times faster, ~200 times less allocated bytes

@zchee zchee requested review from zchee and kanata2 March 11, 2022 05:34
@zchee zchee added the optimize label Mar 11, 2022
@zchee
Copy link
Contributor

zchee commented Mar 11, 2022

@brainexe Thanks contribute and your change very optimized text replacer :D

Actually, I had to compare using benchstat, below is the result:

name              old time/op    new time/op    delta
EscapeMessage-20    1.54µs ± 1%    0.05µs ± 4%  -96.70%  (p=0.008 n=5+5)

name              old alloc/op   new alloc/op   delta
EscapeMessage-20    6.79kB ± 0%    0.03kB ± 0%  -99.53%  (p=0.008 n=5+5)

name              old allocs/op  new allocs/op  delta
EscapeMessage-20      9.00 ± 0%      2.00 ± 0%  -77.78%  (p=0.008 n=5+5)

That's would be great. I'll merge soon.

@zchee zchee merged commit 6ae6da1 into slack-go:master Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants