Repository navigation
A library tslang built allocates from the program's heap; mimalloc on Linux - #779
Merged
Merged
Conversation
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
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
__declspec(dllexport):TypeScriptMimalloc's in an executable, the async runtime's process-heap ones otherwise, tslang.exe's own wrappers under the JIT.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.__tslang_heap_sharedto say it borrows the heap (a DLL link forces it in with-include:, even for a library that never allocates). A program on mimalloc (TypeScriptMimallocexports__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'sDynamicRuntime, and the JIT's loader). The check maps the DLL withDONT_RESOLVE_DLL_REFERENCES(ProcessHeap.h,librarySharesHeap).--shared-libslibrary without the marker keeps it on the process heap.exe.cppand test-runner give an executable importing libraries mimalloc too, so every-sharedrc/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=dlllibrary.Linux
A second commit brings mimalloc to Linux, where it is simpler: an executable that defines
mallocprovides it to every shared object in the process (ELF interposition).TypeScriptMimallocon Linux islibTypeScriptMimalloc.a, built withMI_MALLOC_OVERRIDE.exe.cpplinks it--whole-archiveinto 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.-fsanitizebuild), so a JIT run and everything it loads share tslang's mimalloc;--process-heaphas no effect under the JIT on Linux..soline) 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×),@valueray tracers and strings unchanged within 2%. glibc'smallocis much faster than the Windows process heap, so the gains are smaller than on Windows.Notes for reviewers
GetModuleHandleW/GetProcAddressonly)..defno longer lists the heap entry points: they export themselves now.-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
mallocand report mimalloc stats (MIMALLOC_SHOW_STATS=1); gc and--process-heapexecutables keep libc's; a JIT run reports mimalloc stats.mimalloc-heap.cmakenow expects a program importing a--emit=dlllibrary 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-libslibrary has no marker (a system DLL).A C host exporting
TypeScriptMimalloc's entry points loaded a tslang DLL: the DLL's own__tslang_heap_mallocblock is owned by the host's mimalloc (mi_check_owned); without the exports it is not.Full ctest (Release): 6687/6687, including every
-sharedtest on the shared heap.DefaultLib rebuilt (rc/none/own, release + debug on the release CRT, as the release job does);
tests.ps1all 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