Skip to content

improve getHashCode by removing fmt.Sprintf#111

Merged
zhengchun merged 2 commits into
antchfx:masterfrom
wjc4:master
Apr 5, 2025
Merged

improve getHashCode by removing fmt.Sprintf#111
zhengchun merged 2 commits into
antchfx:masterfrom
wjc4:master

Conversation

@wjc4

@wjc4 wjc4 commented Apr 5, 2025

Copy link
Copy Markdown
Contributor

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