Skip to content

fix: Remove hasattr check for every invocation#1834

Merged
gavin-aguiar merged 6 commits intodevfrom
gaaguiar/improve_perf
Mar 13, 2026
Merged

fix: Remove hasattr check for every invocation#1834
gavin-aguiar merged 6 commits intodevfrom
gaaguiar/improve_perf

Conversation

@gavin-aguiar
Copy link
Contributor

@gavin-aguiar gavin-aguiar commented Mar 4, 2026

Description

This PR has three key optimizations in the dispatcher:

  1. Eliminated Repeated Attribute Checks (hasattr):
    Change: Introduced a global flag _library_worker_has_cv that is set once when the worker library is loaded.
    Impact: get_current_invocation_id runs on every log message. Previously, it called hasattr(_library_worker, 'invocation_id_cv') thousands of times during startup. This is now a fast boolean check.

  2. Lock-Free Fast Path for Invocation ID:
    Change: Reordered get_current_invocation_id to check the ContextVar before checking the thread-local registry.
    Impact: The thread registry requires acquiring a threading.Lock. By checking the context variable first, we avoid lock acquisition overhead and contention for the vast majority of log calls in modern Python versions.

  3. Cached Protobuf Constants in Hot Path:
    Change: Created module-level constants for RpcLog levels (e.g., _LOG_LEVEL_INFO) and categories (_LOG_CATEGORY_SYSTEM).
    Impact: The on_logging method previously performed expensive attribute lookups (e.g., protos.RpcLog.Information) and function calls (.Value('System')) for every log record. These are now instant constant lookups.

Fixes #


Pull Request Checklist

Host-Worker Contract

  • Does this PR impact the host-worker contract (e.g., gRPC messages, shared interfaces)?
    • If yes, have the changes been applied to:
      • azure_functions_worker (Python <= 3.12)
      • proxy_worker (Python >= 3.13)
    • If no, please explain why:

Worker Execution Logic

  • Does this PR affect worker execution logic (e.g., function invocation, bindings, lifecycle)?
    If yes, please answer the following:

Python Version Coverage

  • Does this change apply to both Python <=3.12 and 3.13+?
  • If yes, have the changes been made to:
    • azure_functions_worker (Python <= 3.12)
    • runtimes/v1 / runtimes/v2 (Python >= 3.13)
  • If no, please explain why:

Programming Model Compatibility (for Python 3.13+)

  • Does this change apply to both:
    • V1 programming model (runtimes/v1)?
    • V2 programming model (runtimes/v2)?
  • Explanation (if limited to one model):

@gavin-aguiar gavin-aguiar changed the title Remove hasattr check for every invocation fix: Remove hasattr check for every invocation Mar 13, 2026
@gavin-aguiar gavin-aguiar marked this pull request as ready for review March 13, 2026 16:18
@gavin-aguiar gavin-aguiar merged commit 4453464 into dev Mar 13, 2026
43 of 46 checks passed
@gavin-aguiar gavin-aguiar deleted the gaaguiar/improve_perf branch March 13, 2026 19:05
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.

2 participants