Commit 9b0e23b
authored
perf(upgrade): vectorize bspatch diff-add with SWAR (4x faster apply) (#1280)
## What
The diff-add step (`output[i] = (old[i] + diff[i]) % 256`) is the hot
inner loop of `bspatch` apply. Doing it byte-at-a-time in JS — two
typed-array property reads + a `% 256` per byte — dominates CPU time
when reconstructing a large binary.
## How
Extracts the loop into `addDiffChunk` and processes **4 bytes per
iteration** with a SWAR (SIMD-within-a-register) add on `Uint32Array`:
```
lows = (a & 0x7f7f7f7f) + (b & 0x7f7f7f7f) // top bit cleared → carries stay in-lane
highs = (a ^ b) & 0x80808080 // per-lane high-bit sum
result = lows ^ highs // per-byte sum mod 256
```
Carry-less: each byte lane sums independently mod 256. A short tail loop
handles the trailing `n % 4` bytes. Behavior is identical to the byte
loop it replaces.
On the Lore gateway's ported copy of this file (~310 MB binary, where
this optimization was first landed and reviewed) the diff-add dropped
from **~883 ms to ~221 ms (4×)**. sentry-cli binaries are smaller so the
absolute win is smaller, but it's free.
The code documents the load-bearing INVARIANT: the carry masks must
match the word width. A `BigUint64Array` variant must widen **both**
masks to 64-bit; pairing 64-bit words with the 32-bit mask silently
drops carries in the upper 4 bytes.
## Tests (exhaustive)
- Matches a per-byte reference implementation across all 256 byte values
and every 4-byte alignment.
- Wrap-around at `0xff` in every lane — a cross-lane carry detector.
- Tail lengths 0–3 across many window sizes.
- **Mutation-verified:** a mask/word-width mismatch turns all three SWAR
tests RED.
## Verification
- 18/18 `test/lib/bspatch.test.ts` pass, `tsc --noEmit` clean, `biome
check` clean.
- Independent of #1279 (the `MAX_OUTPUT_SIZE` security fix) — separate
file regions, no overlap.1 parent cb2ac09 commit 9b0e23b
2 files changed
Lines changed: 172 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
494 | 494 | | |
495 | 495 | | |
496 | 496 | | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
497 | 572 | | |
498 | 573 | | |
499 | 574 | | |
| |||
559 | 634 | | |
560 | 635 | | |
561 | 636 | | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
566 | 640 | | |
567 | 641 | | |
568 | 642 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
388 | 389 | | |
389 | 390 | | |
390 | 391 | | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
391 | 485 | | |
392 | 486 | | |
393 | 487 | | |
| |||
0 commit comments