Commit ec28103
Performance [P2]: Make entity method signature caching effective (#202)
* Make entity method signature caching effective
_execute_entity_batch() constructed a new _EntityExecutor inside its
per-operation loop, so the executor's _entity_method_cache was always cold
and inspect.signature() was re-run for every single entity operation.
The executor is now constructed once per batch and shares a bounded,
thread-safe signature cache owned by the worker, so a given
(entity type, operation) pair is reflected once for the lifetime of the
worker rather than once per operation.
The cache is bounded (_EntityMethodSignatureCache, 1024 entries, oldest
evicted first) because entity types and operation names come from user code
and are unbounded in principle. Writes are serialized under a lock so
concurrently processed entity work items stay safe.
Behavior is unchanged: the dispatch logic in _EntityExecutor.execute() is
untouched, so the no-argument versus input-argument distinction and all
error messages for unknown or invalid operations are preserved.
_EntityExecutor's existing three-argument constructor still works; the
shared cache is an optional fourth argument.
Fixes #186
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 2265f7b9-f8ad-4b50-a25d-19bd6f1ac96c
* Test that a non-positive signature cache bound is clamped
_EntityMethodSignatureCache clamps its bound with max(1, max_size), but
nothing covered that clamp, so the PR description overstated the test
coverage. Add regression tests for it.
The clamp is not cosmetic. Without it a bound of 0 evicts each entry
immediately after inserting it, so every lookup misses and the entity
method reflection this cache exists to avoid runs on every operation
again -- silently reintroducing the very problem this change fixes. A
negative bound is worse: the eviction loop keeps popping past an
already-empty dict and raises StopIteration.
The new tests cover a zero and negative bound, that a clamped cache
still behaves as a working size-one cache rather than a no-op, and that
an executor handed a clamped cache still reflects only once. Verified
red before green: with the clamp removed all five fail, including
'assert 3 == 1' on the reflection count.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 2265f7b9-f8ad-4b50-a25d-19bd6f1ac96c
---------
Co-authored-by: Bernd Verst <beverst@microsoft.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 2265f7b9-f8ad-4b50-a25d-19bd6f1ac96c1 parent 6a52e1d commit ec28103
2 files changed
Lines changed: 353 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
535 | 535 | | |
536 | 536 | | |
537 | 537 | | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
538 | 541 | | |
539 | 542 | | |
540 | 543 | | |
| |||
1373 | 1376 | | |
1374 | 1377 | | |
1375 | 1378 | | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
1376 | 1388 | | |
1377 | 1389 | | |
1378 | | - | |
1379 | 1390 | | |
1380 | 1391 | | |
1381 | 1392 | | |
| |||
3107 | 3118 | | |
3108 | 3119 | | |
3109 | 3120 | | |
| 3121 | + | |
| 3122 | + | |
| 3123 | + | |
| 3124 | + | |
| 3125 | + | |
| 3126 | + | |
| 3127 | + | |
| 3128 | + | |
| 3129 | + | |
| 3130 | + | |
| 3131 | + | |
| 3132 | + | |
| 3133 | + | |
| 3134 | + | |
| 3135 | + | |
| 3136 | + | |
| 3137 | + | |
| 3138 | + | |
| 3139 | + | |
| 3140 | + | |
| 3141 | + | |
| 3142 | + | |
| 3143 | + | |
| 3144 | + | |
| 3145 | + | |
| 3146 | + | |
| 3147 | + | |
| 3148 | + | |
| 3149 | + | |
| 3150 | + | |
| 3151 | + | |
| 3152 | + | |
| 3153 | + | |
| 3154 | + | |
| 3155 | + | |
| 3156 | + | |
| 3157 | + | |
| 3158 | + | |
| 3159 | + | |
| 3160 | + | |
3110 | 3161 | | |
3111 | 3162 | | |
3112 | | - | |
| 3163 | + | |
| 3164 | + | |
3113 | 3165 | | |
3114 | 3166 | | |
3115 | 3167 | | |
3116 | | - | |
| 3168 | + | |
| 3169 | + | |
| 3170 | + | |
| 3171 | + | |
| 3172 | + | |
| 3173 | + | |
| 3174 | + | |
| 3175 | + | |
3117 | 3176 | | |
3118 | 3177 | | |
3119 | 3178 | | |
| |||
0 commit comments