refactor(runtime): Simplify RuntimeHelper metrics collection - #889
Draft
CagriYonca wants to merge 1 commit into
Draft
refactor(runtime): Simplify RuntimeHelper metrics collection #889CagriYonca wants to merge 1 commit into
CagriYonca wants to merge 1 commit into
Conversation
CagriYonca
marked this pull request as draft
August 6, 2026 15:19
CagriYonca
marked this pull request as ready for review
August 6, 2026 15:26
Signed-off-by: Cagri Yonca <cagri@ibm.com>
CagriYonca
marked this pull request as draft
August 7, 2026 10:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 & Why
RuntimeHelper._collect_runtime_metrics()contained 15 near-identicalapply_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 ofgather_python_packages()so each concern sits in its own method.Changes
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 agetattrloop for the remaining 10 fields — which imposed unnecessarygetattroverhead. The new code uses direct attribute access throughout, eliminating the loop and the string lookups entirely._collect_gc_metrics()with agc_metricsdict + loop instead of six repeated call-sites._collect_thread_metrics()with a single-passforloop instead of three list-comprehensions._resolve_package_version()helper fromgather_python_packages().lambdainis_webhook_instrumented()with a generator expression.Dict/Listto built-indict/list(PEP 585).return ""injsonable()exception handler.rusage = get_resource_usage()before thetryblock so thefinallyclause always sees it.Performance
Benchmarked on Python 3.12 and 3.14, 50,000 iterations × 5 runs (
_bench_runtime.py). All three hot-paths improved:is_webhook_instrumentedThe rusage improvement (+1.24x) comes from eliminating the mixed
getattrloop: the old code used direct attribute access for 6 fields and agetattrstring-lookup loop for the remaining 10. The new code uses direct access for all 16 fields, removing the loop overhead and the per-fieldgetattrcalls 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
forloop 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.