react-nitro-hermes 0.0.1-alpha.8 → 0.0.1-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hermes/CMakeLists.txt +874 -0
- package/hermes/android/HostObjectJniTest.cpp +178 -0
- package/hermes/android/build-logic/build.gradle.kts +31 -0
- package/hermes/android/build-logic/settings.gradle.kts +17 -0
- package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/helpers/internal/SerializeHermesCompilerPackageJson.kt +33 -0
- package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/plugins/internal/TestUtilsPlugin.kt +80 -0
- package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/tasks/internal/CustomExecTask.kt +46 -0
- package/hermes/android/build-logic/src/main/kotlin/publish.gradle.kts +82 -0
- package/hermes/android/build.gradle.kts +332 -0
- package/hermes/android/cppruntime/build.gradle +72 -0
- package/hermes/android/cppruntime/src/main/AndroidManifest.xml +2 -0
- package/hermes/android/cppruntime/src/main/cpp/CMakeLists.txt +8 -0
- package/hermes/android/cppruntime/src/main/cpp/stub.cpp +12 -0
- package/hermes/android/cross-compile/swift-libicu.patch +13 -0
- package/hermes/android/cross-compile/toolchain-android-arm-api13-gnustl.cmake +13 -0
- package/hermes/android/cross-compile/toolchain-android-arm-api15-gnustl.cmake +14 -0
- package/hermes/android/gradle/libs.versions.toml +30 -0
- package/hermes/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/hermes/android/gradle/wrapper/gradle-wrapper.properties +7 -0
- package/hermes/android/gradle.properties +3 -0
- package/hermes/android/gradlew +248 -0
- package/hermes/android/gradlew.bat +93 -0
- package/hermes/android/intltest/build.gradle +147 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/CMakeLists.txt +45 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.cpp +68 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.h +44 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.java +45 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogueTestData.java +32 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesInstrumentationTest.java +150 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java +147 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlTest262.java +470 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesRuntimeFactory.cpp +73 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.cpp +90 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.h +62 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.java +155 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.cpp +48 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.h +36 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoad.cpp +13 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoadEpilogue.cpp +13 -0
- package/hermes/android/intltest/src/main/AndroidManifest.xml +2 -0
- package/hermes/android/ios-artifacts/build.gradle.kts +65 -0
- package/hermes/android/settings.gradle.kts +27 -0
- package/hermes/cmake/modules/Hermes.cmake +481 -0
- package/hermes/cmake/modules/Lit.cmake +96 -0
- package/hermes/doc/Branches.md +38 -0
- package/hermes/doc/BuildingAndRunning.md +121 -0
- package/hermes/doc/CodingStandards.md +112 -0
- package/hermes/doc/CrossCompilation.md +79 -0
- package/hermes/doc/Design.md +217 -0
- package/hermes/doc/Emscripten.md +156 -0
- package/hermes/doc/Features.md +143 -0
- package/hermes/doc/GCSafeCoding.md +563 -0
- package/hermes/doc/GenGC.md +300 -0
- package/hermes/doc/Hades.md +344 -0
- package/hermes/doc/HighLevelOptimizations.md +37 -0
- package/hermes/doc/IR.md +1306 -0
- package/hermes/doc/IntlAPIs.md +196 -0
- package/hermes/doc/JIT.md +999 -0
- package/hermes/doc/LazyEvalCompilation.md +84 -0
- package/hermes/doc/MemoryProfilers.md +316 -0
- package/hermes/doc/Modules.md +144 -0
- package/hermes/doc/Optimizer.md +83 -0
- package/hermes/doc/PerfProfiling.md +29 -0
- package/hermes/doc/ProhibitInvoke.md +329 -0
- package/hermes/doc/ReactNative.md +40 -0
- package/hermes/doc/ReactNativeIntegration.md +94 -0
- package/hermes/doc/RegExp.md +24 -0
- package/hermes/doc/SpecIncompat.md +85 -0
- package/hermes/doc/Strings.md +140 -0
- package/hermes/doc/TypedLanguage.md +432 -0
- package/hermes/doc/VM.md +174 -0
- package/hermes/doc/blog/2024-11-09-octane-benchmark-jit-progress.md +17 -0
- package/hermes/doc/blog/2024-12-19-static-hermes-update-dec-2024.md +47 -0
- package/hermes/doc/blog/2024-12-23-compiling-javascript-to-wasm.md +267 -0
- package/hermes/doc/blog/2025-06-09-jsi-runtime-data-apis.md +23 -0
- package/hermes/doc/blog/2025-07-15-static-h-performance-june-2025.md +18 -0
- package/hermes/doc/blog/2025-11-02-hermes-compilation-runtime-modes.md +23 -0
- package/hermes/doc/blog/2025-11-06-hermes-memory-modes.md +35 -0
- package/hermes/doc/blog/2025-11-24-json-parsing-performance.md +17 -0
- package/hermes/doc/blog/2025-12-02-iserialization.md +11 -0
- package/hermes/doc/blog/2025-12-12-new-way-to-contribute.md +68 -0
- package/hermes/doc/blog/2026-06-05-new-hermes-stable-release.md +51 -0
- package/hermes/doc/blog/README.md +25 -0
- package/hermes/doc/blog/images/2024-11-09-octane-pi3.jpeg +0 -0
- package/hermes/doc/blog/images/2024-11-09-octane-pi5.jpeg +0 -0
- package/hermes/doc/blog/images/2025-07-15-perf-hv32-interpreter.jpeg +0 -0
- package/hermes/doc/blog/images/2025-07-15-perf-hv32-jit.jpeg +0 -0
- package/hermes/doc/icu_license.txt +420 -0
- package/hermes/doc/img/heap_snapshot_retainers.png +0 -0
- package/hermes/doc/img/logo.svg +1 -0
- package/hermes/doc/img/object_captured_by_environment.png +0 -0
- package/hermes/doc/img/root_categories.png +0 -0
- package/hermes/doc/plans/ir-type/flow-type-system.md +324 -0
- package/hermes/doc/plans/ir-type/ir-type-system-v2-design.md +652 -0
- package/hermes/doc/plans/ir-type/ir-type-system.md +220 -0
- package/hermes/doc/plans/ir-type/ir-type-v2-implementation.md +1187 -0
- package/hermes/doc/plans/ir-type/memory.md +48 -0
- package/hermes/doc/plans/ir-type/phase1-steps.md +512 -0
- package/hermes/doc/plans/ir-type/progress.md +239 -0
- package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-design.md +203 -0
- package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-results.md +71 -0
- package/hermes/doc/superpowers/plans/2026-07-28-worker-arraybuffer-input.md +597 -0
- package/hermes/doc/superpowers/plans/2026-08-18-jitemitter-split.md +1149 -0
- package/hermes/doc/superpowers/plans/2026-08-20-jit-type-asserts.md +969 -0
- package/hermes/doc/superpowers/plans/2026-08-23-jit-shared-compiler-driver.md +478 -0
- package/hermes/doc/superpowers/plans/2026-08-23-x86-64-jit-skeleton.md +797 -0
- package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-arith.md +444 -0
- package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-calls.md +261 -0
- package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-props.md +240 -0
- package/hermes/doc/superpowers/plans/2026-10-26-regexp-bytecode-sharing.md +2197 -0
- package/hermes/doc/superpowers/plans/2026-10-28-regexp-start-filter-and-exec-fast-paths.md +3981 -0
- package/hermes/doc/superpowers/specs/2026-07-28-worker-arraybuffer-input-design.md +206 -0
- package/hermes/doc/superpowers/specs/2026-08-18-jitemitter-split-design.md +530 -0
- package/hermes/doc/superpowers/specs/2026-08-20-jit-type-asserts-design.md +360 -0
- package/hermes/doc/superpowers/specs/2026-08-23-x86-64-jit-design.md +246 -0
- package/hermes/doc/superpowers/specs/2026-08-26-icu-free-normalize-design.md +355 -0
- package/hermes/doc/superpowers/specs/2026-08-27-icu-free-convert-to-case-design.md +305 -0
- package/hermes/doc/superpowers/specs/2026-08-27-icu-free-locale-compare-design.md +429 -0
- package/hermes/doc/superpowers/specs/2026-08-29-icu-free-date-format-design.md +297 -0
- package/hermes/doc/superpowers/specs/2026-10-26-regexp-bytecode-sharing-design.md +605 -0
- package/hermes/doc/superpowers/specs/2026-10-28-regexp-start-filter-and-exec-fast-paths-design.md +551 -0
- package/hermes/doc/typescript-stripping.md +90 -0
- package/hermes/external/CMakeLists.txt +20 -0
- package/hermes/utils/IntlGen/IntlGen.java +531 -0
- package/hermes/utils/bench_with_stats.py +827 -0
- package/hermes/utils/build-apple-framework.sh +323 -0
- package/hermes/utils/chrome_snapshot_parser.py +210 -0
- package/hermes/utils/cmake-dev-build.sh +18 -0
- package/hermes/utils/commons.sh +55 -0
- package/hermes/utils/dump-cpp-funcs.sh +3 -0
- package/hermes/utils/es5objects.js +198 -0
- package/hermes/utils/format.sh +145 -0
- package/hermes/utils/gen-promise-internal-bc.sh +66 -0
- package/hermes/utils/genUnicodeTable.py +2503 -0
- package/hermes/utils/hermes-lit/CMakeLists.txt +22 -0
- package/hermes/utils/hermes-lit/hermes-lit.in +15 -0
- package/hermes/utils/jit/README.md +134 -0
- package/hermes/utils/jit/jit-diff.sh +160 -0
- package/hermes/utils/jit/jit-dump.sh +193 -0
- package/hermes/utils/lit-expect-gen/generate.py +217 -0
- package/hermes/utils/promise/index.js +15 -0
- package/hermes/utils/promise/package.json +13 -0
- package/hermes/utils/promise/rollup.config.js +19 -0
- package/hermes/utils/promise/yarn.lock +247 -0
- package/hermes/utils/scripts/hermes/consts.js +26 -0
- package/hermes/utils/scripts/hermes/create-tarball.js +75 -0
- package/hermes/utils/scripts/hermes/get-hermes-version.js +57 -0
- package/hermes/utils/scripts/hermes/hermes-utils.js +122 -0
- package/hermes/utils/scripts/hermes/package.json +6 -0
- package/hermes/utils/scripts/hermes/prepare-artifacts.js +113 -0
- package/hermes/utils/scripts/hermes/publish-artifacts.js +109 -0
- package/hermes/utils/scripts/hermes/publish-npm.js +96 -0
- package/hermes/utils/scripts/hermes/scm-utils.js +41 -0
- package/hermes/utils/scripts/hermes/set-artifacts-version.js +64 -0
- package/hermes/utils/scripts/hermes/version-utils.js +98 -0
- package/hermes/utils/scripts/hermes/yarn.lock +240 -0
- package/hermes/utils/summarize_sizes.py +184 -0
- package/hermes/utils/symbolicate-nativecall-stats.py +112 -0
- package/hermes/utils/test_runner.py +13 -0
- package/hermes/utils/testsuite/README.facebook +3 -0
- package/hermes/utils/testsuite/README.md +69 -0
- package/hermes/utils/testsuite/cli.py +547 -0
- package/hermes/utils/testsuite/es_ast.py +337 -0
- package/hermes/utils/testsuite/external/LICENSE +28 -0
- package/hermes/utils/testsuite/external/README.facebook +8 -0
- package/hermes/utils/testsuite/external/__init__.py +0 -0
- package/hermes/utils/testsuite/external/_monkeyYaml.py +164 -0
- package/hermes/utils/testsuite/external/parse_test262.py +141 -0
- package/hermes/utils/testsuite/hermes.py +522 -0
- package/hermes/utils/testsuite/preprocess.py +365 -0
- package/hermes/utils/testsuite/progress.py +377 -0
- package/hermes/utils/testsuite/skiplist.json +4356 -0
- package/hermes/utils/testsuite/skiplist.py +270 -0
- package/hermes/utils/testsuite/test_run_defs.py +435 -0
- package/hermes/utils/testsuite/typing_defs.py +52 -0
- package/hermes/utils/testsuite/utils.py +214 -0
- package/hermes/utils/tidy.sh +22 -0
- package/hermes/utils/update-fbcode-hermes.sh +53 -0
- package/hermes/utils/updateErrorTest.py +193 -0
- package/hermes/utils/wasm-compile.sh +65 -0
- package/package.json +1 -1
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: gengc
|
|
3
|
+
title: The GenGC Garbage Collector
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
GenGC used to be the default garbage collector for Hermes, and aims to provide a
|
|
7
|
+
generational garbage collector that returns memory to the OS aggressively and
|
|
8
|
+
frequently. The newer GC is called [Hades](./Hades.md), and it has much lower
|
|
9
|
+
pause times than GenGC. We recommend most users use Hades instead.
|
|
10
|
+
|
|
11
|
+
Throughout this document, we will refer to the **heap**, which in this context
|
|
12
|
+
means the memory space where JS objects reside. This is separate from the C and
|
|
13
|
+
C++ `malloc` heap, and in general if this document says "heap" it means the
|
|
14
|
+
space for JS objects.
|
|
15
|
+
|
|
16
|
+
# Heap Segments
|
|
17
|
+
|
|
18
|
+
The heap is made out of many fixed-size regions of memory known as
|
|
19
|
+
**heap segments** (or more briefly as **segments**). Currently these segments
|
|
20
|
+
are 4 MiB, but this can be changed with the CMake build configuration variable
|
|
21
|
+
`-DHERMESVM_LOG_HEAP_SEGMENT_SIZE=number`. Memory is acquired and released on a
|
|
22
|
+
per-segment basis. Segments are allocated using `mmap` on POSIX systems and
|
|
23
|
+
`VirtualAlloc` on Windows systems. These functions allow virtual memory to be
|
|
24
|
+
requested for some space, and allows us to unmap subregions so that alignment
|
|
25
|
+
can be guaranteed. This is done in separate regions because we found that a lot
|
|
26
|
+
of devices have trouble allocating enough virtual memory upfront to have the
|
|
27
|
+
entire heap be contiguous. By allowing it to grow virtual address space usage
|
|
28
|
+
with the size of the heap (and shrink as well), it works on a broader set of
|
|
29
|
+
devices. We chose 4 MiB as the size of segments because it was small enough to
|
|
30
|
+
not exhaust virtual address space, while also being large enough to not have a
|
|
31
|
+
lot of overhead in managing the segments.
|
|
32
|
+
|
|
33
|
+
Each heap segment is aligned to begin on a pointer address that is a multiple
|
|
34
|
+
of its size. For example, if segments are 4 MiB wide, then their start
|
|
35
|
+
addresses are aligned on a 4 MiB boundary (the low 22 bits are all 0). This
|
|
36
|
+
means for any pointer into the JS heap, you can get the segment start address
|
|
37
|
+
with a simple bitwise-and operation. All metadata for the segment is stored at
|
|
38
|
+
the front of the segment.
|
|
39
|
+
|
|
40
|
+
At the beginning of a segment, there is a small amount of memory reserved for
|
|
41
|
+
some metadata about the heap. Specifically, the following things are kept there:
|
|
42
|
+
- Segment ID: explained in [Compressed Pointers](#compressed-pointers)
|
|
43
|
+
- CardTable: explained in [Generations](#generations)
|
|
44
|
+
- MarkBitArray: explained in [Mark Phase](#mark-phase)
|
|
45
|
+
- Guard page: a page of memory that is protected from reads and writes to ensure
|
|
46
|
+
bugs in the VM or GC don't accidentally overwrite metadata.
|
|
47
|
+
|
|
48
|
+
The rest of the segment is free space available for JS values to be allocated
|
|
49
|
+
into. See
|
|
50
|
+
[`AlignedHeapSegment::Contents`](../include/hermes/VM/AlignedHeapSegment.h)
|
|
51
|
+
for details about the layout of a heap segment.
|
|
52
|
+
|
|
53
|
+
# Object Types
|
|
54
|
+
|
|
55
|
+
In order to determine reachability, we need to be able to discover
|
|
56
|
+
what objects point to. To do this for the variety of types in Hermes, we
|
|
57
|
+
define a system of metadata for each object, associated to its type which is
|
|
58
|
+
encoded as a `VTable`. Metadata is built once for the first Runtime created, and
|
|
59
|
+
is then re-used for every subsequent runtime. The metadata describes at what
|
|
60
|
+
offsets from the head of an object there are pointers. It also describes for
|
|
61
|
+
any array type where the length of the array can be found, so a dynamic number
|
|
62
|
+
of pointers can be found. This design allows pointers to be marked without
|
|
63
|
+
performing any type checks, branches, or virtual dispatch.
|
|
64
|
+
|
|
65
|
+
The `VTable *` is embedded in the header of everything allocated on the heap.
|
|
66
|
+
This is represented by the C++ superclass of all heap objects, called `GCCell`.
|
|
67
|
+
This way, given a pointer to something in the heap, its type (and pointer
|
|
68
|
+
metadata) can be found. The type is stored as a `CellKind` enum. The `VTable`
|
|
69
|
+
also includes function pointers for dynamic dispatch. This is used to implement
|
|
70
|
+
type-based dispatch for things like property accesses and function calls.
|
|
71
|
+
|
|
72
|
+
# Generations
|
|
73
|
+
|
|
74
|
+
GenGC is so named because it is **generational**, meaning it manages groups of
|
|
75
|
+
allocated objects based on their age. Age is determined by how many collections
|
|
76
|
+
an object has survived. GenGC has two generations: the **Young Generation** (YG)
|
|
77
|
+
and **Old Generation** (OG).
|
|
78
|
+
Allocations go initially into YG, and if they survive the first YG collection
|
|
79
|
+
cycle, they are promoted to OG. The OG is collected less frequently and over a
|
|
80
|
+
larger number of objects. YG is collected frequently because it is assumed that
|
|
81
|
+
there are a lot of objects that were allocated but became garbage quickly. This
|
|
82
|
+
is known as the
|
|
83
|
+
[Generational Hypothesis](https://www.memorymanagement.org/glossary/g.html#generational.hypothesis):
|
|
84
|
+
young objects are more likely to die than old objects.
|
|
85
|
+
|
|
86
|
+
The young generation of GenGC is a single segment, regardless of the size
|
|
87
|
+
of the heap as a whole. Since YG is much smaller than OG, this is also a much
|
|
88
|
+
shorter pause to collect YG. Since the YG is also a single segment, it's very
|
|
89
|
+
fast to check if a pointer is a YG pointer: get the segment start address and
|
|
90
|
+
compare to the YG start address. The allocation algorithm is as follows:
|
|
91
|
+
|
|
92
|
+
- Attempt to allocate into the YG segment by bumping a pointer called the
|
|
93
|
+
"level" of the segment up. If the level would go past the end of the segment, it
|
|
94
|
+
fails
|
|
95
|
+
- If the bump allocation succeeds, the allocation is complete and control
|
|
96
|
+
returns to the VM
|
|
97
|
+
- If the bump allocation fails, start a YG collection cycle. This will evacuate
|
|
98
|
+
the YG into the OG, and leave some empty space.
|
|
99
|
+
- Try bump allocation again after the YG collection cycle completes. If it
|
|
100
|
+
succeeds, return.
|
|
101
|
+
- Else, try to allocate directly into the OG.
|
|
102
|
+
- If the OG is full, start a full collection cycle (collect garbage in both the
|
|
103
|
+
YG and OG). Once that completes, try allocating directly into OG again.
|
|
104
|
+
- If all that fails, that means there's not enough space for the allocation,
|
|
105
|
+
raise an Out of Memory (OOM) error
|
|
106
|
+
|
|
107
|
+
The old generation of GenGC is a list of segments, each of which is created
|
|
108
|
+
after an allocation failed in the previous segment. The OG maintains an
|
|
109
|
+
allocation context which remembers the most recent active segment, and this is
|
|
110
|
+
where promoted YG objects are allocated into. Once the OG has reached its
|
|
111
|
+
configured size limit, it will do a full collection, trying to free garbage
|
|
112
|
+
inside the OG.
|
|
113
|
+
|
|
114
|
+
There are more details on collection cycles in the
|
|
115
|
+
[Collection Cycles](#collection-cycles) section.
|
|
116
|
+
|
|
117
|
+
## Allocate directly into the Old Geneneration (Pre-Tenuring)
|
|
118
|
+
|
|
119
|
+
Sometimes there are applications where the generational hypothesis doesn't hold
|
|
120
|
+
true, or in other words they allocate more objects that are long-lived than
|
|
121
|
+
short-lived. This typically occurs during the initialization period of an
|
|
122
|
+
application. If this rule applies to your application, you can request this
|
|
123
|
+
behavior from Hermes by changing the `GCConfig` that's used at Runtime
|
|
124
|
+
construction time:
|
|
125
|
+
|
|
126
|
+
```cpp
|
|
127
|
+
hermes::vm::GCConfig::Builder gcConfigBuilder{};
|
|
128
|
+
gcConfigBuilder
|
|
129
|
+
.withAllocInYoung(false)
|
|
130
|
+
.withRevertToYGAtTTI(true);
|
|
131
|
+
std::unique_ptr<jsi::Runtime> runtime = makeHermesRuntime(
|
|
132
|
+
hermes::vm::RuntimeConfig::Builder()
|
|
133
|
+
.withGCConfig(gcConfigBuilder.build())
|
|
134
|
+
);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The `AllocInYoung` parameter defaults to `true`, and controls whether
|
|
138
|
+
allocations go into YG by default or OG.
|
|
139
|
+
The `RevertToYGAtTTI` parameter defaults to `false`, and is unused unless
|
|
140
|
+
`AllocInYoung` is true.
|
|
141
|
+
|
|
142
|
+
See the full documentation of [GCConfig](../public/hermes/Public/GCConfig.h) for
|
|
143
|
+
more details and other configuration options.
|
|
144
|
+
|
|
145
|
+
# Collection Cycles
|
|
146
|
+
|
|
147
|
+
GenGC determines garbage that can be collected in a program based on what is
|
|
148
|
+
reachable in the graph of objects. The graph has **roots** that define the
|
|
149
|
+
entrypoint to the graph. These are typically things like variables on the
|
|
150
|
+
JS call stack. From the roots, the entire graph of objects is traversed by
|
|
151
|
+
following each property to another object. Think of it as similar to a
|
|
152
|
+
traditional graph search algorithm, although we make many optimizations specific
|
|
153
|
+
to the heap structure.
|
|
154
|
+
|
|
155
|
+
GenGC has two different types of garbage collection cycles
|
|
156
|
+
(abbreviated as "GC"). There is a Young Generation Collection or YG GC, and a
|
|
157
|
+
Full Collection or full GC.
|
|
158
|
+
A YG GC collects only objects present in the YG, and promotes objects that are
|
|
159
|
+
reachable to the OG so they are collected less frequently.
|
|
160
|
+
A full GC collects both the OG and YG simultaneously, and determines the
|
|
161
|
+
reachability of every object in the heap.
|
|
162
|
+
|
|
163
|
+
There are two common concepts across both types of collections:
|
|
164
|
+
|
|
165
|
+
- Mark: Marking means setting some state related to an object to say that it was
|
|
166
|
+
found to be reachable during the traversal of the heap. In GenGC's case, this
|
|
167
|
+
is often referred to as a "mark bit"
|
|
168
|
+
- Sweep: Sweeping means reclaiming unused space so that it can be re-used by the
|
|
169
|
+
allocator. This can look very different depending on the generation
|
|
170
|
+
|
|
171
|
+
## Young Generation Collection
|
|
172
|
+
|
|
173
|
+
When a YG collection is triggered, it first needs to mark the roots of the
|
|
174
|
+
object graph. Because YG collections occur very frequently, we want the set of
|
|
175
|
+
roots to be small. To achieve this we ignore any roots that are known to be
|
|
176
|
+
only in the OG. When each pointer is discovered for the first time, an
|
|
177
|
+
allocation is made in the OG for the same size, the YG object's contents are
|
|
178
|
+
copied, and a forwarding pointer is left in the header of the YG object pointing
|
|
179
|
+
to the new OG object. After that, if we see the same pointer again, we replace
|
|
180
|
+
it with the value of the forwarding pointer.
|
|
181
|
+
|
|
182
|
+
We also need to consider any OG objects that point into YG as part of the root
|
|
183
|
+
set. That is handled by a separate mechanism during JS execution, detailed below
|
|
184
|
+
in the section on [Write Barriers](#write-barriers).
|
|
185
|
+
|
|
186
|
+
To find all the reachable objects, we take advantage of the linear nature of
|
|
187
|
+
OG, and scan linearly from the original end of the allocated region, until
|
|
188
|
+
there aren't any more promoted objects. Each promoted object is scanned for its
|
|
189
|
+
own pointers. This is how all YG objects are discovered. It's similar to a
|
|
190
|
+
semi-space algorithm where the second space is the OG.
|
|
191
|
+
|
|
192
|
+
Once the objects are all moved, any weak references pointing to YG objects are
|
|
193
|
+
updated if the object is still alive, and then any finalizers are run by
|
|
194
|
+
iterating over a list of finalizers.
|
|
195
|
+
|
|
196
|
+
### Write Barriers
|
|
197
|
+
|
|
198
|
+
As part of the root set, we also consider any OG objects that point into YG.
|
|
199
|
+
To determine where these exist without iterating over the whole OG, we use a
|
|
200
|
+
**write barrier** whenever a pointer value is written to during JS execution.
|
|
201
|
+
If it would create an old -> young pointer, we want to mark the source object
|
|
202
|
+
as part of the root set of YG.
|
|
203
|
+
|
|
204
|
+
The way we do this is by classifying a small region of memory in the OG as a
|
|
205
|
+
**card**, currently 512 bytes. We determine the card for a pointer using the
|
|
206
|
+
same alignment trick to get the base address of the segment, then use division
|
|
207
|
+
to figure out the card index. The **card table** is a list of bytes stored in
|
|
208
|
+
the header of the segment, and when we find an object pointing into the YG, we
|
|
209
|
+
"dirty" its card by setting the byte value to 1. We don't use a bitmap because
|
|
210
|
+
the card table is only 8 KiB, and shrinking it further isn't worth it.
|
|
211
|
+
|
|
212
|
+
During YG collection time, we can quickly scan each card table for the set of
|
|
213
|
+
dirtied cards, then we mark all of the objects on that card. We use a separate
|
|
214
|
+
**card object table** to find the first object for a card. That table contains
|
|
215
|
+
another byte per card, except it is signed. If the sign is positive, it means
|
|
216
|
+
"go back that many bytes to find the first object on this card". If the sign
|
|
217
|
+
is negative, it means "go back that many cards and check the table at that
|
|
218
|
+
index". This encoding scheme allows a low memory cost way to jump across very
|
|
219
|
+
large objects.
|
|
220
|
+
|
|
221
|
+
Note that the way cards are dirtied means there's a chance of promoting some
|
|
222
|
+
YG objects that aren't actually live. This tradeoff is chosen so the cards take
|
|
223
|
+
up less memory and can be scanned quickly. Smaller cards are more precise, but
|
|
224
|
+
take up more memory and require more scanning.
|
|
225
|
+
|
|
226
|
+
## Full Collection
|
|
227
|
+
|
|
228
|
+
A full collection is much larger than a YG collection, so it happens less
|
|
229
|
+
frequently. It needs to determine the reachability of every object in the heap.
|
|
230
|
+
A full collection is triggered when the OG doesn't have enough space to satisfy
|
|
231
|
+
an allocation from a YG promotion or a direct-to-OG allocation.
|
|
232
|
+
|
|
233
|
+
### Mark Phase
|
|
234
|
+
|
|
235
|
+
The collection begins with a call to `markRoots`, although this time
|
|
236
|
+
additionally marking roots known to only point into the OG, such as the
|
|
237
|
+
`IdentifierTable`. The first time an object is encountered, it sets a bit in the
|
|
238
|
+
mark bit array, so future accesses know that it has already been found. Then we
|
|
239
|
+
push the newly discovered object on top of a **mark stack**.
|
|
240
|
+
|
|
241
|
+
The mark stack is drained by popping off the top object, scanning all of its
|
|
242
|
+
pointers, and pushing objects it discovers onto the mark stack.
|
|
243
|
+
|
|
244
|
+
There are two changes implemented to prevent the mark stack from growing without
|
|
245
|
+
bound:
|
|
246
|
+
- If a pointer to an object is discovered that is after the current address, it
|
|
247
|
+
isn't pushed on the mark stack. Instead, we jump to the next marked bit when
|
|
248
|
+
the mark stack is empty. If the pointer is before the current address, it has to
|
|
249
|
+
be pushed on the stack because we won't go backwards in the heap normally
|
|
250
|
+
- If the mark stack overflows a limit, we drop the whole stack and re-scan from
|
|
251
|
+
the beginning of the heap. Since many objects have already been marked, this
|
|
252
|
+
will go much faster than previous times. It's guaranteed to terminate because
|
|
253
|
+
in the worst case eventually every object will be marked and there's nothing
|
|
254
|
+
to collect.
|
|
255
|
+
|
|
256
|
+
Once the marking is complete this way, full reachability is known, and sweeping
|
|
257
|
+
can begin.
|
|
258
|
+
|
|
259
|
+
### Weak Map resolution
|
|
260
|
+
|
|
261
|
+
However, one problem to be handled are **weak references**, which are pointers
|
|
262
|
+
that do not keep what they point to alive. Now that we know reachability, we
|
|
263
|
+
know which weak pointers to make null.
|
|
264
|
+
|
|
265
|
+
But there's a special case of the JS type `WeakMap` that has to be handled:
|
|
266
|
+
the keys are weak, and the values are strong, unless they keep the key itself
|
|
267
|
+
alive. This means the values can't be marked until the key is determined
|
|
268
|
+
reachable through some other means.
|
|
269
|
+
|
|
270
|
+
We delay marking any WeakMap key/value pairs until the end of marking. Then for
|
|
271
|
+
each WeakMap, and for each key that is reachable in the weak map, we then mark
|
|
272
|
+
the value. If marking the value ends up making another WeakMap key reachable,
|
|
273
|
+
we then need to mark that. We repeat this for every key that becomes reachable
|
|
274
|
+
until there's nothing more to mark. At that point we can finally null out keys
|
|
275
|
+
that were never found to be reachable.
|
|
276
|
+
|
|
277
|
+
### Sweep & Compact Phases
|
|
278
|
+
|
|
279
|
+
Now that full reachability has been computed, we know which objects are garbage.
|
|
280
|
+
We want to reclaim that memory so that it can be allocated again. To do so, we
|
|
281
|
+
will **compact** all live memory down so that there's no space between them,
|
|
282
|
+
overwriting the space that was used by dead objects.
|
|
283
|
+
|
|
284
|
+
To do this, we iterate through every live object in the heap using the mark
|
|
285
|
+
bits, and for each one we compute its new address. We place that new address
|
|
286
|
+
as a forwarding pointer in the header of the object (displacing the `VTable *`
|
|
287
|
+
to a temporary vector to avoid losing the type information). Once a forwarding
|
|
288
|
+
pointer has been determined for everything in the heap, we do a second pass to
|
|
289
|
+
fixup pointers within objects to use the forwarded value. Finally, we copy the
|
|
290
|
+
memory from the previous location to the new location, overwriting whatever used
|
|
291
|
+
to be there.
|
|
292
|
+
|
|
293
|
+
The end result of this is that all live memory sits right next to each other in
|
|
294
|
+
the heap segments, and all dead memory has now been reclaimed for the allocator.
|
|
295
|
+
We use `madvise` on POSIX systems to tell the operating system that the leftover
|
|
296
|
+
memory is now not needed by the program, and it's free to be paged out and zero
|
|
297
|
+
filled if the OS needs to.
|
|
298
|
+
|
|
299
|
+
At this point control returns to the JS engine, typically to finish the
|
|
300
|
+
allocation that started the collection.
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: hades
|
|
3
|
+
title: The Hades Garbage Collector
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Hades is a garbage collector for Hermes that aims to improve pause times by an
|
|
7
|
+
order of magnitude over GenGC. The main principle Hades uses to achieve those
|
|
8
|
+
low pause times is that most of the garbage collection work happens in a
|
|
9
|
+
background thread concurrently with the interpreter running JavaScript code.
|
|
10
|
+
This is distinct from GenGC, which only runs on a single thread which is shared
|
|
11
|
+
with the interpreter.
|
|
12
|
+
|
|
13
|
+
# Enabling Hades
|
|
14
|
+
|
|
15
|
+
In local builds on the command line using CMake (which forwards to CMake), Hades
|
|
16
|
+
is the default GC used, and currently the only GC supported for production use.
|
|
17
|
+
The GC being used is controlled by the CMake variable `-DHERMESVM_GCKIND=value`.
|
|
18
|
+
|
|
19
|
+
To use a pre-built package of Hermes with Hades enabled, check the
|
|
20
|
+
[Releases page on Github](https://github.com/facebook/hermes/releases).
|
|
21
|
+
As of right now, there aren't any available, but we'll be making one available
|
|
22
|
+
with v0.8 and later.
|
|
23
|
+
|
|
24
|
+
## Check Which GC is Used
|
|
25
|
+
|
|
26
|
+
If you want to know what GC is being used in your application, you can find out
|
|
27
|
+
with some JS:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const gcName = HermesInternal.getRuntimeProperties().GC;
|
|
31
|
+
// If you're running Hermes on the command line, use print.
|
|
32
|
+
print(gcName);
|
|
33
|
+
// If you're running Hermes in some kind of framework like React Native,
|
|
34
|
+
// console.log should exist.
|
|
35
|
+
console.log(gcName);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
This will print one of:
|
|
39
|
+
|
|
40
|
+
- `"hades (concurrent)"`: You're using Hades in concurrent mode
|
|
41
|
+
- `"hades (incremental)"`: You're using Hades in [incremental mode](#incremental-mode)
|
|
42
|
+
|
|
43
|
+
# Basics
|
|
44
|
+
|
|
45
|
+
Most of the basic heap structure of Hades is similar to GenGC, so it is
|
|
46
|
+
recommended to read the [GenGC Documentation](./GenGC.md) first, in particular
|
|
47
|
+
the following sections:
|
|
48
|
+
|
|
49
|
+
- [Heap Segments](./GenGC.md#heap-segments)
|
|
50
|
+
- [Object Types](./GenGC.md#object-types)
|
|
51
|
+
- [Generations](./GenGC.md#generations)
|
|
52
|
+
- [Write Barriers](./GenGC.md#write-barriers)
|
|
53
|
+
|
|
54
|
+
# Generations
|
|
55
|
+
|
|
56
|
+
Similarly to GenGC, Hades also has two generations: the **Young Generation**
|
|
57
|
+
(YG) and **Old Generation** (OG). Allocations go initially into YG, and if they
|
|
58
|
+
survive the first collection they go into OG. YG works exactly the same as
|
|
59
|
+
GenGC, but OG has a different allocation strategy that allows for gaps.
|
|
60
|
+
|
|
61
|
+
## Freelist Allocator
|
|
62
|
+
|
|
63
|
+
Hades's OG is a list of heap segments, and each heap segment maintains a
|
|
64
|
+
**Free List** of empty space. Each **Free List Cell** points to the next cell,
|
|
65
|
+
called an explicit free list. This is opposed to an implicit free list, where
|
|
66
|
+
the length is used to traverse both free and used cells. A free list is used
|
|
67
|
+
because it allows empty space to be left where it is, without requiring
|
|
68
|
+
compaction. This is a requirement for concurrent allocations and sweeping.
|
|
69
|
+
|
|
70
|
+
Furthermore, the free list is **size-segregated**, meaning each size class gets
|
|
71
|
+
a separate free list. In other words, cells of size `N` only point to other
|
|
72
|
+
cells of size `N`. This allows an allocation of size `N` to be satisfied
|
|
73
|
+
instantly with the head of the free list. Each free list head is stored at an
|
|
74
|
+
index in a fixed-size array for small cell sizes. These are known as
|
|
75
|
+
**buckets**.
|
|
76
|
+
|
|
77
|
+
Hades does not do any rounding up of sizes beyond the required heap alignment
|
|
78
|
+
of 8 bytes. This means there is one bucket for each multiple of 8, up to 2048
|
|
79
|
+
bytes. From 2048 bytes to the maximum heap segment size (4 MiB) the buckets go
|
|
80
|
+
by powers of 2. For large buckets, we store cells that are greater than or equal
|
|
81
|
+
to the size bucket, but less than the next power of 2. Cells that need less than
|
|
82
|
+
the size of the free list cell **carve** out a small piece of the cell, and put
|
|
83
|
+
the remaining piece on a the free list corresponding to its new size.
|
|
84
|
+
|
|
85
|
+
Due to having these free lists be per-segment, we need a quick way to find which
|
|
86
|
+
segment has free space for a given size. We do this with a series of bit arrays,
|
|
87
|
+
where bits are flipped to 0 as a free list is exhausted in a segment, and
|
|
88
|
+
flipped back to 1 when sweeping frees some cells. We have per-segment free lists
|
|
89
|
+
so that sweeping and compaction (which also operate on a per-segment basis) can
|
|
90
|
+
destroy them efficiently and create a new list. It can do this to easily
|
|
91
|
+
coalesce adjacent free regions.
|
|
92
|
+
|
|
93
|
+
# Collection Cycles
|
|
94
|
+
|
|
95
|
+
Hades has two different types of collections: a YG collection (YG GC) and an
|
|
96
|
+
OG collection (OG GC). The former is almost exactly the same as GenGC's, so we
|
|
97
|
+
won't repeat it here. The OG GC is very different though, because it runs
|
|
98
|
+
concurrently in a background thread.
|
|
99
|
+
|
|
100
|
+
For the purposes of distinguishing these two threads, we'll name them as
|
|
101
|
+
follows:
|
|
102
|
+
|
|
103
|
+
- **Mutator Thread**: The thread running the JS interpreter
|
|
104
|
+
- **GC Thread**: The thread running any GC operations such as marking or
|
|
105
|
+
sweeping
|
|
106
|
+
|
|
107
|
+
Note that currently there is only ever a single GC thread at any point in time.
|
|
108
|
+
We also cache the thread and reuse it instead of making a new one for each
|
|
109
|
+
collection.
|
|
110
|
+
|
|
111
|
+
There are three different locks used throughout the GC:
|
|
112
|
+
|
|
113
|
+
- The **GC Mutex** is used to protect structures like mark bits, card tables,
|
|
114
|
+
and the free lists
|
|
115
|
+
- The **WeakRef Mutex** is used to protect structures used during weak ref
|
|
116
|
+
marking
|
|
117
|
+
- The **Write Barrier Mutex** is used to protect a small buffer used by write
|
|
118
|
+
barriers and concurrent marking
|
|
119
|
+
|
|
120
|
+
The GC mutex is used to protect most things, as they tend to all be accessed at
|
|
121
|
+
the same time. There was no need for finer grained locks yet, with the exception
|
|
122
|
+
of the write barrier mutex because write barriers are executed all the time.
|
|
123
|
+
|
|
124
|
+
An OG GC is started once the OG is about 75% full. We start it a bit early so
|
|
125
|
+
that it can complete sweeping before reaching 100% full and avoid blocking any
|
|
126
|
+
allocations.
|
|
127
|
+
|
|
128
|
+
## Mark Phase
|
|
129
|
+
|
|
130
|
+
The first step of an OG GC is to mark all of the roots of the object graph.
|
|
131
|
+
We only ever start an OG GC when YG is empty, so there's no need to mark any of
|
|
132
|
+
YG.
|
|
133
|
+
|
|
134
|
+
Marking an object consists of the following steps:
|
|
135
|
+
|
|
136
|
+
- Using mark bits, check if an object has been visited already
|
|
137
|
+
- If it has been visited already, there's nothing to do
|
|
138
|
+
- If not, push it onto a **mark stack** that will be drained later
|
|
139
|
+
- Set its mark bit
|
|
140
|
+
- If the object pointed to is a WeakMap, put it onto a separate stack
|
|
141
|
+
(see [Weak Map Resolution](./GenGC.md#weak-map-resolution))
|
|
142
|
+
|
|
143
|
+
Draining the mark stack works as follows:
|
|
144
|
+
|
|
145
|
+
- Acquire a lock on the GC mutex
|
|
146
|
+
- Check if the write barrier buffer has objects that need to be marked, if so,
|
|
147
|
+
add them to the mark stack
|
|
148
|
+
- While we've marked fewer than a certain number of bytes, defaults to 8 KiB
|
|
149
|
+
- Pull one object off the mark stack
|
|
150
|
+
- Get its type metadata (see [Object Types](./GenGC.md#object-types))
|
|
151
|
+
- Use the metadata to find pointers to other objects
|
|
152
|
+
- Those other objects will be pushed on the stack
|
|
153
|
+
- Release the lock on the GC mutex
|
|
154
|
+
|
|
155
|
+
This can run almost entirely uninterrupted on the background thread since very
|
|
156
|
+
few things need to acquire the GC mutex. The most common way to interrupt
|
|
157
|
+
marking is when YG fills up, as it requires the GC mutex in order to evacuate
|
|
158
|
+
YG.
|
|
159
|
+
|
|
160
|
+
### Write Barriers
|
|
161
|
+
|
|
162
|
+
There's an important race condition to consider when thinking about concurrent
|
|
163
|
+
marking: what happens if a pointer is modified while we're reading it?
|
|
164
|
+
|
|
165
|
+
There are two different races that are possible here:
|
|
166
|
+
|
|
167
|
+
- A non-atomic read of the pointer might race with a non-atomic write, and the
|
|
168
|
+
reads or writes might "tear" (meaning you see only part of the write)
|
|
169
|
+
- You might miss marking the old value or the new value
|
|
170
|
+
|
|
171
|
+
The first is handled on 64-bit platforms because all of the reads are of a 64
|
|
172
|
+
bit value, which can be atomically handled cheaply on a 64-bit CPU. See the
|
|
173
|
+
[Incremental Mode](#incremental-mode) section for what we do on a 32-bit CPU.
|
|
174
|
+
|
|
175
|
+
The second problem is harder to solve. If we see the old value, and the new
|
|
176
|
+
object isn't marked anywhere else, we would accidentally think it's garbage and
|
|
177
|
+
collect it! Alternatively, if we see the new value, the old value won't be
|
|
178
|
+
marked. This would be a problem if the old pointer was moved from one object to
|
|
179
|
+
another, but we had already marked the second object.
|
|
180
|
+
|
|
181
|
+
In order to fix this, we need to know when a pointer is modified during
|
|
182
|
+
concurrent marking. Hades implements this through an additional write barrier.
|
|
183
|
+
This write barrier is based on a principle called "Snapshot at the Beginning"
|
|
184
|
+
(SATB). The principle is that we want to collect the OG based on a snapshot
|
|
185
|
+
of the heap when the collection began. Which means if a pointer is changed, we
|
|
186
|
+
want to make sure we mark the old value instead of the new value.
|
|
187
|
+
|
|
188
|
+
This might feel counter-intuitive compared to the more common alternative
|
|
189
|
+
approach known as "Incremental Update" (IU), where the new value is marked. The
|
|
190
|
+
reason Hades uses SATB instead is that it has a nice guarantee: you'll never
|
|
191
|
+
need to revisit any object you have already marked. This means there is a finite
|
|
192
|
+
upper bound on the amount of work marking has to do. IU write barrier based
|
|
193
|
+
systems often have a race near the end, where the GC thread needs to pause the
|
|
194
|
+
mutator thread to try and complete marking as fast as possible. If it exceeds
|
|
195
|
+
a time quota, it resumes the mutator and tries again later. We avoid this
|
|
196
|
+
complexity with our SATB barrier.
|
|
197
|
+
|
|
198
|
+
A second benefit of SATB is that we can treat any allocations made into OG
|
|
199
|
+
between the start and end of the collection as alive by default, without
|
|
200
|
+
needing to mark them.
|
|
201
|
+
|
|
202
|
+
And the final benefit of SATB is that there is never a need to mark the roots
|
|
203
|
+
again to finish a collection, as their old values were handled at the start
|
|
204
|
+
of the collection while the mutator was paused.
|
|
205
|
+
|
|
206
|
+
The barrier works by pushing the old value onto a small fixed size buffer, which
|
|
207
|
+
has space for 128 elements. Once it fills up, the Write Barrier Lock is taken to
|
|
208
|
+
"flush" the buffer into a separate mark stack used by the concurrent marker.
|
|
209
|
+
This means a lock is only taken every 128 write barriers. It uses a separate
|
|
210
|
+
mutex from the GC mutex to ensure a write barrier is not blocked for very long
|
|
211
|
+
if the GC thread happens to be reading from the separate mark stack.
|
|
212
|
+
|
|
213
|
+
## Complete Marking
|
|
214
|
+
|
|
215
|
+
Once the mark stack is empty, there are a few details that need to be handled in
|
|
216
|
+
order to complete marking and move on to sweeping:
|
|
217
|
+
|
|
218
|
+
- Flush any remaining write barrier pointers left
|
|
219
|
+
- Handle WeakMap resolution
|
|
220
|
+
- Fix weak references (WeakRefs)
|
|
221
|
+
|
|
222
|
+
Handling these things can be very tricky concurrently, so in order to prevent
|
|
223
|
+
bugs and infinite loops, we pause the mutator during this time. Even though SATB
|
|
224
|
+
write barriers don't require the mutator to pause, these other operations do
|
|
225
|
+
require a pause, so unfortunately this is still required.
|
|
226
|
+
|
|
227
|
+
Flushing the remaining write barrier pointers just means copying the pointers
|
|
228
|
+
into the mark stack and draining it one more time. This could potentially take
|
|
229
|
+
a very long time, but in practice that is exceedingly rare.
|
|
230
|
+
|
|
231
|
+
WeakMap resolution is handled in the same way that GenGC handles it. We do this
|
|
232
|
+
during a mutator pause mostly because we didn't want to rewrite the algorithm
|
|
233
|
+
to work in a concurrent context, as it's already very complicated on its own.
|
|
234
|
+
|
|
235
|
+
Weak References also need to be cleared if they point to something that is
|
|
236
|
+
now garbage, and this is much easier to do with the mutator paused. Otherwise
|
|
237
|
+
the mutator would need a lock to read a weak reference value. This could be
|
|
238
|
+
improved to simply be an atomic operation in the future, but for now this can't
|
|
239
|
+
be atomic.
|
|
240
|
+
|
|
241
|
+
Once that's all taken care of, we can move on to sweeping.
|
|
242
|
+
|
|
243
|
+
### WeakRef Read Barriers
|
|
244
|
+
|
|
245
|
+
There's a caveat to mention about WeakRefs and the SATB barrier. If you read a
|
|
246
|
+
pointer out of a weak reference and store it in an object on the heap, SATB
|
|
247
|
+
won't record the change, and the object might not be found reachable. Something
|
|
248
|
+
similar can happen if a weak ref is read and placed into a root.
|
|
249
|
+
|
|
250
|
+
To fix this, we have WeakRef reads perform a barrier on the pointer being read.
|
|
251
|
+
This conservatively assumes the pointer being read is alive. WeakRefs are not
|
|
252
|
+
read from that often, so this was deemed an acceptable cost.
|
|
253
|
+
|
|
254
|
+
Note that there also weak roots, such as the HiddenClass cache stored in each
|
|
255
|
+
CodeBlock. These do not perform a read barrier, specifically because they are
|
|
256
|
+
only ever used for comparisons. They never produce a pointer that was otherwise
|
|
257
|
+
dead. A possible simplification of this in the future could use a HiddenClass ID
|
|
258
|
+
instead of a pointer, as it achieves the same effect without requiring a special
|
|
259
|
+
case.
|
|
260
|
+
|
|
261
|
+
## Sweep Phase
|
|
262
|
+
|
|
263
|
+
Once complete reachability information is known, the OG GC turns off the
|
|
264
|
+
SATB barriers. The sweeper iterates over one segment at a time, allowing the
|
|
265
|
+
mutator to interleave. This is specifically allowed because the sweeper only
|
|
266
|
+
ever modifies garbage objects that aren't used, therefore there's no races.
|
|
267
|
+
It holds the GC mutex to prevent YG from allocating into the OG while it's
|
|
268
|
+
being swept.
|
|
269
|
+
|
|
270
|
+
The process works as follows:
|
|
271
|
+
|
|
272
|
+
- Acquire a lock on the GC mutex
|
|
273
|
+
- Clear one heap segment's free list
|
|
274
|
+
- Iterate linearly over cells, using the embedded length to skip over live cells
|
|
275
|
+
- Check if a cell is new garbage using its type tag
|
|
276
|
+
- If it is not new garbage, continue to the next cell
|
|
277
|
+
- If it is new garbage, turn it into a free list cell.
|
|
278
|
+
- Contiguous unused regions are added as a single region onto the new free list
|
|
279
|
+
- Once all cells in the segment have been processed, release the lock
|
|
280
|
+
|
|
281
|
+
Once that process is completed for every heap segment, sweeping completes and
|
|
282
|
+
the OG collection is over.
|
|
283
|
+
|
|
284
|
+
## Compact Phase
|
|
285
|
+
|
|
286
|
+
Compacting live memory to be closer together is still a beneficial concept in
|
|
287
|
+
Hades, as it allows us to return unused memory to the OS, and reduces the
|
|
288
|
+
fragmentation of the free lists for mostly empty heap segments. Implementing it
|
|
289
|
+
is more tricky than GenGC though, as we can't modify pointers concurrently with
|
|
290
|
+
the mutator thread.
|
|
291
|
+
|
|
292
|
+
Due to these restrictions, we can currently only compact a single segment
|
|
293
|
+
(called the compactee) for each full collection. Compaction runs as part of the
|
|
294
|
+
collection cycle and flows as follows:
|
|
295
|
+
|
|
296
|
+
1. At the start of an OG collection, determine whether the heap is currently
|
|
297
|
+
larger than its target size. If so, select and record a segment to compact.
|
|
298
|
+
2. Write barriers start dirtying cards for pointers pointing into the
|
|
299
|
+
compaction candidate. This will continue until the compaction is fully complete.
|
|
300
|
+
3. Marking begins. During marking, we dirty cards in the card table
|
|
301
|
+
corresponding to any on-heap pointers that point into the compaction candidate.
|
|
302
|
+
Any YG collection that occurs during marking needs special care. Promoted
|
|
303
|
+
objects will not be scanned by the OG since they are allocated as marked, so
|
|
304
|
+
they need to be scanned for compactee pointers after they have been promoted.
|
|
305
|
+
Furthermore, the card table cannot be cleared at the end of the YG collection,
|
|
306
|
+
since that would erase information from the ongoing compaction.
|
|
307
|
+
4. During the STW pause, the internal state of the GC is updated to signal that
|
|
308
|
+
all pointers into the compactee have been marked, and that the next YG
|
|
309
|
+
collection should complete the compaction.
|
|
310
|
+
5. Sweeping. The segment identified for compaction will not be swept, however
|
|
311
|
+
compaction may take place during sweeping if the next YG collection starts
|
|
312
|
+
before sweeping is complete. Note that write barriers will continue to be active
|
|
313
|
+
until compaction is complete, since new pointers from the OG into the compactee
|
|
314
|
+
may be added.
|
|
315
|
+
6. Compaction. The next young gen collection evacuates both the YG and the
|
|
316
|
+
compactee. It will mark long lived roots and update pointers based on the
|
|
317
|
+
previously dirtied cards. Combining compaction with YG collections lets us
|
|
318
|
+
share the overhead of updating roots, and lets us avoid tracking pointers from
|
|
319
|
+
the YG into the compactee.
|
|
320
|
+
7. The now empty segment is released by the GC and returned to the OS.
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
## Incremental Mode
|
|
324
|
+
|
|
325
|
+
Hades's concurrent marking relies on being able to read a 64-bit value
|
|
326
|
+
atomically at the same time it might be modified by the mutator. If the
|
|
327
|
+
underlying hardware supports this natively, then we use it.
|
|
328
|
+
However, some hardware does not support doing those atomic reads in a lock-free
|
|
329
|
+
manner, primarily 32-bit ARM CPUs. Since Hermes's main target is mobile devices,
|
|
330
|
+
it's important for us to still support them, and have some of the fast pause
|
|
331
|
+
time guarantees that Hades gives.
|
|
332
|
+
|
|
333
|
+
In order to do this, on 32-bit platforms we don't use any other threads, and
|
|
334
|
+
instead run Hades in "incremental mode". This means instead of marking objects
|
|
335
|
+
concurrently on the GC thread, we use a portion of each YG GC to do some OG GC
|
|
336
|
+
work. This means the OG GC is completed incrementally on each individual YG GC.
|
|
337
|
+
Each YG GC takes a little bit longer while an OG GC is active, but the penalty
|
|
338
|
+
is small enough to still have better guarantees than running a fully blocking OG
|
|
339
|
+
GC.
|
|
340
|
+
|
|
341
|
+
The concurrent mode of Hades has faster pauses and is preferred to be used if
|
|
342
|
+
possible, but incremental mode has to be used on most 32-bit CPUs. You can also
|
|
343
|
+
use incremental mode if threads aren't supported on your platform, or if you
|
|
344
|
+
prefer to not use threads for some other reason.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: high-level-optimizations
|
|
3
|
+
title: High Level Optimizations
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
JS authors frequently use high level library functions which they expect to perform efficiently.
|
|
7
|
+
They call these in conjunction with other library calls to write code which has similarities to
|
|
8
|
+
functional programming languages.
|
|
9
|
+
|
|
10
|
+
Executing this kind of code in Hermes can be slow due to the compiler not having proper visibility
|
|
11
|
+
into the JS library. However, we can get some information about what is being called in certain
|
|
12
|
+
scenarios, such as with `-fstatic-builtins` or in actual typed SH code.
|
|
13
|
+
|
|
14
|
+
This document is a place to put some ideas on how to make specific scenarios more efficient
|
|
15
|
+
in the future. It is possible we'll just happen to optimize some cases here as a result of good
|
|
16
|
+
type information in SH and a solid optimization pipeline.
|
|
17
|
+
|
|
18
|
+
# Spreading and modifying existing structures
|
|
19
|
+
|
|
20
|
+
An example of some sample code found in input to Hermes to clone a Map and add an element:
|
|
21
|
+
```
|
|
22
|
+
return new Map([...Array.from(origMap), [id, obj]]);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
1. Iterates `origMap` and creates a new array `%tmp1`, element by element.
|
|
26
|
+
2. Creates a new array `%tmp2`, iterates `%tmp1`, and copies the elements.
|
|
27
|
+
3. Adds the new array `[id, obj]` to `%tmp2`.
|
|
28
|
+
4. Iterates `%tmp2` and populates a newly created `Map`.
|
|
29
|
+
|
|
30
|
+
Some potential ways to simplify things:
|
|
31
|
+
|
|
32
|
+
1. `...Array.from(a)` is the same as `...a` provided that the array iterator has not been modified.
|
|
33
|
+
2. `new Map([...oldMap, [x, y]])` is the same as `const %tmp = new Map(oldMap); %tmp.set(x, y)`
|
|
34
|
+
provided that the `Map` iterator has not been modified.
|
|
35
|
+
|
|
36
|
+
If we can enforce that iterators haven't been modified, then we can do step 1 potentially even in
|
|
37
|
+
legacy code, and step 2 in typed code.
|