env: optimize performance
Created by: keegancsmith
I was noticing running tests, but specifying no tests to run would take a second. I assumed this is due to us doing too much work in "func init". I found https://github.com/mvdan/benchinit and applied it to ./cmd/searcher out of interest. It found in that case that env is the main cause of init slowness.
This is unlikely the main cause, but it was fun to optimize anyways. Will have to properly investigate init startup by targetting a slow package. But this acheived a much faster Get implementation by:
- removing call to fmt.Sprintf if we have a defaultValue
- removing map insertions, instead build a list and validate uniqueness in Lock.
- remove updating expvar.Map. This involves a bunch of nasty locks. Instead we change the format of our expvar and return the list we build up.
This ended in the following results:
name old time/op new time/op delta
Init-8 1.15µs ± 0% 0.53µs ±28% -53.72% (p=0.016 n=4+5)
name old alloc/op new alloc/op delta
Init-8 384B ± 0% 604B ±22% +57.19% (p=0.008 n=5+5)
name old allocs/op new allocs/op delta
Init-8 14.0 ± 0% 1.0 ± 0% -92.86% (p=0.008 n=5+5)
Note the numbers are tiny already, but ./cmd/searcher isn't representative of slowness experienced in ./cmd/frontend/...