Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ jobs:
New-Item -ItemType Directory -Force -Path .\gcdll_stage\gcdll | Out-Null
Copy-Item -Path ..\3rdParty\gcdll\x64\release\lib\gc.lib, ..\3rdParty\gcdll\x64\release\bin\gc.dll -Destination .\gcdll_stage\gcdll -ErrorAction Stop
Get-ChildItem -Path ..\3rdParty\llvm\x64\release\lib\clang -Directory | ForEach-Object { $dest = ".\resourcedir_stage\lib\clang\$($_.Name)"; New-Item -ItemType Directory -Force -Path $dest | Out-Null; Copy-Item -Path (Join-Path $_.FullName "include") -Destination $dest -Recurse -ErrorAction Stop }
Get-ChildItem -Path .\tslang\msbuild\x64\release\bin\tslang.exe, .\tslang\msbuild\x64\release\bin\tsbindgen.exe, .\tslang\msbuild\x64\release\bin\TypeScriptRuntime.dll, .\tslang\msbuild\x64\release\bin\gc.dll, .\gc\msbuild\x64\release\${{ env.BUILD_TYPE }}\gc.lib, .\tslang\msbuild\x64\release\lib\TypeScriptAsyncRuntime.lib, ..\3rdParty\llvm\x64\release\bin\wasm-ld.exe, ..\TypeScriptCompilerDefaultLib\__build, .\gcdll_stage, .\resourcedir_stage, .\android_stage | Compress-Archive -DestinationPath ..\${{ env.ASSET_NAME }}-windows-x64.zip
Get-ChildItem -Path .\tslang\msbuild\x64\release\bin\tslang.exe, .\tslang\msbuild\x64\release\bin\tsbindgen.exe, .\tslang\msbuild\x64\release\bin\TypeScriptRuntime.dll, .\tslang\msbuild\x64\release\bin\gc.dll, .\gc\msbuild\x64\release\${{ env.BUILD_TYPE }}\gc.lib, .\tslang\msbuild\x64\release\lib\TypeScriptAsyncRuntime.lib, .\tslang\msbuild\x64\release\lib\TypeScriptMimalloc.lib, ..\3rdParty\llvm\x64\release\bin\wasm-ld.exe, ..\TypeScriptCompilerDefaultLib\__build, .\gcdll_stage, .\resourcedir_stage, .\android_stage | Compress-Archive -DestinationPath ..\${{ env.ASSET_NAME }}-windows-x64.zip
shell: pwsh

- name: Archive Zip of Windows Asset
Expand Down
35 changes: 35 additions & 0 deletions docs/memory-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,41 @@ two throwaway heap allocations of the same size back at the same point and the p
at the unpromoted speed (157 ms against 158). Expect the same kind of swing, in either direction,
from any change to what a program allocates early.

## The heap on Windows

