Commit cadb353
committed
perf(array): optimize defragment with TypedArray loop
Replaces the slow DataView byte-by-byte copy loop in `ShareableArray.defragment` with a `Uint8Array` indexed loop.
This avoids the overhead of `DataView` methods and the overhead of creating `subarray` views for small objects.
Also fixes a bug where `DATA_OBJECT_OFFSET` was added twice to the data pointer, causing 8-byte gaps between objects after defragmentation.
Benchmark results (100k items, 50% fragmentation):
- Before: ~22ms
- After: ~14ms (~35% improvement)1 parent ba797cc commit cadb353
3 files changed
Lines changed: 38 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
945 | 945 | | |
946 | 946 | | |
947 | 947 | | |
948 | | - | |
| 948 | + | |
| 949 | + | |
949 | 950 | | |
950 | 951 | | |
951 | 952 | | |
| |||
956 | 957 | | |
957 | 958 | | |
958 | 959 | | |
959 | | - | |
960 | | - | |
| 960 | + | |
| 961 | + | |
961 | 962 | | |
962 | 963 | | |
963 | 964 | | |
964 | 965 | | |
965 | 966 | | |
966 | 967 | | |
967 | | - | |
| 968 | + | |
968 | 969 | | |
969 | 970 | | |
970 | 971 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
629 | 629 | | |
630 | 630 | | |
631 | 631 | | |
632 | | - | |
633 | | - | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
634 | 662 | | |
635 | 663 | | |
636 | 664 | | |
| |||
0 commit comments