Skip to content

perf: memoize per-step signature/arg introspection - #528

Open
jmecosta wants to merge 1 commit into
buildingSMART:mainfrom
jmecosta:perf/cache-step-introspection
Open

perf: memoize per-step signature/arg introspection#528
jmecosta wants to merge 1 commit into
buildingSMART:mainfrom
jmecosta:perf/cache-step-introspection

Conversation

@jmecosta

Copy link
Copy Markdown

Memoizes the per-step inspect introspection that apply_operation() / handle_then() run once per instance.

Problem

apply_operation() calls inspect.signature(fn) and handle_then() calls inspect.getargs(fn.__code__) on every instance to decide whether a step accepts path / npath / inst. On a 54 MB model that is ~540k calls and ~12 s of pure signature recompilation (cProfile self-time dominated by inspect._signature_from_function + builtins.compile).

Fix

The set of step functions is small and fixed, and whether a given function accepts a parameter never changes — so memoize it with functools.lru_cache (_fn_accepts_param, _code_accepts_arg). No behavioural change: outcomes are byte-identical. Benefits every rule and execution mode (in-memory and production).

Profiling write-up and before/after numbers in #527.

apply_operation()/handle_then() call inspect.signature(fn) and
inspect.getargs(fn.__code__) once per instance. On a 54 MB model that is
~540k calls and ~12s of pure signature recompilation (cProfile:
inspect._signature_from_function + builtins.compile dominate self-time).

The set of step functions is small and fixed, and whether a step accepts the
path / npath parameter never changes for a given function, so memoize it with
functools.lru_cache. No behavioural change (byte-identical outcomes); benefits
every rule and execution mode.

See buildingSMART#527 for the profiling write-up.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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