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,84 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: lazy-eval-compilation
|
|
3
|
+
title: Lazy/Eval Compilation
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
This is a high level overview of how we handle compiling code while the VM is executing, using existing Environments. Used for lazy compilation (deferring full compilation of functions until they are executed) and debugger local `eval` (used by the debugger REPL). For more detailed explanations about each of the parts, look at comments in the code for each of the compiler pipeline phases.
|
|
7
|
+
|
|
8
|
+
The concepts of lazy and eval compilation have conceptual overlap, but use of one doesn't technically require use of the other. It's possible to run lazy compilation but disallow debugger local `eval` and vice versa. The code supplied to debugger local `eval` may also be lazily compiled, but it doesn't have to be.
|
|
9
|
+
|
|
10
|
+
NOTE: All of the below currently only works when executing from source, as it relies on `BCProviderFromSrc`. Lazy compilation doesn't make sense when running from an HBC file (everything's already compiled) and debugging HBC hasn't been implemented yet. We also rely on optimizations not running, to avoid deleting variables/promoting them to the stack, because we rely on everything being in Environments at runtime.
|
|
11
|
+
|
|
12
|
+
# Parsing
|
|
13
|
+
|
|
14
|
+
The parser has 3 phases when running lazy compilation:
|
|
15
|
+
|
|
16
|
+
* `PreParse`: Run the parser over the whole file, but discard the AST. The only information retained is `PreParsedData`, which has information on function locations and metadata (arrow functions, strict mode, etc.).
|
|
17
|
+
* `LazyParse`: Run the parser using the `PreParsedData` to skip over sufficiently large function bodies. The function bodies are stubbed out and the `isLazyFunctionBody` flag on their `BlockStatementNode` is set to `true`.
|
|
18
|
+
* `FullParse`: A regular parse. Produces a full AST for the input JS source.
|
|
19
|
+
|
|
20
|
+
In lazy compilation, the `PreParse` and `LazyParse` passes run before execution begins. Then `FullParse` is run when the program attempts to actually execute a lazy function.
|
|
21
|
+
|
|
22
|
+
# Data Storage
|
|
23
|
+
|
|
24
|
+
The data we need to run both lazy compilation and debugger `eval` is stored in `BCProviderFromSrc`'s `CompilationData`. It points to all the information needed to run semantic resolution, IRGen, and HBC BCGen.
|
|
25
|
+
|
|
26
|
+
`BCProviderFromSrc` uses `shared_ptr` to store the IR `Module` and `SemContext`, because it's possible for us to use the same `Module` and `SemContext` for multiple `BCProviderFromSrc`s. That happens during debugger `eval` when the `SemContext` and IR are reused (to be able to access captured variables) but the source is new so we use a new `BCProvider` (because one `BCProvider` corresponds to one `RuntimeModule`, which corresponds to a single JS source).
|
|
27
|
+
|
|
28
|
+
The fact that we persist `Module` and `SemContext` allows us to run the compiler with new code almost as if it existed the whole time.
|
|
29
|
+
|
|
30
|
+
We introduce two IR instructions: `LazyCompilationDataInst` and `EvalCompilationDataInst`. These store data needed for lazy/eval compilation respectively and ensure that relevant Variables are kept alive by storing every parent `VariableScope` as an operand (the `VariableScope`s have users, so they won't get destroyed).
|
|
31
|
+
|
|
32
|
+
We can optionally have a pointer to an IR `Function` from the `functionIR_` field of `BytecodeFunction`, which allows us to get to the relevant `CompilationDataInst`. The `functionIR_` will be destroyed when the `BytecodeFunction` is itself destroyed along with its `BytecodeModule` when the GC cleans up the corresponding `RuntimeModule`.
|
|
33
|
+
|
|
34
|
+
# Semantic Resolution
|
|
35
|
+
|
|
36
|
+
## First pass
|
|
37
|
+
|
|
38
|
+
The first time the resolver runs with lazy compilation or in preparation for allowing debugger `eval`, it stores extra data to the `FunctionInfo` by populating the `bindingTableScope` field. In lazy compilation mode, it doesn't have to enter lazy functions because they're empty stub `BlockStatementNode`s.
|
|
39
|
+
|
|
40
|
+
## Actual Resolution
|
|
41
|
+
|
|
42
|
+
`SemanticResolver::runLazy` is the entrypoint into `SemanticResolver` for lazy compilation. It uses an *existing* `SemContext` and adds new information to it. After resolving a lazy function, it cleans up binding table state unless we still need it for debugger `eval` in the future.
|
|
43
|
+
|
|
44
|
+
`SemanticResolver::runInScope` allows for local `eval`, and operates similarly, though it requires a `ProgramNode` as input. It doesn't free any preexisting `SemContext` data. It is called with a *new* `SemContext`, which is created as a child of the original `SemContext` to share the binding table but to allow it to be freed separately from the root `SemContext`.
|
|
45
|
+
|
|
46
|
+
These paths just run the `SemanticResolver` as usual after some setup, because they can restore the binding table state and internal `SemanticResolver` state based on their input.
|
|
47
|
+
|
|
48
|
+
# IRGen
|
|
49
|
+
|
|
50
|
+
## First pass
|
|
51
|
+
|
|
52
|
+
When IRGen encounters a lazy function body stub, it calls `setupLazyFunction`, which makes an IR `Function` with a `LazyCompilationDataInst` and an `UnreachableInst` terminator.
|
|
53
|
+
|
|
54
|
+
When IRGen actually compiles a function, if debugger `eval` is to be supported, it replaces the body of the IR `Function` with an `EvalCompilationDataInst`.
|
|
55
|
+
|
|
56
|
+
We also now have two lists of `Function`s in the `Module`: one ordinary list of functions to be compiled, and a `compiledFunctions_` list which only holds functions that have already passed through the whole pipeline once. This is a performance optimization to avoid reiterating through these functions needlessly every single time we rerun IRGen.
|
|
57
|
+
|
|
58
|
+
## Actual IRGen
|
|
59
|
+
|
|
60
|
+
Running IRGen on a lazy `Function` can produce many new IR `Function`s, because there may be many small functions inside the lazy function. These new functions will be some mix of lazy and eagerly compiled. They are not added to the `compiledFunctions_` list yet.
|
|
61
|
+
|
|
62
|
+
# BCGen
|
|
63
|
+
|
|
64
|
+
This part interacts heavily with the IR data structures.
|
|
65
|
+
|
|
66
|
+
## First pass
|
|
67
|
+
|
|
68
|
+
On trying to run BCGen for a lazy `Function`, assign it a `bcFunctionID`. This allows it to get a dummy `CodeBlock` created at runtime, and we can use the ID to replace the lazy bytecode with real bytecode. Then, assign it to the `functionIR_` field on the new `BytecodeFunction` so that we can find it later. Move the function to the `compiledFunctions_` list so that we no longer treat it as a member of the regular function list in the IR lowering pipeline. Generate a mostly empty function header and empty bytecode, so we have enough information to run actual compilation later. So an IR `Function` used in lazy compilation can be: not compiled yet (and containing a `LazyCompilationDataInst`), assigned a BC ID but not actually compiled (and in `compiledFunctions_`), or destroyed and replaced with a real `Function` with real instructions.
|
|
69
|
+
|
|
70
|
+
Similarly for functions with `EvalCompilationDataInst`. They're already in the `compiledFunctions_` list, but we store them in `functionIR_`.
|
|
71
|
+
|
|
72
|
+
## Actual compilation
|
|
73
|
+
|
|
74
|
+
When the function is called during lazy compilation, `generateLazyFunctions` is issued to recompile *every* `Function` in the `Module` that isn't a lazy/eval function. This assigns `bcFunctionID`s to all newly discovered functions. They are then generated as usual, and the `BCProviderFromSrc` is *modified* to point to the updated values for string tables, bytecode function tables, etc. by calling `setBytecodeModuleRefs`.
|
|
75
|
+
|
|
76
|
+
`generateForEval` works similarly but the key distinction is that `eval` creates a new `BCProvider`, so the existing one is not updated, and there's no existing `BytecodeFunction` with an ID that has to be replaced.
|
|
77
|
+
|
|
78
|
+
After either of these functions consumes the newly generated functions in the `Module`, we call `resetForMoreCompilation` which cleans upany `Function`s we don't need any more or replaces their body with just `EvalCompilationDataInst` if they're needed still for `eval`. It also deletes any now-unused `VariableScope`s.
|
|
79
|
+
|
|
80
|
+
# Running the pipeline
|
|
81
|
+
|
|
82
|
+
Lazy compilation is called via `CodeBlock::lazyCompile`, which detects whether there's no bytecode on a `CodeBlock` and calls `hbc::compileLazyFunction`. It modifies the existing `BCProviderFromSrc`, and `CodeBlock` replaces its own internal state with the new information.
|
|
83
|
+
|
|
84
|
+
Debugger `eval` runs through the standard `directEval` pipeline, which uses `evalInEnvironment`. If `evalInEnvironment` is provided a `Handle<Environment>` then it won't run the pipeline from scratch but rather go through `hbc::compileEvalModule`.
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: memory-profilers
|
|
3
|
+
title: Memory Profilers
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
<!--
|
|
7
|
+
Note to editors adding new videos to this file: upload to
|
|
8
|
+
assets.hermesengine.dev instead of using local files. Videos will increase the
|
|
9
|
+
clone size for everyone who clones Hermes.
|
|
10
|
+
-->
|
|
11
|
+
|
|
12
|
+
This page describes the various tools you can use to diagnose memory issues in
|
|
13
|
+
your app using Hermes. The tools can be used in a variety of ways based on
|
|
14
|
+
your needs.
|
|
15
|
+
|
|
16
|
+
The main goal of these profilers are to help you identify ways your app can
|
|
17
|
+
reduce its memory footprint. This is important for improving the user experience
|
|
18
|
+
in many ways:
|
|
19
|
+
|
|
20
|
+
* Your app can run faster and have fewer, less expensive garbage collection
|
|
21
|
+
cycles
|
|
22
|
+
* Your app can avoid running out of memory entirely, and crashing with an Out of Memory (OOM) error
|
|
23
|
+
* Your app can avoid being killed by the operating system for using too much memory
|
|
24
|
+
|
|
25
|
+
# Heap Snapshots
|
|
26
|
+
|
|
27
|
+
A Heap Snapshot is a view of the entire JavaScript heap at a point in time. It
|
|
28
|
+
represents the data as a graph, where nodes are the values in the heap, and the
|
|
29
|
+
edges are pointers from one node to another. The snapshot can answer these types
|
|
30
|
+
of questions:
|
|
31
|
+
|
|
32
|
+
* What types of values is my app using?
|
|
33
|
+
* How much memory do those values use?
|
|
34
|
+
* What memory is being retained unnecessarily?
|
|
35
|
+
|
|
36
|
+
The nodes in the snapshot can be any value usable in JS, such as:
|
|
37
|
+
|
|
38
|
+
* Objects: `{a: 1, b: 2}`
|
|
39
|
+
* Arrays: `[1, 2, 3]`
|
|
40
|
+
* Functions: `function foo(args) { ... }`
|
|
41
|
+
* Strings: `"hello world"`
|
|
42
|
+
* Numbers: `3.14`
|
|
43
|
+
* Native host memory, injected into the heap via `jsi::HostFunction` and
|
|
44
|
+
`jsi::HostObject` in the JSI API
|
|
45
|
+
* Hermes-specific internal memory, such as `Environment`, `ArrayStorage`, and
|
|
46
|
+
`HiddenClass`, and the backing storage of ArrayBuffers.
|
|
47
|
+
|
|
48
|
+
Each node has a list of edges to other nodes, which can be used to determine
|
|
49
|
+
what nodes **retain** other nodes. If node A retains node B, that means if A
|
|
50
|
+
is reachable by your program, B will also remain reachable. Therefore, B cannot
|
|
51
|
+
be garbage collected until A is garbage collected.
|
|
52
|
+
Many heap snapshot viewers will define the difference between **shallow size**
|
|
53
|
+
and **retained size**. **Shallow size** is the size of just that node, whereas
|
|
54
|
+
**retained size** is the size of all the nodes the current node retains.
|
|
55
|
+
|
|
56
|
+
The snapshot format is a JSON file ending in `.heapsnapshot`, and is the same
|
|
57
|
+
format used by the Chrome Developer Tools and the V8 JavaScript engine. This
|
|
58
|
+
means all tools that work with heap snapshots taken from V8, Chrome, or Node.js
|
|
59
|
+
are also compatible with snapshots taken from Hermes.
|
|
60
|
+
|
|
61
|
+
In particular, you can take memory snapshots from the Chrome Developer Tools
|
|
62
|
+
connected to Hermes!
|
|
63
|
+
|
|
64
|
+
## Taking a heap snapshot with the Chrome DevTools
|
|
65
|
+
|
|
66
|
+
Before reading this section, make sure you can
|
|
67
|
+
[connect the Chrome DevTools to Hermes](https://reactnative.dev/docs/next/hermes#debugging-js-on-hermes-using-google-chromes-devtools)
|
|
68
|
+
to debug some JavaScript. Once you have that set up you can proceed.
|
|
69
|
+
|
|
70
|
+
1. Open the "Memory" tab of the DevTools, and make sure the "Heap snapshot" radio
|
|
71
|
+
button is selected
|
|
72
|
+
1. (Optional): Click on the garbage can icon in the top left to collect any
|
|
73
|
+
unreachable objects. This will prevent them from showing up in the snapshot.
|
|
74
|
+
1. Click on "Take snapshot"
|
|
75
|
+
|
|
76
|
+
<video autoplay muted loop controls height="auto" width="100%" alt="Take a heap snapshot from Chrome">
|
|
77
|
+
<source src="https://assets.hermesengine.dev/chrome_take_heap_snapshot.mp4" type="video/mp4" />
|
|
78
|
+
Your browser does not support the video/mp4 encoding.
|
|
79
|
+
</video>
|
|
80
|
+
|
|
81
|
+
Chrome will alert Hermes that a snapshot is requested, and Hermes will stream
|
|
82
|
+
the snapshot JSON file back to Chrome. This may take some time if you have a
|
|
83
|
+
large heap. Once it's completed, you can go to the
|
|
84
|
+
[Using the Chrome DevTools Heap Snapshot Explorer](#Using-the-Chrome-Devtools-Heap-Snapshot-Explorer)
|
|
85
|
+
section to start reading it.
|
|
86
|
+
|
|
87
|
+
You can save the snapshot to disk by pressing the "Save" button on your snapshot
|
|
88
|
+
in the Chrome menu:
|
|
89
|
+
|
|
90
|
+
<video autoplay muted loop controls height="auto" width="100%" alt="Save heap snapshot to disk from Chrome">
|
|
91
|
+
<source src="https://assets.hermesengine.dev/chrome_save_heap_snapshot.mp4" type="video/mp4" />
|
|
92
|
+
Your browser does not support the video/mp4 encoding.
|
|
93
|
+
</video>
|
|
94
|
+
|
|
95
|
+
## Taking a heap snapshot from C++
|
|
96
|
+
|
|
97
|
+
Using the Chrome DevTools only applies when you have attached a debugger. For
|
|
98
|
+
various reasons, it might be hard to do that for your app. In those cases, you
|
|
99
|
+
can write some C++ code to ask Hermes for a heap snapshot.
|
|
100
|
+
|
|
101
|
+
Before reading this section, make sure to read the JSI documentation which
|
|
102
|
+
explains all of the basics of the API.
|
|
103
|
+
|
|
104
|
+
Now, say you have some Native Module in React Native, and you want to create
|
|
105
|
+
a heap snapshot. Here's what you do:
|
|
106
|
+
|
|
107
|
+
```cpp
|
|
108
|
+
void myFunc(jsi::Runtime &rt) {
|
|
109
|
+
// If you want to write to a file name, use createSnapshotToFile.
|
|
110
|
+
rt.instrumentation().createSnapshotToFile("/tmp/filename.heapsnapshot");
|
|
111
|
+
// If you already have a C++ std::ostream set up, use createSnapshotToStream.
|
|
112
|
+
rt.instrumentation().createSnapshotToStream(std::cout);
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
This method gives you a lot of flexibility of when and where you want to set up
|
|
117
|
+
your heap snapshots. You can also expose the functionality above through a
|
|
118
|
+
`jsi::HostFunction` and call it from JS. The downside is you'll need to add a
|
|
119
|
+
new native module and build React Native from source for your app.
|
|
120
|
+
|
|
121
|
+
## Taking a heap snapshot from JavaScript with the Hermes CLI
|
|
122
|
+
|
|
123
|
+
Currently, it isn't possible to take a heap snapshot from JavaScript unless you
|
|
124
|
+
are running hermes via the command line interface.
|
|
125
|
+
|
|
126
|
+
If you are running from the command line interface, there's a function defined
|
|
127
|
+
on the global object called `createHeapSnapshot`, used like so:
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
// Calling with no arguments will print to stdout
|
|
131
|
+
createHeapSnapshot();
|
|
132
|
+
// Calling with a single string argument will write the snapshot to that file
|
|
133
|
+
// path. If it isn't a valid file, or the permissions won't allow Hermes to
|
|
134
|
+
// write to it, it'll throw a TypeError
|
|
135
|
+
createHeapSnapshot("/tmp/filename.heapsnapshot");
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Note that `createHeapSnapshot` does not exist if you are running Hermes in
|
|
139
|
+
React Native, or if Hermes was built without memory instrumentation, and
|
|
140
|
+
you will get an exception if you try to use it.
|
|
141
|
+
|
|
142
|
+
## Loading a heap snapshot from disk
|
|
143
|
+
|
|
144
|
+
If after any of the above methods you have a `filename.heapsnapshot` file saved
|
|
145
|
+
somewhere, you can load that into Chrome without needing to connect to any
|
|
146
|
+
running app. From the "Memory" tab, next to "Take snapshot" there is a "Load"
|
|
147
|
+
button which will open a file browser. Navigate to the file you want to load
|
|
148
|
+
and open it.
|
|
149
|
+
|
|
150
|
+
## Using the Chrome DevTools Heap Snapshot Explorer
|
|
151
|
+
|
|
152
|
+
Now you have a heap snapshot taken and you want to find a memory problem. Here's
|
|
153
|
+
an explanation of what you can do in Chrome.
|
|
154
|
+
|
|
155
|
+
### Sorting
|
|
156
|
+
|
|
157
|
+
You can sort by any of the following categories by clicking on the category
|
|
158
|
+
name:
|
|
159
|
+
|
|
160
|
+
* Retained Size: size of all nodes pointed to by this node
|
|
161
|
+
* Shallow Size: size of just the node itself
|
|
162
|
+
* Constructor: The name of the constructor of the node. Can think of it as a
|
|
163
|
+
type name
|
|
164
|
+
* Distance: The number of edges needed to traverse from a root to this node
|
|
165
|
+
|
|
166
|
+
Click a second time to reverse the order of the sort.
|
|
167
|
+
|
|
168
|
+
### Type names
|
|
169
|
+
You can search for a type name, which uses the name of the constructor function
|
|
170
|
+
by default. For example, if you have code like the following:
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
function MyObject() {}
|
|
174
|
+
var obj = new MyObject();
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
The node for `obj` in the snapshot will have the constructor name `MyObject`.
|
|
178
|
+
Expanding the type category will show all objects that are in the heap of that
|
|
179
|
+
type.
|
|
180
|
+
|
|
181
|
+
Hermes tries to be as specific as it can with type names for objects. For
|
|
182
|
+
objects created without a constructor, such as object literals
|
|
183
|
+
(`{a: 1, b: "hello"}`), Hermes will display its type name as `Object(a, b)`. If
|
|
184
|
+
an object has more than 5 properties, it will be displayed as
|
|
185
|
+
`Object(a, b, c, d, e, ...)`. If an object has more than
|
|
186
|
+
`HiddenClass::kDictionaryThreshold` properties (currently 64), it will be
|
|
187
|
+
displayed as `Object(Dictionary)`.
|
|
188
|
+
Warning: If you change the name of the constructor function dynamically, or if
|
|
189
|
+
the `.name` property of a function is an accessor, Hermes may only report the
|
|
190
|
+
original name of the function as defined in the source file.
|
|
191
|
+
|
|
192
|
+
### Retainers
|
|
193
|
+
|
|
194
|
+
If you click on a particular object in your snapshot, a drawer will pop up below
|
|
195
|
+
saying "Retainers". This drawer will show a tree of all nodes that have a
|
|
196
|
+
reference to this node.
|
|
197
|
+
|
|
198
|
+
If you click on the dropdown arrow for one node, you'll see all of the other
|
|
199
|
+
nodes which retain that node, and so on until you reach what's called a "root"
|
|
200
|
+
of the heap. You can also right-click on a node in the retainers drawer and
|
|
201
|
+
click on "Reveal in Summary view" to jump to that node in the summary box.
|
|
202
|
+
|
|
203
|
+

|
|
204
|
+
|
|
205
|
+
In this example, you can see that an object (who has an "a" property), is
|
|
206
|
+
retained by the property "x" in an instance of MyObject. That object is retained
|
|
207
|
+
by the 1,000th element in an array. That array is retained by `(Registers)`,
|
|
208
|
+
which is the root category for all local variables on the JS stack.
|
|
209
|
+
|
|
210
|
+
Below you can see some more of the root categories that Hermes uses to describe
|
|
211
|
+
some things that can retain objects:
|
|
212
|
+
|
|
213
|
+

|
|
214
|
+
|
|
215
|
+
The most important ones are:
|
|
216
|
+
|
|
217
|
+
* `(Registers)`: all local variables on the JS stack
|
|
218
|
+
* `(IdentifierTable)`: a table of strings used as object properties and symbol
|
|
219
|
+
descriptions
|
|
220
|
+
* `(GCScopes)`: the equivalent of the `(Registers)` group for native code inside
|
|
221
|
+
the Hermes engine. These hold onto JS values currently in use by native code
|
|
222
|
+
* `(Prototypes)`: lists of prototypes and constructors of system objects like
|
|
223
|
+
`Function` and array iterators
|
|
224
|
+
* `(Custom)`: these are any roots created by native code outside of Hermes. In
|
|
225
|
+
the case of any embedder using JSI (such as React Native), this corresponds to
|
|
226
|
+
the `jsi::Value`s being used
|
|
227
|
+
* `(SymbolRegistry)`: this is a table of all symbols made by `Symbol.for("foo")`
|
|
228
|
+
|
|
229
|
+
Knowing which root anchors your node is an important step to knowing why it is
|
|
230
|
+
being retained, and if it shouldn't be. If an object doesn't have a retaining
|
|
231
|
+
path to the root, it means there is nothing keeping it alive, and it will be
|
|
232
|
+
collected at the next garbage collection cycle.
|
|
233
|
+
|
|
234
|
+
Outside of root categories, there is another frequent retainer of objects:
|
|
235
|
+
the `Environment` type. An `Environment` in Hermes is a simple array that is
|
|
236
|
+
used to store captured variables of a closure. In this example you can see that
|
|
237
|
+
the variable `x` is captured:
|
|
238
|
+
|
|
239
|
+
```js
|
|
240
|
+
function makeCallback(x) {
|
|
241
|
+
return () => x;
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
The closure returned from `makeCallback` will capture `x`, and Hermes implements
|
|
246
|
+
this by placing `x` into an `Environment` pointed to by the closure.
|
|
247
|
+
Environments can be shared if multiple closures capture the same variable:
|
|
248
|
+
|
|
249
|
+
```js
|
|
250
|
+
function makeManyCallbacks(x) {
|
|
251
|
+
return [() => x = {foo: 1}, () => x.foo];
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
In this case, both closures will capture the same environment pointing to `x`,
|
|
256
|
+
so the second closure will see updates made by the first closure.
|
|
257
|
+
Taking a snapshot of some objects created by calling `makeManyCallbacks` shows
|
|
258
|
+
the following:
|
|
259
|
+
|
|
260
|
+

|
|
261
|
+
|
|
262
|
+
The `[1] in Array` shows this was the second closure stored in the array.
|
|
263
|
+
|
|
264
|
+
### Comparing Snapshots
|
|
265
|
+
|
|
266
|
+
If you have two heap snapshots from the same execution you can compare them to
|
|
267
|
+
each other and see which objects were allocated or destroyed between those
|
|
268
|
+
points in time.
|
|
269
|
+
|
|
270
|
+
First, load two snapshots into your Chrome workspace. Make sure they are from
|
|
271
|
+
the same process. Two snapshots from two separate runs of your app will not be
|
|
272
|
+
comparable, and the output might not make sense.
|
|
273
|
+
|
|
274
|
+
Then, click on the "Summary" dropdown box at the top and change it to
|
|
275
|
+
"Comparison". If you don't see the "Comparison" option that means you don't have
|
|
276
|
+
two snapshots loaded into Chrome yet. The following video shows how to do this:
|
|
277
|
+
|
|
278
|
+
<video autoplay muted loop controls height="auto" width="100%" alt="Heap Snapshot Comparison Video">
|
|
279
|
+
<source src="https://assets.hermesengine.dev/chrome_save_heap_snapshot.mp4" type="video/mp4" />
|
|
280
|
+
Your browser does not support the video/mp4 encoding.
|
|
281
|
+
</video>
|
|
282
|
+
|
|
283
|
+
Once you're in a comparison view, you can see what objects were created since
|
|
284
|
+
the last snapshot represented by a `+` icon, and what objects were destroyed
|
|
285
|
+
represented by a `-` icon. The columns are as follows:
|
|
286
|
+
|
|
287
|
+
* `# New`: Number of new objects created of a particular type
|
|
288
|
+
* `# Deleted`: Number of objects deleted (garbage collected)
|
|
289
|
+
* `# Delta`: New - Deleted. Positive means more were created than deleted.
|
|
290
|
+
* `Alloc. Size`: Size of all newly created objects of that type added together
|
|
291
|
+
* `Freed Size`: Size of all deleted objects of that type added together
|
|
292
|
+
* `Size Delta`: Alloc. Size - Freed Size. Positive means more bytes were created
|
|
293
|
+
than deleted.
|
|
294
|
+
|
|
295
|
+
Typically sorting by `Size Delta` is the most useful for hunting down memory
|
|
296
|
+
regressions. A large positive value means a lot of extra bytes were allocated
|
|
297
|
+
for that type of object.
|
|
298
|
+
|
|
299
|
+
Expanding the type category will show for each object ID, whether it was new
|
|
300
|
+
or deleted, and the size it was.
|
|
301
|
+
|
|
302
|
+
### Statistics View
|
|
303
|
+
|
|
304
|
+
From the same dropdown menu for selecting "Summary" or "Comparison", you can
|
|
305
|
+
also select "Statistics", which shows a pie chart of various memory categories.
|
|
306
|
+
This view only accounts for a few limited categories of memory, and doesn't have
|
|
307
|
+
any way to drill down into the category to see more, so this view is of limited
|
|
308
|
+
use.
|
|
309
|
+
|
|
310
|
+
# Heap Timelines
|
|
311
|
+
|
|
312
|
+
TODO: Fill out this section.
|
|
313
|
+
|
|
314
|
+
# Sampling Heap Profiler
|
|
315
|
+
|
|
316
|
+
TODO: Fill out this section.
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: modules
|
|
3
|
+
title: Modules
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
In which we describe the module system and metadata input.
|
|
7
|
+
|
|
8
|
+
### Metadata Format
|
|
9
|
+
|
|
10
|
+
In order to provide a directory or zipfile to Hermes,
|
|
11
|
+
it must contain a `metadata.json` file at the root.
|
|
12
|
+
|
|
13
|
+
The `metadata.json` file must be a JSON object, with the following fields:
|
|
14
|
+
|
|
15
|
+
- `segments`
|
|
16
|
+
- An object with string keys representing integers, which are the segments
|
|
17
|
+
IDs. The IDs are generally sequential but there can be gaps (ex. having IDs
|
|
18
|
+
"6" and "8" and no "7"). ID zero has special meaning, this is the "main
|
|
19
|
+
segment" that gets loaded on startup (ex. when React Native starts).
|
|
20
|
+
- Each value must be an array containing file paths
|
|
21
|
+
of the files to place into that segment, expressed relative to the root of
|
|
22
|
+
the ZIP file (ex. `subdir/foo.js`). File paths may include `./` at the start,
|
|
23
|
+
but they are not required to.
|
|
24
|
+
- The first element of `segments["0"]` is the first module `require`d at run time.
|
|
25
|
+
- `resolutionTable` (Optional)
|
|
26
|
+
- An object for which the keys are relative file names
|
|
27
|
+
(the same file names as in `segments`).
|
|
28
|
+
- Values are objects which map from strings given to `require()`
|
|
29
|
+
to their actual resolved file path relative to the directory or zip file root.
|
|
30
|
+
These relative paths should be the same as in `segments`, with leading `./`.
|
|
31
|
+
|
|
32
|
+
#### Example Metadata File
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
{
|
|
36
|
+
"segments": {
|
|
37
|
+
"0": [
|
|
38
|
+
"./cjs-subdir-main.js",
|
|
39
|
+
"cjs-subdir-2.js",
|
|
40
|
+
"bar/cjs-subdir-bar.js",
|
|
41
|
+
"foo/cjs-subdir-foo.js"
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
"resolutionTable": {
|
|
45
|
+
"./cjs-subdir-main.js": {
|
|
46
|
+
"foo": "./foo/cjs-subdir-foo.js"
|
|
47
|
+
},
|
|
48
|
+
"./foo/cjs-subdir-foo.js": {
|
|
49
|
+
"bar": "./bar/cjs-subdir-bar.js"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Require system
|
|
56
|
+
|
|
57
|
+
Modules can require each other using the `require` function provided as a local
|
|
58
|
+
variable by the runtime. The argument to `require` must be an absolute path
|
|
59
|
+
which root is the root of the ZIP file or input directory. For example:
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
const Foo = require('/subdir/foo.js');
|
|
63
|
+
|
|
64
|
+
Foo.doSmth();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Source maps
|
|
68
|
+
|
|
69
|
+
Each JavaScript file can optionally provide a corresponding source map, which is
|
|
70
|
+
the name of the source file with the suffix `.map`. For example `subdir/foo.js`
|
|
71
|
+
might provide a `subdir/foo.js.map` file.
|
|
72
|
+
|
|
73
|
+
### Complete design
|
|
74
|
+
|
|
75
|
+
Module mode is currently activated via passing `-commonjs` to Hermes while compiling.
|
|
76
|
+
Hermes can then be given a directory and a `metadata.json` file,
|
|
77
|
+
or a simple list of files with the first being the entry point.
|
|
78
|
+
|
|
79
|
+
First, each file is parsed just like any other JS file.
|
|
80
|
+
Then, a call to `hermes::wrapCJSModule` puts each file in its own "module function",
|
|
81
|
+
which binds `exports, require, module` as parameters.
|
|
82
|
+
|
|
83
|
+
IR generation relies on this wrapping of the AST.
|
|
84
|
+
Every CJS module file generates IR into a shared `hermes::Module`
|
|
85
|
+
(it's called `M` in `generateIRForSourcesAsCJSModules`).
|
|
86
|
+
Each of these generated CJS modules are now `hermes::Function *`s.
|
|
87
|
+
These are registered in the `cjsModules_` field using the `hermes::Module::addCJSModule` function,
|
|
88
|
+
which stores information in the `CJSModule` struct regarding filename, id, and IR function.
|
|
89
|
+
This allows lookup of the CJS modules either via `hermes::Function *` or by string literal (file path).
|
|
90
|
+
|
|
91
|
+
If the caller of the Hermes CLI passes `-static-require`,
|
|
92
|
+
then we attempt to resolve all `require` calls at compilation time.
|
|
93
|
+
This occurs in `ResolveStaticRequire.cpp`, which is able to resolve files if they were povided
|
|
94
|
+
by the user at invocation time and if all `require` calls only take string literals as arguments.
|
|
95
|
+
If every `require` call is able to be resolved, every one of these `require` calls is replaced
|
|
96
|
+
with a call to `HermesBuiltin_requireFast` with an ID for the CJS module,
|
|
97
|
+
and that function does no string work and is therefore very fast.
|
|
98
|
+
|
|
99
|
+
After all CJS modules have generated IR and all the `require`s have been resolved,
|
|
100
|
+
we generate one or more bytecode files from our `IR::Module M`.
|
|
101
|
+
This requires two special bits of logic in `hbc::generateBytecodeModule`.
|
|
102
|
+
- We add a mapping from a CJS module to the HBC function ID.
|
|
103
|
+
This allows us to actually run `require` when JS execution demands it.
|
|
104
|
+
If `require`s have been resolved, we add to the `cjsModulesStatic_` table in the HBC file,
|
|
105
|
+
else we add to to the `cjsModules_` table in the HBC file (which maps from strings instead of IDs).
|
|
106
|
+
- To accommodate bundle splitting, we also pass a `SegmentRange` to the function.
|
|
107
|
+
This allows us to only compile the functions which are needed by the CJS modules in the segment.
|
|
108
|
+
We then set a `cjsModuleOffset_` field in the HBC file,
|
|
109
|
+
which allows us to know how far into the complete set of CJS modules this segment is.
|
|
110
|
+
**Because every HBC file contains a contiguous set of CJS modules, each of which are unique,
|
|
111
|
+
it is not possible for multiple segments to contain the same CJS module.**
|
|
112
|
+
|
|
113
|
+
Having built 1 or more HBC files in the compiler, we can now execute them.
|
|
114
|
+
The `vm::Domain` data structure is used for keeping track of all HBC files which were compiled together.
|
|
115
|
+
In particular, the `Domain` owns a CJS module table.
|
|
116
|
+
This table is an array of CJS modules, indexed by the CJS module's ID.
|
|
117
|
+
If `require`s were not resolved at compile time, there's also a mapping from file path to that ID.
|
|
118
|
+
|
|
119
|
+
The first HBC file to be loaded must have a `cjsModuleOffset_` of `0`.
|
|
120
|
+
CJSModule `0` is the entry point, and will be executed first.
|
|
121
|
+
Loading of other segments is then done via a `loadSegment` call in the runtime;
|
|
122
|
+
the user can call it via the ConsoleHost `loadSegment` or more commonly via the Hermes API.
|
|
123
|
+
`loadSegment` does need a `requireContext`, which allows us to determine which `Domain` to actually
|
|
124
|
+
load the new CJS modules from the segment into.
|
|
125
|
+
This is registered into `require.context` (recall that `require` was bound as a param in all CJS modules).
|
|
126
|
+
|
|
127
|
+
Finally, we can `require`.
|
|
128
|
+
When `require` is called, one of two things happens:
|
|
129
|
+
- If we DID resolve all static `require`s, then calls to `require` were turned into Hermes builtin
|
|
130
|
+
calls, so the actual value of `require`s (the parameter) is not used.
|
|
131
|
+
We simply call `HermesBuiltin_requireFast` via the `CallBuiltin` instruction.
|
|
132
|
+
- If we DID NOT resolve all static `require`s, we call the `require` function.
|
|
133
|
+
The runtime will have set up that function to also pass along the base path (to allow relative `require`),
|
|
134
|
+
so `require` is now a BoundFunction (as if we'd done `require = require.bind(currentPath)`).
|
|
135
|
+
Note that this is transparent to the application developer, and doesn't change the way `require`
|
|
136
|
+
is actually called in JS source.
|
|
137
|
+
In either case, we do `runRequireCall` as implemented in `require.cpp`.
|
|
138
|
+
It performs any necessary checks, calls the target CJS module, and caches and reads `exports` back.
|
|
139
|
+
|
|
140
|
+
Hermes also partially supports ECMAScript modules.
|
|
141
|
+
Currently, the interop of ESM and CJS modules is defined simply by turning `import` into `require` calls.
|
|
142
|
+
This precludes full correctness of the ESM module system, which requires _live bindings_;
|
|
143
|
+
when a value is changed in the imported module, the local binding to that value must also be updated.
|
|
144
|
+
Tests for the current emitted code can be found in `test/hermes/esm/`.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: optimizer
|
|
3
|
+
title: Design of the Optimizer
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
### Introduction
|
|
7
|
+
|
|
8
|
+
This document describes the high-level design of the Hermes optimizer. The
|
|
9
|
+
Hermes optimizer transforms the Hermes IR into a more efficient representation
|
|
10
|
+
that preserves the original semantics of the program. The IR.md document describes
|
|
11
|
+
the design of the Hermes IR.
|
|
12
|
+
|
|
13
|
+
### Key concepts
|
|
14
|
+
|
|
15
|
+
This section describes a few key concepts and ideas:
|
|
16
|
+
|
|
17
|
+
- The optimizer is responsible for optimizing the IR. IRGen and BytecodeGen
|
|
18
|
+
are not the right place for implementing optimizations. The parts of the
|
|
19
|
+
compiler that translate from one representation to another are inherently
|
|
20
|
+
complex because they require the understanding of the semantics of both
|
|
21
|
+
representations. Moreover, translators are not designed like optimizers.
|
|
22
|
+
They do not have good access to analysis and do not allow the separation
|
|
23
|
+
of the optimizer from the translation, which makes debugging more
|
|
24
|
+
difficult.
|
|
25
|
+
|
|
26
|
+
- Passes: Optimizations are organized in passes. There are two kinds of
|
|
27
|
+
passes: function passes and module passes. Function passes can modify only
|
|
28
|
+
the functions that they operate on, while module passes operate on the whole
|
|
29
|
+
module. Function passes are allowed to read the whole module but only
|
|
30
|
+
touch the current function.
|
|
31
|
+
|
|
32
|
+
- Analysis: Analyses are caches in front of a computation of some property.
|
|
33
|
+
For example, the dominator analysis is a cache that helps reduce compile
|
|
34
|
+
time by removing the need to recompute the dominator tree for each
|
|
35
|
+
function. Analyses are all about caching and invalidating pre-computed
|
|
36
|
+
properties.
|
|
37
|
+
|
|
38
|
+
- Optimizations do one thing: Optimizations are designed to be simple and
|
|
39
|
+
this means that they do only one thing. For example, the common
|
|
40
|
+
subexpression elimination optimization does not delete dead code
|
|
41
|
+
"on the way" just because it can.
|
|
42
|
+
|
|
43
|
+
- Optimizations are predictable: Sometimes there are several legal
|
|
44
|
+
representations of the program, but the optimizer should never
|
|
45
|
+
randomize the output of the compiler. Randomization of the output
|
|
46
|
+
happens when the output depends on runtime information such as the order
|
|
47
|
+
of elements in a set or map. Randomizing the output of the compiler
|
|
48
|
+
makes it very difficult to write tests and reproduce bugs. LLVM has
|
|
49
|
+
data structures that provide guaranteed order - use them!
|
|
50
|
+
|
|
51
|
+
- Write compile-time efficient algorithms: The compile time of a compiler is a
|
|
52
|
+
very important metric and we attempt to minimize compile time as much as
|
|
53
|
+
possible. Do not write exponential algorithms (or polynomial algorithm with
|
|
54
|
+
a high degree). If you are writing a quadratic algorithm make sure to
|
|
55
|
+
implement a sliding-window or other techniques that will allows to limit
|
|
56
|
+
the quadratic search to a small subset of the graph. Always assume that
|
|
57
|
+
there exist a function with hundreds of consecutive basic blocks or a basic
|
|
58
|
+
block with thousands of instructions. If you are writing a "solver" then
|
|
59
|
+
you are probably doing it wrong.
|
|
60
|
+
|
|
61
|
+
- There are three kinds of transformations: canonicalization,
|
|
62
|
+
simplification and lowering. Make sure that you know exactly what kind of
|
|
63
|
+
transformation you are doing and why. Canonicalizations are transformations that
|
|
64
|
+
expose opportunities for other transformations. Re-association (reducing tree
|
|
65
|
+
height, placing constants on the RHS, etc.) is a canonicalization because it
|
|
66
|
+
organizes things in predictable patterns and makes the life of future
|
|
67
|
+
optimizations simpler by reducing the number of possible inputs. Inlining is
|
|
68
|
+
another example of effective canonicalization because it exposes opportunities
|
|
69
|
+
for optimizations in the caller function (by providing more information).
|
|
70
|
+
Another example is loop rotation, which is a canonical representation of all
|
|
71
|
+
loops. In canonicalization we strive to clean up the program as much as
|
|
72
|
+
possible and reach a pure representation of the program. Simplification is what
|
|
73
|
+
we normally think of as optimizations, like removing redundancy by deleting dead
|
|
74
|
+
code and optimizing arithmetic, etc. Canonicalization can allow simplification
|
|
75
|
+
that can allow more canonicalization. For example, de-virtualization unblocks
|
|
76
|
+
inlining that may allow some transformations that enable more de-virtualization.
|
|
77
|
+
Lowering transformations are the opposite of canonicalization. In Lowering
|
|
78
|
+
transformations we generate patterns that are closer to the target
|
|
79
|
+
representation. We may not be able to recover from lowering transformations. One
|
|
80
|
+
example for lowering transformation is loop strength reduction where the
|
|
81
|
+
optimizer transforms loop indices into non-consecutive accesses that fit with
|
|
82
|
+
the hardware instruction set. Another example is loop versioning where the body
|
|
83
|
+
of the loop is duplicated and versioned multiple times .
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: perf-profiling
|
|
3
|
+
title: perf Profiling
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
This document describes how to profile the performance of Static Hermes.
|
|
7
|
+
|
|
8
|
+
## Profiling JIT
|
|
9
|
+
|
|
10
|
+
Currently, we rely on Linux perf tool for profiling JIT'ed code, and we only
|
|
11
|
+
enable JIT on ARM64 platform. So you need to have a Linux machine with ARM64.
|
|
12
|
+
|
|
13
|
+
### How To Profile
|
|
14
|
+
|
|
15
|
+
1. Recording the trace. `-Xperf-prof` is to enable perf profiling, `-Xperf-prof-dir`
|
|
16
|
+
is to specify the output directory (by default it's `/tmp` so if you don't want
|
|
17
|
+
to look into those generated files, you can skip this flag). `-k mono` means we
|
|
18
|
+
use CLOCK_MONOTONIC for clock id, as it's used in jitdump. Here we only sample
|
|
19
|
+
user space events, but you could also sample kernel events and also specify
|
|
20
|
+
different flags (e.g., -g for call graph).
|
|
21
|
+
2. Injecting the jitdump into the perf.data. This will create an ELF binary for
|
|
22
|
+
each JIT'ed function.
|
|
23
|
+
3. Reporting the trace.
|
|
24
|
+
```bash
|
|
25
|
+
perf record -g -k mono -e cycles:u ./bin/hermes ~/js/test.js -Xjit=force \
|
|
26
|
+
-Xperf-prof -Xperf-prof-dir ./
|
|
27
|
+
perf inject -j -i perf.data -o perf.data.jitted
|
|
28
|
+
perf report -i perf.data.jitted --call-graph=fractal,callee --children
|
|
29
|
+
```
|