Skip to content

A Windows executable and the JIT allocate from mimalloc - #778

Merged
ASDAlexander77 merged 1 commit into
mainfrom
mimalloc-heap
Nov 8, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
mimalloc-heap

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

What

Under -mm=rc, -mm=own and -mm=none, a Windows program now allocates from mimalloc (v2.2.4, vendored in tslang/lib/mimalloc, MIT) instead of the process heap (HeapAlloc), whenever it is the only module allocating:

  • Executables - tslang --emit=exe links the new TypeScriptMimalloc.lib ahead of TypeScriptAsyncRuntime.lib. It defines the same five __tslang_heap_* entry points ProcessHeapPass renames malloc & co. to, so the async runtime's process-heap ProcessHeap.obj is never pulled in.
  • JIT - the program's heap functions go through wrappers that dispatch on a flag chosen once, before the program runs.

Why

Measured with --emit=exe --opt --opt_level=3, median of 11 runs, --process-heap → default:

program model ms
ray tracer, every vector/colour a heap object own 529 → 180 (2.9×)
20,000 arrays of 200 new objects rc / own 131 → 48 (2.7×)
5M strings built with + own 910 → 696 (1.3×)
ray tracer with @value vectors/colours own / rc 78 → 74 / 111 → 106

Allocation-heavy code gains a lot. Code that @value / stack promotion already keeps off the heap barely changes.

When the process heap stays

A block made by one module is routinely freed by another, so there must be one heap per process. Until a tslang-built DLL and its host can share mimalloc (the planned next step), these keep the process heap:

  • --emit=dll, and an executable that imports a library dynamically (importsSharedLibrary);
  • the JIT when any tslang library is loaded: the default library's DLL, a --shared-libs library, or a dynamic import (a JIT-cache unit's object is checked for tslang_load_library_permanently). So an ordinary JIT run with the default library still uses the process heap; the JIT gets mimalloc with --no-default-lib (as the test suite runs it);
  • -mm=gc (Boehm, unchanged), non-Windows targets (C library malloc, unchanged);
  • --process-heap (new flag), for heap debugging tools such as page heap / Application Verifier;
  • programs linked by other build systems against TypeScriptAsyncRuntime.lib (opt in by linking TypeScriptMimalloc.lib first).

Notes for reviewers

  • MimallocHeap.c is C on purpose: a C++ object carries RuntimeLibrary / _ITERATOR_DEBUG_LEVEL mismatch checks into a --di program linking the release-CRT package libraries. It adds #pragma comment(lib, "advapi32.lib"), which mimalloc's large-page code needs and tslang's link line lacks.
  • tslang.exe links mimalloc itself (TypeScriptMimallocCore, object library) for the JIT; --verbose prints JIT heap: mimalloc|process heap.
  • Packaging: the x86 runtime build (tslang/runtime/CMakeLists.txt) and the Windows release zip (create-release.yml) carry the library. tslang errors out, naming --process-heap, if TSLANG_LIB_PATH points at runtime libraries without it.
  • test-runner links the library into the rc/none/own programs it builds whole (single-file and multi-file static), so the suite exercises mimalloc; -shared tests keep the process heap.
  • The default library's C++ wrappers never hand a CRT block to TypeScript code to free (they pair with regexp_free_string, regexp_free, http_response_free), so moving TypeScript code off the CRT's heap is safe there.
  • Docs: new "The heap on Windows" section in docs/memory-models.md.

Testing

  • New test-compile-mimalloc-heap (mimalloc-heap.cmake): exe under own/rc/none links and runs on mimalloc (its exit statistics with MIMALLOC_SHOW_STATS=1), gc and --process-heap do not; JIT cached and uncached picks mimalloc, --process-heap does not; a program importing a --emit=dll library keeps the process heap as exe and under the JIT. Plus 00mimalloc_heap.ts under all four models, JIT and compiled.
  • Full ctest (Release): 6687/6687.
  • DefaultLib tests.ps1 for rc, none and own: release and debug, compiled and JIT, all pass (186/187, the skip is gc-only).
  • x86: rebuilt the 32-bit runtime; a 32-bit own/rc executable runs on mimalloc.
  • Not run locally: Linux (all new code is Windows-only).

🤖 Generated with Claude Code

Under -mm=rc, -mm=own and -mm=none a Windows program allocated from the process heap
(HeapAlloc). An executable that loads no library tslang built is one module, so it can own a
faster heap: tslang now links TypeScriptMimalloc.lib - mimalloc v2.2.4, vendored in
tslang/lib/mimalloc, behind the five __tslang_heap_* entry points - ahead of
TypeScriptAsyncRuntime.lib, whose process-heap definitions are then never pulled in.

- exe.cpp: the library is linked for x86/x64 unless the output is a DLL, the program imports a
  library dynamically, the model is gc, or --process-heap (new) asks for the process heap, for
  heap debugging tools such as page heap.
- jit.cpp: the program's heap functions dispatch on a flag chosen once before it runs: mimalloc,
  unless a library tslang built is loaded (the default library's DLL, --shared-libs) or the
  program imports one (a JIT-cache unit's object is checked for tslang_load_library_permanently).
  --verbose prints which.
- MimallocHeap.c is C so it carries no C++ runtime mismatch check into a --di program linking
  the release-CRT package libraries; it asks for advapi32, which mimalloc's large-page code needs.
- The x86 runtime build (tslang/runtime) and the Windows release zip carry the library too;
  test-runner links it into the rc/none/own programs it builds whole.

A library tslang built, and a program loading one, keep the process heap until both can share
one heap (next step).

Measured (--emit=exe, --process-heap -> default): own ray tracer with heap vectors 529 -> 180 ms,
20,000 arrays of 200 objects 131 -> 48 ms, 5M strings 910 -> 696 ms; @value ray tracers ~5%.
Tests: mimalloc-heap.cmake (exe, JIT cached/uncached, --process-heap, gc, DLL importer) and
00mimalloc_heap.ts under every model. ctest 6687/6687; DefaultLib tests.ps1 rc/none/own, release
and debug, compiled and JIT, all pass; a 32-bit executable runs on mimalloc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 2ac10ef into main Nov 8, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the mimalloc-heap branch November 8, 2026 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant