Skip to content

Commit 43f61ed

Browse files
committed
refactor(napi/parser): correct comment about raw transfer buffer size (#12273)
Correct and update comment about the buffer size used by raw transfer, and why we use that size.
1 parent f130a0c commit 43f61ed

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

napi/parser/src/raw_transfer.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ use crate::{
2222
raw_transfer_types::{EcmaScriptModule, Error, RawTransferData},
2323
};
2424

25-
// For raw transfer, use a buffer 4 GiB in size, with 4 GiB alignment.
25+
// For raw transfer, use a buffer 2 GiB in size, with 4 GiB alignment.
2626
// This ensures that all 64-bit pointers have the same value in upper 32 bits,
2727
// so JS only needs to read the lower 32 bits to get an offset into the buffer.
28-
// However, only use first half of buffer (2 GiB) for the arena, so 32-bit offsets
29-
// don't have the highest bit set. JS bitwise operators interpret the highest bit as sign bit,
30-
// so this enables using `>>` bitshift operator in JS, rather than the more expensive `>>>`,
31-
// without offsets being interpreted as negative.
28+
//
29+
// Buffer size only 2 GiB so 32-bit offsets don't have the highest bit set.
30+
// This is advantageous for 2 reasons:
31+
//
32+
// 1. V8 stores small integers ("SMI"s) inline, rather than on heap, which is more performant.
33+
// But when V8 pointer compression is enabled, 31 bits is the max integer considered an SMI.
34+
// So using 32 bits for offsets would be a large perf hit when pointer compression is enabled.
35+
// 2. JS bitwise operators work only on signed 32-bit integers, with 32nd bit as sign bit.
36+
// So avoiding the 32nd bit being set enables using `>>` bitshift operator, which may be cheaper
37+
// than `>>>`, without offsets being interpreted as negative.
3238
const TWO_GIB: usize = 1 << 31;
3339
const FOUR_GIB: usize = 1 << 32;
3440

0 commit comments

Comments
 (0)