Skip to content

Inline the observable check in _raise_if_not_observable - #1262

Open
g4lb wants to merge 1 commit into
prometheus:masterfrom
g4lb:perf-observable-check
Open

g4lb wants to merge 1 commit into
prometheus:masterfrom
g4lb:perf-observable-check

Conversation

@g4lb

@g4lb g4lb commented Nov 18, 2026

Copy link
Copy Markdown

@csmarchbanks, each inc(), dec(), set() and observe() call runs _raise_if_not_observable(). This change puts the condition of _is_observable() directly in that method, so each update does one less method call. The output and the error do not change.

Benchmark (1000 calls) Before After Change
Counter.inc() 132.6 µs 118.9 µs -10.3%
labels(...).inc() 409.4 µs 381.9 µs -6.7%
Gauge child inc() and set() 143.0 µs 123.2 µs -13.9%
Histogram.observe() 352.6 µs 337.7 µs -4.2%
Summary.observe() 211.5 µs 198.6 µs -6.1%

These numbers are from CPython 3.13. CPython 3.10 and 3.14 show the same direction: Counter.inc() is 12.9% and 15.9% faster. I measured no slowdown on exposition, parsing, labels() and metric creation. I used 20 interleaved rounds and a Mann-Whitney test (Apple M5). On PyPy 3.11, the change is within the noise. Import time did not change (25 rounds). I did not measure other architectures.

Checks:

  • pytest passes on CPython 3.10, 3.13 and 3.14, and on PyPy 3.11.
  • flake8, isort and mypy pass with the tox settings.
  • Old and new outputs matched on 1.15M inputs (metric values, both text formats, parsed families, and exceptions).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant