Commit 2640c18
committed
perf: cache _replica_dict on Tablet for O(1) host/shard lookup
Build a {host_id: shard_id} dict at Tablet construction time and use it
in the two per-query hot paths that previously did repeated work:
1. policies.py (TokenAwarePolicy.make_query_plan):
- Before: set(map(lambda r: r[0], tablet.replicas)) -- 372 ns/call
(creates a new set, lambda, and iterates replicas on every query)
- After: tablet._replica_dict -- 18 ns/call (attribute access)
- Saving: 354 ns/query (20.7x faster)
2. pool.py (HostConnectionPool._get_connection_for_routing_key):
- Before: linear scan of tablet.replicas -- 199 ns/call (worst case)
- After: tablet._replica_dict.get(host_id) -- 73 ns/call
- Saving: 126 ns/query (2.7x faster)
3. replica_contains_host_id (used by drop_tablets_by_host_id):
- Before: linear scan -- O(n)
- After: dict 'in' -- O(1)
Total per-query savings: ~480 ns (both paths combined).
Memory cost: 224 bytes/tablet for the dict. Net vs original baseline
(416 bytes/tablet): still 136 bytes MORE, but the per-query time
savings dominate for any meaningful query rate. The dict is built
once at tablet creation (rare) and amortized across all queries.
Tablet.from_row is slower (186 -> 464 ns) due to dict construction,
but this runs only on tablet metadata updates, not per query.1 parent 06cf4de commit 2640c18
4 files changed
Lines changed: 55 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
507 | 507 | | |
508 | 508 | | |
509 | 509 | | |
510 | | - | |
| 510 | + | |
511 | 511 | | |
512 | 512 | | |
513 | | - | |
| 513 | + | |
514 | 514 | | |
515 | 515 | | |
516 | 516 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
462 | 462 | | |
463 | 463 | | |
464 | 464 | | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
| 465 | + | |
469 | 466 | | |
470 | 467 | | |
471 | 468 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
24 | 29 | | |
25 | 30 | | |
26 | 31 | | |
| |||
39 | 44 | | |
40 | 45 | | |
41 | 46 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
124 | 125 | | |
125 | 126 | | |
126 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
0 commit comments