cockroachdb / cockroachdb/cockroach
optimize value storage in contexts
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
As of mid November, `context.Value` and `context.WithValue` take about 1% of the CPU time in sysbench. This is a proposal to speed this up.
Most of the uses in the hot path are in our code and only involve a small number of keys (tracing, log tags, statement, plan gist, server ID).
Proposal: implement a new "fast value" context which contains all values for these keys in an array. The keys are globally registered at init time (e.g. `var tracingContextKey = ctxutil.RegisterFastValue()`).
The lower-most `fastValueCtx` contains all the current values for these keys.
To interoperate with other contexts, the `fastValueCtx` produces a `Value()` for a special `fastValuesKey{}`, containing the values in the most recent `fastValueCtx` in the chain.
To reduce the number of allocations, we can allocate `fastValueCtx`es in "magazines" and store the magazines that still have unused slots in a pool.
Jira issue: CRDB-45145
Contributor guide
Assessment
This issue has not been assessed yet.