Skip to content

improve getHashCode by removing fmt.Sprintf #111

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 2 commits into from
Apr 5, 2025
Merged

Conversation

wjc4
Copy link
Contributor

@wjc4 wjc4 commented Apr 5, 2025

I'm using this library to perform quite a bit of ancestor query but is hitting high cpu usage.
did profiling and found out that getHashCode is rather expensive.

image

did some further research and found out that sprintf can be expensive as shown in the profiling results
https://medium.com/swlh/bad-go-frivolous-sprintf-2ad28fedf1a0
https://hackernoon.com/fmtsprintf-looks-simple-but-will-burn-a-hole-in-your-pocket

Before

Benchmark

goos: darwin
goarch: arm64
pkg: github.com/antchfx/xpath
cpu: Apple M3 Pro
=== RUN   Benchmark_GetHashCode
Benchmark_GetHashCode
=== RUN   Benchmark_GetHashCode/getHashCode
Benchmark_GetHashCode/getHashCode
Benchmark_GetHashCode/getHashCode-12             2976260               402.4 ns/op           144 B/op         10 allocs/op
PASS
ok      github.com/antchfx/xpath        2.555s

After

Benchmark

goos: darwin
goarch: arm64
pkg: github.com/antchfx/xpath
cpu: Apple M3 Pro
=== RUN   Benchmark_GetHashCode
Benchmark_GetHashCode
=== RUN   Benchmark_GetHashCode/getHashCode
Benchmark_GetHashCode/getHashCode
Benchmark_GetHashCode/getHashCode-12             9322789               122.1 ns/op            88 B/op          2 allocs/op
PASS
ok      github.com/antchfx/xpath        2.000s

@zhengchun zhengchun merged commit b9e198d into antchfx:master Apr 5, 2025
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