Skip to content

do inspect signature once#6197

Merged
adhami3310 merged 1 commit intomainfrom
do-inspect-signature-once
Mar 19, 2026
Merged

do inspect signature once#6197
adhami3310 merged 1 commit intomainfrom
do-inspect-signature-once

Conversation

@adhami3310
Copy link
Member

No description provided.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 19, 2026

Greptile Summary

This PR is a small but impactful performance optimization in var_operation. The call to inspect.signature(func).parameters — which builds the parameter list used to map positional arguments to their names — was previously executed inside the wrapper closure, meaning it ran on every single invocation of any decorated var operation. By hoisting it one scope level up (into the body of var_operation itself), the signature inspection now happens exactly once at decoration/import time and the result is reused across all subsequent calls.

  • Changed file: reflex/vars/base.pyvar_operation decorator
  • Optimization: func_args is computed once when the decorator is applied instead of on every call to the wrapped function
  • Correctness: Semantically identical — func's signature never changes at runtime, so caching the parameter list is always safe
  • No behavioral change: The wrapper logic is unchanged; it still maps positional args and keyword args correctly using the pre-computed func_args list

Confidence Score: 5/5

  • This PR is safe to merge — it is a pure performance optimisation with no behavioural change.
  • The moved expression is referentially transparent (same function, same result every time), so the refactor is provably equivalent. There are no side-effects, no threading concerns, and no API surface changes. The only risk would be a latent bug in the existing wrapper logic, which this PR does not touch.
  • No files require special attention.

Important Files Changed

Filename Overview
reflex/vars/base.py Moves inspect.signature(func).parameters computation from inside the wrapper closure to the outer var_operation scope, so it runs once at decoration time instead of on every invocation of each var operation. The change is semantically equivalent and a clear performance win.

Sequence Diagram

sequenceDiagram
    participant Decorator as var_operation(func)
    participant Wrapper as wrapper(*args, **kwargs)
    participant Inspect as inspect.signature(func)

    Note over Decorator,Inspect: BEFORE — called on every invocation
    Decorator->>Wrapper: define wrapper
    Wrapper->>Inspect: inspect.signature(func).parameters
    Inspect-->>Wrapper: func_args
    Wrapper->>Wrapper: build args_vars / kwargs_vars
    Wrapper->>Wrapper: return CustomVarOperation.create(...)

    Note over Decorator,Inspect: AFTER (this PR) — called once at decoration time
    Decorator->>Inspect: inspect.signature(func).parameters
    Inspect-->>Decorator: func_args (cached in closure)
    Decorator->>Wrapper: define wrapper (func_args already available)
    Wrapper->>Wrapper: build args_vars / kwargs_vars
    Wrapper->>Wrapper: return CustomVarOperation.create(...)
Loading

Last reviewed commit: "do inspect signature..."

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 19, 2026

Merging this PR will not alter performance

✅ 8 untouched benchmarks


Comparing do-inspect-signature-once (59de2ca) with main (7f5fdcc)

Open in CodSpeed

Copy link
Collaborator

@masenf masenf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i love stuff like this! 🫶

@adhami3310 adhami3310 merged commit cb91b90 into main Mar 19, 2026
47 checks passed
@adhami3310 adhami3310 deleted the do-inspect-signature-once branch March 19, 2026 22:00
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