mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-01 07:33:12 +08:00
A large program's hot code spans hundreds of 4 KiB pages, more than the instruction TLB holds. tsc-ts has 12 MB of code, and 90% of its main-thread samples at --checkers 1 fall in 274 distinct code pages. `scriptc build --large-pages` links the new runtime unit scr_text_pages.c, whose constructor moves the executable's code onto 2 MiB transparent huge pages before main. It copies the executable segment, from its first to its last 2 MiB boundary, into anonymous memory advised MADV_HUGEPAGE, makes the copy read-only and executable, and moves it over the original range with one mremap, so addresses and instructions stay the same. The move runs only on Linux, only when THP is "always" or "madvise", and only in executables. If a step fails, the code stays file-mapped. Like Node's --use-largepages, it is off by default: without the flag the unit is not linked, and executables are unchanged. The move costs about 3 ms per 10 MiB of code at startup, and all of the moved code stays resident as private memory. The flag is part of the executable cache key and the runtime-pack selection, and is refused for non-executable output, like --detached-exit.