On Sunday, June 6, 2021 at 8:35:02 AM UTC-4 Paul S. R. Chisholm wrote: > For example, could this code: > > func BenchmarkPopCountAlive(b *testing.B) { > sum = 0 > for i := 0; i < b.N; i++ { > sum += PopCount(0x1234567890abcdef) > } > } > > hypothetically be optimized to: > > func BenchmarkPopCountAlive(b *testing.B) { > sum = PopCount(0x1234567890abcdef) * b.N > } > > since PopCount() always returns the same value for the same argument? It > probably wouldn't, since that would break many existing benchmarks. >
We're talking about adding that very optimization, and I estimate the chance of it appearing in the next 2 years is above 50%. Preserving benchmark performance is an anti-goal for people working on general-purpose (*) compilers and runtimes. (*) crypto and real-time are special-purpose. As to "why", I know of at least three reasons: - hoisting invariant expressions out of loops is one of those mechanical things that computers do pretty well, and Go's package layering makes it easy to gather "pure function" information. - we're getting more and more Go programmers, doing more and more diverse things, they won't all be interested in learning hand-optimization tricks. - when we turn on generics, and as more things are written generically, generic-expansion will remove opportunities to hand-optimize code in the usual way So we are *definitely* thinking about this. As to what to do for your benchmarks, I am not sure. One obvious answer is "write them more carefully", but this does somewhat subvert the idea that it's easy to write benchmarks in Go. We might also provide more guidance, as in "a benchmark must do useful work that is consumed or checked" and then provide examples of the same. It's not just the Go compiler, either -- if a loop-invariant input causes code to take exactly the same paths over and over again, hardware branch predictors will do a better-than-usual job. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/35975862-cfee-4fe5-ad99-70eb83154c1en%40googlegroups.com.