Commit a0f4032
fix(read-state): O(n) eviction, fitsAfterTrim signal, suppress publish on overflow
Address two IMPORTANT findings from Thufir's review of #1305:
1. O(n²) re-encode loop — the eviction loop called encoder.encode() on
every iteration, O(n) per step × n steps = O(n²). With 7,500 entries
this caused a confirmed 4.7s UI freeze on first publish after upgrade.
Replace with an O(n) pass: compute total bytes once, subtract each
entry's per-entry delta (key.length + 4 + timestamp digits), collect
entries to evict, delete them in a batch, then do one final encode as
the authoritative check (handles JSON comma-accounting edge cases the
delta estimate ignores).
2. Silent failure when channel-only blob exceeds budget — after exhausting
all msg:/thread: entries the function returned without checking whether
the remaining blob still exceeded the budget. The caller would then
proceed to publish a blob the relay would reject.
Fix: trimContextsToBudget() now returns { evicted, fitsAfterTrim }.
currentContexts() returns null when fitsAfterTrim is false; publish()
and initialize() both guard against null and skip the publish.
Also address two MINOR findings:
- JSDoc now documents that trimContextsToBudget mutates contexts in place
- Empty-map test replaced with a test that actually exercises the
fitsAfterTrim=false path (channel-only blob smaller than budget)
Co-authored-by: Will Pfleger <pfleger.will@gmail.com>
Signed-off-by: Will Pfleger <pfleger.will@gmail.com>1 parent dd32b21 commit a0f4032
2 files changed
Lines changed: 81 additions & 20 deletions
Lines changed: 32 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
159 | | - | |
| 159 | + | |
160 | 160 | | |
| 161 | + | |
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
| |||
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
179 | | - | |
| 180 | + | |
180 | 181 | | |
| 182 | + | |
181 | 183 | | |
182 | 184 | | |
183 | 185 | | |
| |||
201 | 203 | | |
202 | 204 | | |
203 | 205 | | |
204 | | - | |
| 206 | + | |
205 | 207 | | |
206 | 208 | | |
207 | 209 | | |
| 210 | + | |
208 | 211 | | |
209 | 212 | | |
210 | 213 | | |
| |||
232 | 235 | | |
233 | 236 | | |
234 | 237 | | |
235 | | - | |
| 238 | + | |
236 | 239 | | |
| 240 | + | |
237 | 241 | | |
238 | 242 | | |
239 | 243 | | |
240 | 244 | | |
241 | | - | |
| 245 | + | |
| 246 | + | |
242 | 247 | | |
243 | | - | |
| 248 | + | |
244 | 249 | | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
245 | 271 | | |
Lines changed: 49 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
126 | 136 | | |
127 | 137 | | |
128 | 138 | | |
129 | 139 | | |
130 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
131 | 145 | | |
132 | 146 | | |
133 | 147 | | |
134 | 148 | | |
135 | 149 | | |
136 | 150 | | |
137 | 151 | | |
138 | | - | |
| 152 | + | |
139 | 153 | | |
140 | 154 | | |
141 | 155 | | |
142 | 156 | | |
143 | | - | |
144 | | - | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
145 | 160 | | |
146 | 161 | | |
147 | 162 | | |
| |||
157 | 172 | | |
158 | 173 | | |
159 | 174 | | |
160 | | - | |
161 | | - | |
162 | | - | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
163 | 188 | | |
164 | | - | |
165 | 189 | | |
166 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
167 | 195 | | |
168 | 196 | | |
169 | 197 | | |
| |||
207 | 235 | | |
208 | 236 | | |
209 | 237 | | |
210 | | - | |
| 238 | + | |
| 239 | + | |
211 | 240 | | |
212 | 241 | | |
213 | 242 | | |
| |||
543 | 572 | | |
544 | 573 | | |
545 | 574 | | |
546 | | - | |
547 | | - | |
| 575 | + | |
| 576 | + | |
548 | 577 | | |
549 | 578 | | |
550 | 579 | | |
| |||
630 | 659 | | |
631 | 660 | | |
632 | 661 | | |
633 | | - | |
| 662 | + | |
634 | 663 | | |
635 | 664 | | |
636 | 665 | | |
| |||
643 | 672 | | |
644 | 673 | | |
645 | 674 | | |
646 | | - | |
| 675 | + | |
647 | 676 | | |
648 | 677 | | |
649 | 678 | | |
| |||
653 | 682 | | |
654 | 683 | | |
655 | 684 | | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
656 | 691 | | |
657 | 692 | | |
658 | 693 | | |
| |||
0 commit comments