Commit 06fbe8b
rodrigo.nogueira
## Description
The `signature_adapter` causes significant overhead in hot transition paths due to repeated signature binding on every callback invocation. This PR implements caching for `bind_expected()` to avoid recomputing argument bindings when the kwargs pattern is unchanged.
## Root Cause
The `SignatureAdapter.bind_expected()` method iterates through all parameters and matches them against the provided kwargs on every invocation. In typical state machine usage, callbacks are invoked repeatedly with the same kwargs keys (e.g., `source`, `target`, `event`), making this repeated computation wasteful.
## Fix
Added a per-instance cache (`_bind_cache`) to `SignatureAdapter` that stores "binding templates" based on the arguments structure:
- **Cache key**: `(len(args), frozenset(kwargs.keys()))`
- **Cache value**: A template of which parameters to extract
- **Fast path**: On cache hit, extract arguments directly using the template (~1 µs)
- **Slow path**: First call computes full binding and stores template (~2 µs)
This approach preserves **full Dependency Injection functionality** - callbacks still receive correctly filtered arguments (`source`, `target`, etc.).
## Performance
When measuring `bind_expected()` in isolation:
- **Cached**: 0.86 µs/call
- **Uncached**: 2.12 µs/call
- **Improvement**: ~59%
This is consistent with the ~30% end-to-end improvement reported in #548, as binding is one of several components in a full transition.
## Testing
All existing tests pass (328 passed, 9 xfailed).
Fixes #5481 parent 9a089ed commit 06fbe8b
1 file changed
Lines changed: 56 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
44 | 47 | | |
45 | 48 | | |
46 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
47 | 55 | | |
48 | 56 | | |
49 | 57 | | |
| |||
60 | 68 | | |
61 | 69 | | |
62 | 70 | | |
63 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
64 | 104 | | |
65 | 105 | | |
66 | 106 | | |
67 | 107 | | |
68 | 108 | | |
69 | 109 | | |
70 | | - | |
| 110 | + | |
| 111 | + | |
71 | 112 | | |
72 | 113 | | |
73 | 114 | | |
74 | 115 | | |
75 | 116 | | |
| 117 | + | |
76 | 118 | | |
77 | 119 | | |
78 | 120 | | |
| |||
141 | 183 | | |
142 | 184 | | |
143 | 185 | | |
| 186 | + | |
144 | 187 | | |
145 | 188 | | |
146 | 189 | | |
147 | 190 | | |
148 | 191 | | |
149 | 192 | | |
| 193 | + | |
150 | 194 | | |
151 | 195 | | |
152 | 196 | | |
| |||
172 | 216 | | |
173 | 217 | | |
174 | 218 | | |
175 | | - | |
| 219 | + | |
| 220 | + | |
176 | 221 | | |
177 | 222 | | |
178 | 223 | | |
179 | 224 | | |
180 | | - | |
| 225 | + | |
| 226 | + | |
181 | 227 | | |
182 | 228 | | |
183 | 229 | | |
184 | 230 | | |
185 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
0 commit comments