Commit 1fd4b7e
perf: cache signature binding results (#550)
* ## 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 #548
* fix: Ensure `**kwargs` only contains unmatched arguments during signature binding by filtering out named parameters.
* chore: Remove extra empty line in test file.
---------
Co-authored-by: rodrigo.nogueira <rodrigo.nogueira@prf.gov.br>1 parent bf06293 commit 1fd4b7e
3 files changed
Lines changed: 99 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
| 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 | + | |
| 112 | + | |
64 | 113 | | |
65 | 114 | | |
66 | 115 | | |
67 | 116 | | |
68 | 117 | | |
69 | 118 | | |
70 | | - | |
| 119 | + | |
| 120 | + | |
71 | 121 | | |
72 | 122 | | |
73 | 123 | | |
74 | 124 | | |
75 | 125 | | |
| 126 | + | |
76 | 127 | | |
77 | 128 | | |
78 | 129 | | |
| |||
140 | 191 | | |
141 | 192 | | |
142 | 193 | | |
| 194 | + | |
143 | 195 | | |
144 | 196 | | |
145 | 197 | | |
146 | 198 | | |
147 | 199 | | |
148 | 200 | | |
| 201 | + | |
149 | 202 | | |
150 | 203 | | |
151 | 204 | | |
| |||
171 | 224 | | |
172 | 225 | | |
173 | 226 | | |
174 | | - | |
| 227 | + | |
| 228 | + | |
175 | 229 | | |
176 | 230 | | |
177 | 231 | | |
178 | 232 | | |
179 | | - | |
| 233 | + | |
| 234 | + | |
180 | 235 | | |
181 | 236 | | |
182 | 237 | | |
183 | 238 | | |
184 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
0 commit comments