-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Hi!
The zapcore/sampler.go allocates ~0.5MB per instance.
That's quite a bit considering that some binaries instantiate quite a few. For example, OpenTelemetry collectors monitoring Kubernetes cluters can instantiate tens of thousands of those.
Here I suggest an update that divides the memory consumption by 7 and improves performance, with very little drawback.
Details
The sampler allocates numberOfLogLevels * 4096 instances of the counter structure (128 bits). There are 7 different log levels. Once an log message is emitted, it's hashed to one of those 7*4096 buckets.
In practice, all 7 log levels are not used uniformly (far from it), and some (e.g. DPanic, Panic, Fatal) re barely used at all.
I thus propose to only instantiate 4096 instances of the counter structure (dividing the memory footprint in 7) and include the log level as an input to the hash function. Since the 7 log levels are use very unevenly, this only marginally impact the risks of collision while improving cache locality.