Skip to content

A library tslang built allocates from the program's heap; mimalloc on Linux - #779

Merged
ASDAlexander77 merged 2 commits into
mainfrom
mimalloc-dll
Nov 8, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
mimalloc-dll

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

What

The second step of the mimalloc heap (#778 did executables and the JIT): a library tslang built now allocates from the program's heap, mimalloc included, instead of forcing the program back onto the process heap.

A block made by one module is routinely freed by another, so a process has one heap, and it is the program's:

  • The program exports it. Every heap entry point is __declspec(dllexport): TypeScriptMimalloc's in an executable, the async runtime's process-heap ones otherwise, tslang.exe's own wrappers under the JIT.
  • Libraries borrow it. lib/ProcessHeapExports.inc (in every library tslang builds, and in TypeScriptRuntime.dll) looks the five entry points up in the process's executable at its first allocation - lock-free, all five or none - and calls them. In the program's own module, or under a host that exports none (one tslang did not build, or an older tslang), it is the process heap as before.
  • Old libraries are caught. A library exports __tslang_heap_shared to say it borrows the heap (a DLL link forces it in with -include:, even for a library that never allocates). A program on mimalloc (TypeScriptMimalloc exports __tslang_heap_mimalloc) refuses a library without it - one an older tslang built, which keeps a heap of its own - before any of its code runs, and says to rebuild it (the executable's DynamicRuntime, and the JIT's loader). The check maps the DLL with DONT_RESOLVE_DLL_REFERENCES (ProcessHeap.h, librarySharesHeap).
  • The JIT chooses its heap before loading any library (their global constructors already allocate). A --shared-libs library without the marker keeps it on the process heap.
  • exe.cpp and test-runner give an executable importing libraries mimalloc too, so every -shared rc/none/own test now runs its library on the program's mimalloc.

So an ordinary JIT run - which loads the default library's DLL - now gets mimalloc, as do executables that import a --emit=dll library.

Linux

A second commit brings mimalloc to Linux, where it is simpler: an executable that defines malloc provides it to every shared object in the process (ELF interposition).

  • TypeScriptMimalloc on Linux is libTypeScriptMimalloc.a, built with MI_MALLOC_OVERRIDE. exe.cpp links it --whole-archive into an executable under rc/own/none (not -shared, gc, Android, or with --process-heap). The default library's .so, libraries tslang built and libc all allocate from it: no marker, and older libraries need no rebuild.
  • tslang links the same override (except in a -fsanitize build), so a JIT run and everything it loads share tslang's mimalloc; --process-heap has no effect under the JIT on Linux.
  • The test runner's Linux program link lines (not the .so line) and the Linux release package include the library.
  • docs/memory-models.md: "The heap on Windows" became "The heap", with Windows and Linux parts and timings for both.

Linux timings (WSL 2, glibc, median of 11, ms, --process-heap → default): arrays 68 → 35 (1.9×), heap-object ray tracer 131 → 118 (1.1×), @value ray tracers and strings unchanged within 2%. glibc's malloc is much faster than the Windows process heap, so the gains are smaller than on Windows.

Notes for reviewers

  • The default library has to be rebuilt with this runtime for its DLL to carry the marker. An older build is not an error: the JIT stays on the process heap with it. The release workflow builds the default library after the compiler, so a release gets a matching one.
  • Lazily resolved, because a library's own global constructors may allocate before anything else in it runs; resolution is safe under the loader lock (GetModuleHandleW / GetProcAddress only).
  • TypeScriptRuntime.dll's .def no longer lists the heap entry points: they export themselves now.
  • x86: the marker's -include: uses the decorated name (___tslang_heap_shared).

Testing

  • Linux (WSL Ubuntu, official LLVM 22.1.8 package, gc 8.2.12, as the Linux CI): full ctest 6659/6659. rc/own/none executables define malloc and report mimalloc stats (MIMALLOC_SHOW_STATS=1); gc and --process-heap executables keep libc's; a JIT run reports mimalloc stats.

  • mimalloc-heap.cmake now expects a program importing a --emit=dll library to run on mimalloc as an executable and under the JIT (cached and uncached), the process heap with --process-heap, and the process heap under the JIT when a --shared-libs library has no marker (a system DLL).

  • A C host exporting TypeScriptMimalloc's entry points loaded a tslang DLL: the DLL's own __tslang_heap_malloc block is owned by the host's mimalloc (mi_check_owned); without the exports it is not.

  • Full ctest (Release): 6687/6687, including every -shared test on the shared heap.

  • DefaultLib rebuilt (rc/none/own, release + debug on the release CRT, as the release job does); tests.ps1 all pass for all three models, release and debug, compiled and JIT - the JIT passes now on mimalloc with the default library's DLL.

  • x86: rebuilt runtime; a 32-bit DLL carries the marker and an importing 32-bit executable runs on mimalloc.

  • An old default-library DLL (installed package) given to the JIT keeps it on the process heap.

  • Not tested (Windows): the refusal message for an old library imported dynamically by an executable (no such library could be produced locally).

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits November 8, 2026 01:25
A block made by one module is routinely freed by another, so a process has one heap. Until now a
program that loaded a library tslang built - a dynamic import, the default library's DLL under the
JIT, a --shared-libs library - stayed on the process heap so its blocks and the library's matched.
Now the heap is the program's, mimalloc included, and the libraries borrow it:

- Every heap entry point is exported: TypeScriptMimalloc's in an executable, the async runtime's
  process-heap ones otherwise, and tslang.exe's own under the JIT.
- lib/ProcessHeapExports.inc, in every library tslang builds and in TypeScriptRuntime.dll, looks
  the five up in the process's executable at its first allocation (lock-free, all five or none)
  and calls them; in the program's own module, or under a host that exports none, it is the
  process heap as before. It exports `__tslang_heap_shared` to say so, and a DLL link forces it in
  even when the library never allocates.
- A program on mimalloc (TypeScriptMimalloc exports `__tslang_heap_mimalloc`) refuses, before any
  of its code runs, a library without the marker - one an older tslang built, which keeps a heap
  of its own - and says to rebuild it (DynamicRuntime, and the JIT's loader). The JIT chooses its
  heap before loading any library, and a --shared-libs library without the marker keeps it on the
  process heap (ProcessHeap.h, librarySharesHeap).
- exe.cpp and test-runner give an executable importing libraries mimalloc too; every -shared
  rc/none/own test now runs its library on the program's mimalloc.

Checked with a host that exports mimalloc's entry points: the library's own allocation is a
mimalloc block; without the exports it is not. An ordinary JIT run, with the default library's
DLL, now gets mimalloc. The default library has to be rebuilt to carry the marker; an older build
keeps the JIT on the process heap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On Linux, an executable that defines malloc provides it to every shared
object in the process, so TypeScriptMimalloc is built with
MI_MALLOC_OVERRIDE and linked whole into an executable tslang builds
under rc, own and none. The default library's .so, a library tslang
built, and libc all allocate from it; no marker is needed and older
libraries need no rebuild. --process-heap keeps libc's malloc.

tslang links the same override (except in sanitizer builds), so a
program run with --emit=jit and every library it loads share tslang's
mimalloc.

The test runner's Linux link lines and the Linux release package
include the library; docs/memory-models.md covers both systems and gives
Linux timings (arrays 1.9x, heap-object ray tracer 1.1x, strings and
@value code unchanged).

Linux (WSL, prebuilt LLVM 22.1.8): ctest 6659/6659.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77 ASDAlexander77 changed the title A library tslang built allocates from the program's heap A library tslang built allocates from the program's heap; mimalloc on Linux Nov 8, 2026
@ASDAlexander77
ASDAlexander77 merged commit 3d3a0f7 into main Nov 8, 2026
2 of 3 checks passed
@ASDAlexander77
ASDAlexander77 deleted the mimalloc-dll branch November 8, 2026 11:57
ASDAlexander77 added a commit that referenced this pull request Nov 8, 2026
Since #779 ProcessHeapExports.inc exported __tslang_heap_malloc, calloc,
realloc, free and aligned_alloc from every module that links the async
runtime, libraries included. A library's exports become its import
library's, so a DLL linking the default library's DLL - which comes first
on the link line - took the five from TypeScriptDefaultLib.lib, then
pulled ProcessHeap.obj in for -include:__tslang_heap_shared and had each
twice:

  TypeScriptAsyncRuntime.lib(ProcessHeap.obj) : error LNK2005:
  __tslang_heap_free already defined in
  TypeScriptDefaultLib.lib(TypeScriptDefaultLib.dll)

Any --emit=dll that allocates against a default library rebuilt since
#779 failed this way; test-compile-shared-defaultlib-data shows it once
that library is built.

Only the program's entry points are ever looked up (GetProcAddress on the
executable), and the ones that lend a heap - TypeScriptMimalloc's and
tslang.exe's under the JIT - are defined and exported apart. These are the
process heap, which a library falls back to when the program exports
none, so exporting them added nothing. The marker __tslang_heap_shared
stays exported.

The default library has to be rebuilt for its import library to lose the
five.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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