Under `-mm=rc`, `-mm=own` and `-mm=none`, a Windows program allocates from
[mimalloc](https://github.com/microsoft/mimalloc) rather than the Windows process heap
(`HeapAlloc`):

- an executable `tslang --emit=exe` builds links `TypeScriptMimalloc.lib` (it ships beside
`TypeScriptAsyncRuntime.lib`);
- a program run with `--emit=jit` gets the same.

There is one exception, because a block allocated by one module is often freed by another. A
program that loads a library tslang built keeps the process heap, and so does that library. That
covers importing a `--emit=dll` library, the default library's DLL under the JIT, and a library
given with `--shared-libs`. A program you link yourself, against `TypeScriptAsyncRuntime.lib`, also
keeps the process heap unless you link `TypeScriptMimalloc.lib` ahead of it. Do that only for an
executable that loads no library tslang built.

`--process-heap` keeps the process heap anyway, for heap debugging tools such as page heap or
Application Verifier. `-mm=gc` allocates from the collector, and other platforms from the C
library's `malloc`; neither changes.

Measured, median of eleven runs, ahead-of-time build with `--opt --opt_level=3`, in milliseconds.
`--process-heap` → default:

| program | model | `--process-heap` → default |
| --- | --- | --- |
| ray tracer, every vector and colour an object on the heap | `-mm=own` | 529 → 180 (2.9×) |
| ray tracer, vectors and colours `@value` types | `-mm=own` | 78 → 74 |
| ray tracer, vectors and colours `@value` types | `-mm=rc` | 111 → 106 |
| 20,000 arrays of 200 new objects | `-mm=rc` / `-mm=own` | 131 → 48 (2.7×) |
| 5 million strings built by `+` | `-mm=own` | 910 → 696 (1.3×) |

The gain is in programs that make and drop many small objects, arrays and strings. Where `@value`
types or stack promotion already keep a program's data off the heap, the heap barely matters.

## Other limits of `-mm=rc`

- **Objects crossing between differently-managed modules are never freed.** If you link a
Expand Down
3 changes: 2 additions & 1 deletion tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@

// The process-heap allocator of a Windows module (TypeScript/ProcessHeap.h), which ProcessHeapPass
// calls instead of the C runtime's. Defined by the async runtime library a program links,
// TypeScriptRuntime.dll and the JIT.
// TypeScriptRuntime.dll and the JIT - and, served by mimalloc, by TypeScriptMimalloc.lib, which an
// executable links ahead of the async runtime.
#define PROCESS_HEAP_MALLOC "__tslang_heap_malloc"
#define PROCESS_HEAP_CALLOC "__tslang_heap_calloc"
#define PROCESS_HEAP_REALLOC "__tslang_heap_realloc"
Expand Down
4 changes: 4 additions & 0 deletions tslang/include/TypeScript/ProcessHeap.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
// list of its blocks per copy, while tslang.exe's own `malloc` is rpmalloc when LLVM is the
// prebuilt package. The process heap is the same heap in every module and every build.
//
// An executable that loads no library tslang built is one module, so it allocates from mimalloc
// instead: tslang links TypeScriptMimalloc.lib, whose definitions of the same entry points win over
// these (lib/TypeScriptMimalloc), and the JIT does the same for the program it runs (jit.cpp).
//
// Header-only, so each place that has to provide them - the async runtime library a program links,
// TypeScriptRuntime.dll and the JIT - compiles the same code.

Expand Down
1 change: 1 addition & 0 deletions tslang/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_subdirectory(TypeScriptDebugInfoPass)
add_subdirectory(TypeScriptExportPass)
add_subdirectory(TypeScriptRuntime)
add_subdirectory(TypeScriptAsyncRuntime)
add_subdirectory(TypeScriptMimalloc)
add_subdirectory(TypeScriptAliasPass)
add_subdirectory(TypeScriptMemAllocPass)
add_subdirectory(TsClangImporter)
28 changes: 28 additions & 0 deletions tslang/lib/TypeScriptMimalloc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# The heap a Windows executable built by tslang allocates from: mimalloc (lib/mimalloc, one
# translation unit) behind the process-heap entry points (MimallocHeap.c). exe.cpp links it ahead
# of TypeScriptAsyncRuntime. tslang.exe links the `TypeScriptMimallocCore` half alone, for the JIT.
# The same sources are built for x86 by tslang/runtime/CMakeLists.txt.
if (NOT WIN32)
return()
endif()

set(MIMALLOC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../mimalloc")

# mimalloc itself, its symbols prefixed mi_ - nothing else in the process is replaced
add_library(TypeScriptMimallocCore OBJECT "${MIMALLOC_DIR}/src/static.c")
target_include_directories(TypeScriptMimallocCore PUBLIC "${MIMALLOC_DIR}/include")
# not hardened: the guard pages and encoded free lists of MI_SECURE cost what this is for
target_compile_definitions(TypeScriptMimallocCore PRIVATE MI_SECURE=0)
if (MSVC)
# upstream's own code; its warnings are not ours to fix
target_compile_options(TypeScriptMimallocCore PRIVATE /W0)
endif()

add_library(TypeScriptMimalloc STATIC
MimallocHeap.c
$<TARGET_OBJECTS:TypeScriptMimallocCore>)
target_include_directories(TypeScriptMimalloc PRIVATE "${MIMALLOC_DIR}/include")
# beside TypeScriptAsyncRuntime.lib, where --tslang-lib-path / TSLANG_LIB_PATH point (the generator
# expression keeps a multi-config generator from adding a per-config subfolder)
set_target_properties(TypeScriptMimalloc PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "$<TARGET_FILE_DIR:TypeScriptAsyncRuntime>")
65 changes: 65 additions & 0 deletions tslang/lib/TypeScriptMimalloc/MimallocHeap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// The process-heap entry points (PROCESS_HEAP_* in TypeScript/Defines.h) served by mimalloc instead
// of the Windows process heap. A Windows executable tslang links links this library ahead of
// TypeScriptAsyncRuntime, so these definitions are the ones its calls resolve to and
// TypeScriptAsyncRuntime's ProcessHeap.cpp is never pulled in.
//
// One allocator per process is still the rule (TypeScript/ProcessHeap.h): a block made by one module
// is freed by another. A program linked whole - its own code, the static default library and
// everything else it links statically - is one module, so it can own a mimalloc of its own. A
// program that loads a library tslang built (--emit=dll, a dynamic import) does not get this
// library: that library allocates from the process heap, and so does the program (exe.cpp).
//
// The same rules as ProcessHeap.h where the C functions leave room: an allocation of nothing is a
// block of its own, and a block resized to nothing is freed and nothing is returned.
//
// C, not C++: it pulls in no C++ runtime library, whose debug/release checks (LNK2038) a `--di`
// program linking the release-CRT runtime libraries of a tslang package would trip on.

#ifdef _WIN32

#include <stddef.h>

#include <mimalloc.h>

// mimalloc asks for the large-page privilege through advapi32 (win_enable_large_os_pages), and
// neither tslang nor test-runner links advapi32 into a program otherwise
#pragma comment(lib, "advapi32.lib")

void *__tslang_heap_malloc(size_t size)
{
return mi_malloc(size ? size : 1);
}

void *__tslang_heap_calloc(size_t count, size_t size)
{
if (count == 0 || size == 0)
{
return mi_calloc(1, 1);
}

// mi_calloc refuses a count * size that overflows
return mi_calloc(count, size);
}

void *__tslang_heap_realloc(void *ptr, size_t size)
{
if (ptr && size == 0)
{
mi_free(ptr);
return NULL;
}

return mi_realloc(ptr, size);
}

void __tslang_heap_free(void *ptr)
{
mi_free(ptr);
}

void *__tslang_heap_aligned_alloc(size_t alignment, size_t size)
{
return mi_malloc_aligned(size ? size : 1, alignment);
}

#endif // _WIN32
21 changes: 21 additions & 0 deletions tslang/lib/mimalloc/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-2025 Microsoft Corporation, Daan Leijen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions tslang/lib/mimalloc/README.tslang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# mimalloc (vendored)

[mimalloc](https://github.com/microsoft/mimalloc) **v2.2.4** (commit `fbd8b99c2b828428947d70fdc046bb55609be93e`),
MIT licensed (see `LICENSE`). Only `include/` and `src/` are kept; the ETW manifest files under
`src/prim/windows/` and everything else of the upstream tree are left out.

Built as one translation unit (`src/static.c`) into `TypeScriptMimalloc` (see
`lib/TypeScriptMimalloc`) - the heap a Windows executable built by tslang allocates from - and into
tslang.exe, whose JIT gives it to the program it runs.

To update: replace `include/` and `src/` with the new release's, drop the ETW files again, and
update the version above.
66 changes: 66 additions & 0 deletions tslang/lib/mimalloc/include/mimalloc-new-delete.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* ----------------------------------------------------------------------------
Copyright (c) 2018-2020 Microsoft Research, Daan Leijen
This is free software; you can redistribute it and/or modify it under the
terms of the MIT license. A copy of the license can be found in the file
"LICENSE" at the root of this distribution.
-----------------------------------------------------------------------------*/
#pragma once
#ifndef MIMALLOC_NEW_DELETE_H
#define MIMALLOC_NEW_DELETE_H

// ----------------------------------------------------------------------------
// This header provides convenient overrides for the new and
// delete operations in C++.
//
// This header should be included in only one source file!
//
// On Windows, or when linking dynamically with mimalloc, these
// can be more performant than the standard new-delete operations.
// See <https://en.cppreference.com/w/cpp/memory/new/operator_new>
// ---------------------------------------------------------------------------
#if defined(__cplusplus)
#include <new>
#include <mimalloc.h>

#if defined(_MSC_VER) && defined(_Ret_notnull_) && defined(_Post_writable_byte_size_)
// stay consistent with VCRT definitions
#define mi_decl_new(n) mi_decl_nodiscard mi_decl_restrict _Ret_notnull_ _Post_writable_byte_size_(n)
#define mi_decl_new_nothrow(n) mi_decl_nodiscard mi_decl_restrict _Ret_maybenull_ _Success_(return != NULL) _Post_writable_byte_size_(n)
#else
#define mi_decl_new(n) mi_decl_nodiscard mi_decl_restrict
#define mi_decl_new_nothrow(n) mi_decl_nodiscard mi_decl_restrict
#endif

void operator delete(void* p) noexcept { mi_free(p); };
void operator delete[](void* p) noexcept { mi_free(p); };

void operator delete (void* p, const std::nothrow_t&) noexcept { mi_free(p); }
void operator delete[](void* p, const std::nothrow_t&) noexcept { mi_free(p); }

mi_decl_new(n) void* operator new(std::size_t n) noexcept(false) { return mi_new(n); }
mi_decl_new(n) void* operator new[](std::size_t n) noexcept(false) { return mi_new(n); }

mi_decl_new_nothrow(n) void* operator new (std::size_t n, const std::nothrow_t& tag) noexcept { (void)(tag); return mi_new_nothrow(n); }
mi_decl_new_nothrow(n) void* operator new[](std::size_t n, const std::nothrow_t& tag) noexcept { (void)(tag); return mi_new_nothrow(n); }

#if (__cplusplus >= 201402L || _MSC_VER >= 1916)
void operator delete (void* p, std::size_t n) noexcept { mi_free_size(p,n); };
void operator delete[](void* p, std::size_t n) noexcept { mi_free_size(p,n); };
#endif

#if (__cplusplus > 201402L || defined(__cpp_aligned_new))
void operator delete (void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
void operator delete[](void* p, std::align_val_t al) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
void operator delete (void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
void operator delete[](void* p, std::size_t n, std::align_val_t al) noexcept { mi_free_size_aligned(p, n, static_cast<size_t>(al)); };
void operator delete (void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }
void operator delete[](void* p, std::align_val_t al, const std::nothrow_t&) noexcept { mi_free_aligned(p, static_cast<size_t>(al)); }

void* operator new (std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
void* operator new[](std::size_t n, std::align_val_t al) noexcept(false) { return mi_new_aligned(n, static_cast<size_t>(al)); }
void* operator new (std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_new_aligned_nothrow(n, static_cast<size_t>(al)); }
void* operator new[](std::size_t n, std::align_val_t al, const std::nothrow_t&) noexcept { return mi_new_aligned_nothrow(n, static_cast<size_t>(al)); }
#endif
#endif

#endif // MIMALLOC_NEW_DELETE_H
68 changes: 68 additions & 0 deletions tslang/lib/mimalloc/include/mimalloc-override.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* ----------------------------------------------------------------------------
Copyright (c) 2018-2020 Microsoft Research, Daan Leijen
This is free software; you can redistribute it and/or modify it under the
terms of the MIT license. A copy of the license can be found in the file
"LICENSE" at the root of this distribution.
-----------------------------------------------------------------------------*/
#pragma once
#ifndef MIMALLOC_OVERRIDE_H
#define MIMALLOC_OVERRIDE_H

/* ----------------------------------------------------------------------------
This header can be used to statically redirect malloc/free and new/delete
to the mimalloc variants. This can be useful if one can include this file on
each source file in a project (but be careful when using external code to
not accidentally mix pointers from different allocators).
-----------------------------------------------------------------------------*/

#include <mimalloc.h>

// Standard C allocation
#define malloc(n) mi_malloc(n)
#define calloc(n,c) mi_calloc(n,c)
#define realloc(p,n) mi_realloc(p,n)
#define free(p) mi_free(p)

#define strdup(s) mi_strdup(s)
#define strndup(s,n) mi_strndup(s,n)
#define realpath(f,n) mi_realpath(f,n)

// Microsoft extensions
#define _expand(p,n) mi_expand(p,n)
#define _msize(p) mi_usable_size(p)
#define _recalloc(p,n,c) mi_recalloc(p,n,c)

#define _strdup(s) mi_strdup(s)
#define _strndup(s,n) mi_strndup(s,n)
#define _wcsdup(s) (wchar_t*)mi_wcsdup((const unsigned short*)(s))
#define _mbsdup(s) mi_mbsdup(s)
#define _dupenv_s(b,n,v) mi_dupenv_s(b,n,v)
#define _wdupenv_s(b,n,v) mi_wdupenv_s((unsigned short*)(b),n,(const unsigned short*)(v))

// Various Posix and Unix variants
#define reallocf(p,n) mi_reallocf(p,n)
#define malloc_size(p) mi_usable_size(p)
#define malloc_usable_size(p) mi_usable_size(p)
#define malloc_good_size(sz) mi_malloc_good_size(sz)
#define cfree(p) mi_free(p)

#define valloc(n) mi_valloc(n)
#define pvalloc(n) mi_pvalloc(n)
#define reallocarray(p,s,n) mi_reallocarray(p,s,n)
#define reallocarr(p,s,n) mi_reallocarr(p,s,n)
#define memalign(a,n) mi_memalign(a,n)
#define aligned_alloc(a,n) mi_aligned_alloc(a,n)
#define posix_memalign(p,a,n) mi_posix_memalign(p,a,n)
#define _posix_memalign(p,a,n) mi_posix_memalign(p,a,n)

// Microsoft aligned variants
#define _aligned_malloc(n,a) mi_malloc_aligned(n,a)
#define _aligned_realloc(p,n,a) mi_realloc_aligned(p,n,a)
#define _aligned_recalloc(p,s,n,a) mi_aligned_recalloc(p,s,n,a)
#define _aligned_msize(p,a,o) mi_usable_size(p)
#define _aligned_free(p) mi_free(p)
#define _aligned_offset_malloc(n,a,o) mi_malloc_aligned_at(n,a,o)
#define _aligned_offset_realloc(p,n,a,o) mi_realloc_aligned_at(p,n,a,o)
#define _aligned_offset_recalloc(p,s,n,a,o) mi_recalloc_aligned_at(p,s,n,a,o)

#endif // MIMALLOC_OVERRIDE_H
Loading
Loading