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
sync: Pool.Get is half the speed when uncontended #31820
Comments
For the record, this is the main issue that's stopping me using a slightly more sophisticated algorithm in |
CC @aclements |
These results actually indicate that sync.Pool is scaling basically perfectly and saturating the CPU pipeline (assuming you have two 2-way hyperthreaded cores). The way parallel benchmark results are reported is frustratingly confusing. It's not reporting CPU-ns/op, it's reporting wall-ns/op. For example, suppose each op takes exactly 100 ns, regardless of parallelism. If the single-threaded benchmark runs for 1 sec, it will execute 10,000,000 ops, so ns/op = 1s/10,000,000 ops = 100 ns/op. But if the same benchmark runs 4-way parallel for 1 sec, it will execute 40,000,000 ops, so ns/op = 1s/40,000,000 = 25 ns/op. (I really wish it didn't work this way...) Your results only differ by a factor of 2 rather than 4 because of hyperthreading. Since the benchmark is highly predictable and obviously stays within the cache, there are probably basically no pipeline stalls (though I didn't measure this to confirm), so you really only get 2x parallelism. For example, when I run with -cpu 1,4 on a six core 2-way hyperthreaded machine I get:
4.70 * 4 = 18.8, indicating perfect scalability. I'm going to go ahead and close this, but feel free to follow up. |
OK, I wondered if it was something like that. So "op" in this case does not mean "1 operation that I'm testing takes time $T", but "1 operation that I'm testing takes time ($T * $NCPU)", so the numbers in different That is definitely somewhat confusing. Is it documented somewhere? (I scanned through |
Thanks for the quick response BTW. |
That's right. I don't think this is documented anywhere. I've opened #31884 about the documentation issue. |
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
With this program, run
go test -count 5 -bench . -cpu 1,4 -benchmem
.Running -
What did you expect to see?
I'd expect to see that Pool.Get has roughly the same overhead when called in the single-threaded case as when called in parallel.
What did you see instead?
The results seem to indicate a significant overhead when called single threaded, which is unexpected to me.
The text was updated successfully, but these errors were encountered: