Repository navigation
A Windows executable and the JIT allocate from mimalloc - #778
Merged
Merged
Conversation
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>
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
Under
-mm=rc,-mm=ownand-mm=none, a Windows program now allocates from mimalloc (v2.2.4, vendored intslang/lib/mimalloc, MIT) instead of the process heap (HeapAlloc), whenever it is the only module allocating:tslang --emit=exelinks the newTypeScriptMimalloc.libahead ofTypeScriptAsyncRuntime.lib. It defines the same five__tslang_heap_*entry pointsProcessHeapPassrenamesmalloc& co. to, so the async runtime's process-heapProcessHeap.objis never pulled in.Why
Measured with
--emit=exe --opt --opt_level=3, median of 11 runs,--process-heap→ default:+@valuevectors/coloursAllocation-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);--shared-libslibrary, or a dynamic import (a JIT-cache unit's object is checked fortslang_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 librarymalloc, unchanged);--process-heap(new flag), for heap debugging tools such as page heap / Application Verifier;TypeScriptAsyncRuntime.lib(opt in by linkingTypeScriptMimalloc.libfirst).Notes for reviewers
MimallocHeap.cis C on purpose: a C++ object carriesRuntimeLibrary/_ITERATOR_DEBUG_LEVELmismatch checks into a--diprogram 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.TypeScriptMimallocCore, object library) for the JIT;--verboseprintsJIT heap: mimalloc|process heap.tslang/runtime/CMakeLists.txt) and the Windows release zip (create-release.yml) carry the library. tslang errors out, naming--process-heap, ifTSLANG_LIB_PATHpoints at runtime libraries without it.-sharedtests keep the process heap.regexp_free_string,regexp_free,http_response_free), so moving TypeScript code off the CRT's heap is safe there.docs/memory-models.md.Testing
test-compile-mimalloc-heap(mimalloc-heap.cmake): exe under own/rc/none links and runs on mimalloc (its exit statistics withMIMALLOC_SHOW_STATS=1), gc and--process-heapdo not; JIT cached and uncached picks mimalloc,--process-heapdoes not; a program importing a--emit=dlllibrary keeps the process heap as exe and under the JIT. Plus00mimalloc_heap.tsunder all four models, JIT and compiled.tests.ps1for rc, none and own: release and debug, compiled and JIT, all pass (186/187, the skip is gc-only).🤖 Generated with Claude Code