gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
74.78k stars 7.46k forks source link

Maybe cache the results of reflect.Type.MethodByName? #9386

Open quasilyte opened 2 years ago

quasilyte commented 2 years ago

See https://github.com/golang/go/issues/50617

tl;dr: hugo happens to call MethodByName a lot for types with many exported methods (120).

It could be beneficial to cache the MethodByName results to avoid this pathological performance regression. Using the binary search makes big sites (https://github.com/GSA/digitalgov.gov in this case) build ~20% faster, but there is a chance that this optimization is not worthwhile in the Go stdlib itself.

With caching, it should be possible to reduce the overhead and get the speedup that is comparable to that.

Since I don't know hugo code at all, it's hard for me to give any other suggestions.

How to reproduce the behavior:

  1. Build hugo from sources
  2. Clone https://github.com/GSA/digitalgov.gov
  3. Run hugo in the cloned repo root (with CPU profiling enabled)
git clone https://github.com/GSA/digitalgov.gov.git
cd digitalgov.gov
hugo --profile-cpu cpu.out

Note that there will be a few warnings related to the usages of getenv(), but they can be fixed rather easily.

bep commented 2 years ago

Thanks for those numbers, I will have a look to see how to do it.