cue/load: avoid repeatedly loading parent directories by caching
In order to load a cue package, the `cue.load.loader` performs various
CPU/IO heavy operations:
- readDir: in order to load a cue instance, all ancestors of the
instance directory are loaded as well, causing multiple dir
reads. In case multiple instances are loaded, such as in
`cue fmt ./...`, the same directory may be read many times as
we descend into subdirectories
- each file is loaded as a `*build.File` and its source parsed as
an `*ast.File`
We now maintain two separate caches of the above work:
- a cache of directory path to []*build.File
- a cache of file path to []*ast.File
After this change we can see the following speedup on my monorepo:
before:
cue fmt --check ./... 2.50s user 0.54s system 83% cpu 3.661 total
after:
cue fmt --check ./... 0.72s user 0.21s system 51% cpu 1.815 total
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: I95472ad22eb124df730f6711af730425106a85e8
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193678
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
authored by