Commit b2eca73
perf: planning a png or a gif stops coding the picture twice (#58)
Planning drew the whole picture and compressed it only to learn its
length, threw that away, and let the writer do it again. That was 38 to
53% of a PNG run.
The ladder is walked largest rung first, so for any request comfortably
above what the top rung encodes to, that rung is already the answer and
the encode only confirms it. ladderCeiling is what "comfortably above"
means, and planning takes the top rung without encoding when the request
clears it. Everything near a rung boundary still encodes and still gets
the exact number, so the answer never changes.
The bytes are identical, and that was checked rather than reasoned:
147 combinations of size, seed, label and frame count - including sizes
either side of the fast path threshold and either side of every rung -
came out identical, refusals included.
Measured with tools/probes/abcpu, order reversed, ranges disjoint:
png, 300 files of 200 kB cpu 3797 -> 1859 ms wall 4734 -> 2580 ms
gif, 200 files of 200 kB cpu 797 -> 453 ms wall 1232 -> 789 ms
jpg is deliberately left alone. It writes its padding in FRONT of the
picture, so the comment length has to be known before anything is
encoded - which is exactly the number the fast path does not have. The
only way round is moving the padding behind the picture, and that is a
documented padding channel and a breaking change.
Two guards, four mutations, all caught. One says the ceiling is safe -
too low is the dangerous direction, because planning would then accept a
size the writer has to refuse - and it reads the constant out of the
source rather than copying it. The other says the fast path is actually
taken: 50 plans allocate 29 kB against 2.4 MB for one write.
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>1 parent 9b087d5 commit b2eca73
4 files changed
Lines changed: 370 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
61 | 80 | | |
62 | 81 | | |
63 | 82 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
162 | 167 | | |
163 | 168 | | |
164 | 169 | | |
| |||
209 | 214 | | |
210 | 215 | | |
211 | 216 | | |
212 | | - | |
213 | | - | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
214 | 225 | | |
215 | 226 | | |
216 | 227 | | |
| |||
312 | 323 | | |
313 | 324 | | |
314 | 325 | | |
315 | | - | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
316 | 337 | | |
317 | 338 | | |
318 | 339 | | |
| |||
368 | 389 | | |
369 | 390 | | |
370 | 391 | | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
371 | 407 | | |
372 | 408 | | |
373 | 409 | | |
| |||
402 | 438 | | |
403 | 439 | | |
404 | 440 | | |
405 | | - | |
| 441 | + | |
406 | 442 | | |
407 | 443 | | |
408 | 444 | | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
409 | 460 | | |
410 | 461 | | |
411 | 462 | | |
412 | 463 | | |
413 | 464 | | |
414 | 465 | | |
415 | 466 | | |
416 | | - | |
| 467 | + | |
417 | 468 | | |
418 | 469 | | |
419 | 470 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
125 | 131 | | |
126 | 132 | | |
127 | 133 | | |
| |||
184 | 190 | | |
185 | 191 | | |
186 | 192 | | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
187 | 201 | | |
188 | 202 | | |
189 | 203 | | |
| |||
263 | 277 | | |
264 | 278 | | |
265 | 279 | | |
266 | | - | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
267 | 295 | | |
268 | 296 | | |
269 | 297 | | |
| |||
280 | 308 | | |
281 | 309 | | |
282 | 310 | | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
283 | 327 | | |
284 | 328 | | |
285 | 329 | | |
| |||
321 | 365 | | |
322 | 366 | | |
323 | 367 | | |
324 | | - | |
| 368 | + | |
325 | 369 | | |
326 | 370 | | |
327 | 371 | | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
328 | 397 | | |
329 | 398 | | |
330 | 399 | | |
331 | 400 | | |
332 | 401 | | |
333 | 402 | | |
334 | 403 | | |
335 | | - | |
| 404 | + | |
336 | 405 | | |
337 | 406 | | |
338 | 407 | | |
| |||
0 commit comments