perf: memoize per-step signature/arg introspection - #528
Open
jmecosta wants to merge 1 commit into
Open
Conversation
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>
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.
Memoizes the per-step
inspectintrospection thatapply_operation()/handle_then()run once per instance.Problem
apply_operation()callsinspect.signature(fn)andhandle_then()callsinspect.getargs(fn.__code__)on every instance to decide whether a step acceptspath/npath/inst. On a 54 MB model that is ~540k calls and ~12 s of pure signature recompilation (cProfile self-time dominated byinspect._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.