|
19 | 19 | // 32 | 4 | startLine |
20 | 20 | // 36 | 2 | startColumn |
21 | 21 | // 38 | 2 | (padding) |
| 22 | +// |
| 23 | +// HOW THE ARENA WORKS: |
| 24 | +// 1. BYTES_PER_NODE defines the size of each node (40 bytes). The ArrayBuffer size is calculated |
| 25 | +// as: capacity × BYTES_PER_NODE. For example, 1024 nodes = 40,960 bytes (~40KB). |
| 26 | +// Node indices map to byte offsets via: node_offset = node_index × 40. |
| 27 | +// |
| 28 | +// 2. We use a single DataView over the ArrayBuffer to read/write different types at specific offsets. |
| 29 | +// - Uint8: 1-byte reads/writes for type, flags (e.g., view.getUint8(offset)) |
| 30 | +// - Uint16: 2-byte reads/writes for length, deltas, column (e.g., view.getUint16(offset, true)) |
| 31 | +// - Uint32: 4-byte reads/writes for startOffset, pointers, line (e.g., view.getUint32(offset, true)) |
| 32 | +// The 'true' parameter specifies little-endian byte order (native on x86/ARM CPUs). |
| 33 | +// |
| 34 | +// 3. Padding (6 bytes total at offsets 2-3, 10-11, 38-39) ensures memory alignment for performance: |
| 35 | +// - Uint32 fields align to 4-byte boundaries (offsets 4, 20, 24, 28, 32) |
| 36 | +// - Uint16 fields align to 2-byte boundaries (offsets 8, 10, 12, 14, 16, 18, 36, 38) |
| 37 | +// Aligned access is faster (single CPU instruction) vs unaligned (multiple memory accesses). |
| 38 | +// Modern CPUs penalize unaligned reads/writes, making padding essential for performance. |
| 39 | +// |
| 40 | +// 4. The padding at offset 2-3 is reused for attribute selector data (attr_operator, attr_flags), |
| 41 | +// making efficient use of otherwise wasted bytes. This is a space optimization trick. |
| 42 | +// |
| 43 | +// 5. Delta offsets (contentStartDelta, valueStartDelta) save memory: instead of storing absolute |
| 44 | +// positions as uint32 (4 bytes), we store relative offsets as uint16 (2 bytes). This reduced |
| 45 | +// node size from 44→40 bytes (10% smaller), saving memory while maintaining performance. |
22 | 46 |
|
23 | 47 | let BYTES_PER_NODE = 40 |
24 | 48 |
|
|
0 commit comments