Commit 277271d
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 277271d
1 file changed
Lines changed: 63 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
9 | 17 | | |
10 | 18 | | |
11 | 19 | | |
| |||
44 | 52 | | |
45 | 53 | | |
46 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
47 | 60 | | |
48 | 61 | | |
49 | 62 | | |
| |||
60 | 73 | | |
61 | 74 | | |
62 | 75 | | |
63 | | - | |
| 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 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
64 | 112 | | |
65 | 113 | | |
66 | 114 | | |
67 | 115 | | |
68 | 116 | | |
69 | 117 | | |
70 | | - | |
| 118 | + | |
| 119 | + | |
71 | 120 | | |
72 | 121 | | |
73 | 122 | | |
74 | 123 | | |
75 | 124 | | |
| 125 | + | |
76 | 126 | | |
77 | 127 | | |
78 | 128 | | |
| |||
95 | 145 | | |
96 | 146 | | |
97 | 147 | | |
98 | | - | |
99 | | - | |
| 148 | + | |
100 | 149 | | |
101 | 150 | | |
102 | 151 | | |
| |||
141 | 190 | | |
142 | 191 | | |
143 | 192 | | |
| 193 | + | |
144 | 194 | | |
145 | 195 | | |
146 | 196 | | |
147 | 197 | | |
148 | 198 | | |
149 | 199 | | |
| 200 | + | |
150 | 201 | | |
151 | 202 | | |
152 | 203 | | |
| |||
172 | 223 | | |
173 | 224 | | |
174 | 225 | | |
175 | | - | |
| 226 | + | |
| 227 | + | |
176 | 228 | | |
177 | 229 | | |
178 | 230 | | |
179 | 231 | | |
180 | | - | |
| 232 | + | |
| 233 | + | |
181 | 234 | | |
182 | 235 | | |
183 | 236 | | |
184 | 237 | | |
185 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
0 commit comments