Skip to content

refactor(runtime): Simplify RuntimeHelper metrics collection - #889

Draft
CagriYonca wants to merge 1 commit into
mainfrom
gc-metrics
Draft

refactor(runtime): Simplify RuntimeHelper metrics collection #889
CagriYonca wants to merge 1 commit into
mainfrom
gc-metrics

Conversation

@CagriYonca

Copy link
Copy Markdown
Contributor

What & Why

RuntimeHelper._collect_runtime_metrics() contained 15 near-identical apply_delta() call-sites — one per rusage field — and _collect_gc_metrics() had six more. This PR replaces all of them with direct attribute access and purpose-built loops, and extracts _resolve_package_version() from the body of gather_python_packages() so each concern sits in its own method.


Changes

  • Replace 15 repetitive apply_delta() calls in _collect_runtime_metrics() with 16 direct-attribute-access call-sites. The previous code used a mixed strategy — two direct calls followed by a getattr loop for the remaining 10 fields — which imposed unnecessary getattr overhead. The new code uses direct attribute access throughout, eliminating the loop and the string lookups entirely.
  • Rewrite _collect_gc_metrics() with a gc_metrics dict + loop instead of six repeated call-sites.
  • Rewrite _collect_thread_metrics() with a single-pass for loop instead of three list-comprehensions.
  • Extract _resolve_package_version() helper from gather_python_packages().
  • Replace lambda in is_webhook_instrumented() with a generator expression.
  • Update all type hints from Dict / List to built-in dict / list (PEP 585).
  • Add missing return "" in jsonable() exception handler.
  • Move rusage = get_resource_usage() before the try block so the finally clause always sees it.
  • Improve docstring placement and wording throughout.

Performance

Benchmarked on Python 3.12 and 3.14, 50,000 iterations × 5 runs (_bench_runtime.py). All three hot-paths improved:

Hot-path OLD (µs/call) NEW (µs/call) Speedup
rusage — 16 fields 1.292 1.040 +1.24x
GC metrics — 6 fields 1.011 0.601 +1.68x
Thread metrics — real threads 0.539 0.305 +1.77x
Thread metrics — 100 threads 11.238 6.249 +1.80x
Thread metrics — 500 threads 51.330 31.103 +1.65x
is_webhook_instrumented 0.443 0.445 ≈ neutral

The rusage improvement (+1.24x) comes from eliminating the mixed getattr loop: the old code used direct attribute access for 6 fields and a getattr string-lookup loop for the remaining 10. The new code uses direct access for all 16 fields, removing the loop overhead and the per-field getattr calls entirely.

The thread metrics improvement (+1.7–1.85x across all thread counts from 1 to 500) comes from replacing three full list-comprehension passes over the thread list with a single for loop that classifies each thread in one pass.


How to review

The diff is large in line count but mechanical — every deleted block has a direct replacement immediately after it. The rusage section is the most verbose part: 16 aligned apply_delta() lines that replace the previous mixed direct+loop pattern. The GC and thread sections follow the same structural pattern.

@CagriYonca CagriYonca self-assigned this Aug 6, 2026
@CagriYonca
CagriYonca requested a review from a team as a code owner August 6, 2026 15:11
@CagriYonca
CagriYonca marked this pull request as draft August 6, 2026 15:19
@CagriYonca
CagriYonca marked this pull request as ready for review August 6, 2026 15:26
Signed-off-by: Cagri Yonca <cagri@ibm.com>
@CagriYonca
CagriYonca marked this pull request as draft August 7, 2026 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant