[statsd] Add caching to tag normalization for Python3.2+ #674
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
On Python3.2 we now use the built-in
@lru_cache
decorator to add a small (size: 512) cache onnormalize_tags
to avoid expensivere.sub
calls when previously-seen tags are used in metrics.This decreases the
statsd
latency, CPU, and benchmark test duration significantly (~10-30%) onPython3.2+ with negligible impact on Python2.
Since this function is used in the
submit()
API too, it may offersignificant performance improvement there as well.
Description of the Change
Since tag normalization is still the highest bottleneck in metrics
submission latency, this change adds small caching (512 entries) to
that method's calls via built-in
@lru_cache
where available(Python3.2+). When the cache is hit, we avoid the ultra-expensive
re.sub
operation and increase the performance.Fixes #673
Alternate Designs
We could either add a new dependency or roll our own lru_cache to support ancient Python versions but that
seems like possibly wasted effort and/or bloat increase.
Possible Drawbacks
that in check.
Verification Process
statsd
metric sending)Additional Notes
Benchmark results:
Note: Benchmark code uses a limited amount of mostly-static global and metric tags
Single-threaded:
Multi-threaded:
Memory overhead: Negligible (see note about tags)
Release Notes
Review checklist (to be filled by reviewers)
changelog/
label attached. If applicable it should have thebackward-incompatible
label attached.do-not-merge/
label attached.kind/
andseverity/
labels attached at least.