Share
Embed
Hermes: Better Performance with Bytecode Translation (React Universe 2024)
Introducing the Hermes JIT (bytecode translation) and diving deeper in how it works.
Tzvetan Mikov
September 12, 2024
Transcript
46 slides
-
HOW DO YOU THINK HE IS DOING NOW, A YEAR
LATER? • Probably still not very happy, since we haven’t released Static Hermes yet -
HERMES JavaScript engine for React Native Optimized for mobile Low
runtime resource consumption Extremely fast startup -
KEY FEATURES AOT compilation to bytecode Optimization happens once, before
execution Lightweight runtime Low memory footprint -
STATIC HERMES BUILDS UPON HERMES • Understands type annotations •
Great performance by compiling typed JS code ahead of time • Emits typed bytecode or native machine code • State of the art compiler pipeline • Leverages the best production native compiler: LLVM -
STATIC HERMES: LOGISTICAL CHALLENGES • Existing JS build pipelines do
not preserve type annotations • Shipping native code makes OTA updates harder • A lot of untyped code still exists. Amdahl’s law: • The performance of a system is limited by untyped JavaScript. -
STATIC HERMES: LOGISTICAL CHALLENGES • Existing JS build pipelines do
not preserve type annotations • Shipping native code makes OTA updates harder • A lot of untyped code still exists. Amdahl’s law: • The performance of a system is limited by untyped JavaScript. • The performance of a system is limited by its slowest part. -
BYTECODE TRANSLATION ON DEVICE • Bytecode is translated to machine
instructions at runtime • Ship bytecode like we do today; OTA updates work • Improved untyped performance • Excellent typed performance -
WAIT, ISN’T THAT … A JIT? • Technically, yes •
In JavaScript “JIT” tends to mean a very complex speculative runtime compiler • Bytecode translation is very lightweight by comparison • Designed for the Hermes AOT pipeline -
0 0.5 1 1.5 Box2D Crypto Gameboy Navier-stokes Richards N-body
TS Raytracer Untyped JS Benchmarks Hermes 2023 Hermes 2024 -
0 2 4 6 8 10 12 14 Raytracer nbody
Typed Benchmarks Hermes Untyped Typed Native -
RENDERING A MANDELBROT SET SOMETIMES THE NEW PERFORMANCE IS JUST
FUN https://github.com/tmikov/mandelbrot-demo -
DEEP DIVE: HOW DOES IT WORK? A brief tutorial on
building a Hermes JIT (but seriously, do not do this at home!) -
IN THE BEGINNING: HERMES BYTECODE Function<getprop>(2 params, 2 registers): LoadParam
r0, 1 GetByIdShort r1, r0, 1, "prop" LoadConstUInt8 r0, 100 Mul r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 Ret r0 function getprop(o) { return (o.prop * 100) - 1; } -
HERMES BYTECODE Function<getprop>(2 params, 2 registers): LoadParam r0, 1 GetByIdShort
r1, r0, 1, "prop" LoadConstUInt8 r0, 100 Mul r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 Ret r0 function getprop(o) { return (o.prop * 100) - 1; } -
HERMES BYTECODE Function<getprop>(2 params, 2 registers): LoadParam r0, 1 GetByIdShort
r1, r0, 1, "prop" LoadConstUInt8 r0, 100 Mul r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 Ret r0 function getprop(o) { return (o.prop * 100) - 1; } -
HERMES BYTECODE Function<getprop>(2 params, 2 registers): LoadParam r0, 1 GetByIdShort
r1, r0, 1, "prop" LoadConstUInt8 r0, 100 Mul r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 Ret r0 function getprop(o) { return (o.prop * 100) - 1; } -
Function<getprop> LoadParam r0, 1 GetByIdShort r1, r0, 1, "prop” LoadConstUInt8
r0, 100 Mul r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 mov x0, x19 mov w1, 1 bl _sh_ljs_param str x0, [x20, 16] mov x0, x19 add x1, x20, 16 mov w2, 658 ldr x3, [RO_DATA] add x3, x3, 16 bl _sh_ljs_get_by_id_rjs str x0, [x20, 24] mov x1, 100 str x1, [x20, 16] mov x0, x19 add x1, x20, 24 add x2, x20, 16 bl _sh_ljs_mul_rjs fmov d1, x0 fmov d0, 1 fsub d0, d1, d0 function call function call function call -
Function<getprop> LoadParam r0, 1 GetByIdShort r1, r0, 1, "prop” LoadConstUInt8
r0, 100 Mul r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 mov x0, x19 mov w1, 1 bl _sh_ljs_param str x0, [x20, 16] mov x0, x19 add x1, x20, 16 mov w2, 658 ldr x3, [RO_DATA] add x3, x3, 16 bl _sh_ljs_get_by_id_rjs str x0, [x20, 24] mov x1, 100 str x1, [x20, 16] mov x0, x19 add x1, x20, 24 add x2, x20, 16 bl _sh_ljs_mul_rjs fmov d1, x0 fmov d0, 1 fsub d0, d1, d0 function call function call function call -
Function<getprop> LoadParam r0, 1 GetByIdShort r1, r0, 1, "prop” LoadConstUInt8
r0, 100 Mul r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 mov x0, x19 mov w1, 1 bl _sh_ljs_param str x0, [x20, 16] mov x0, x19 add x1, x20, 16 mov w2, 658 ldr x3, [RO_DATA] add x3, x3, 16 bl _sh_ljs_get_by_id_rjs str x0, [x20, 24] mov x1, 100 str x1, [x20, 16] mov x0, x19 add x1, x20, 24 add x2, x20, 16 bl _sh_ljs_mul_rjs fmov d1, x0 fmov d0, 1 fsub d0, d1, d0 function call function call function call -
Function<getprop> LoadParam r0, 1 GetByIdShort r1, r0, 1, "prop” LoadConstUInt8
r0, 100 Mul r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 mov x0, x19 mov w1, 1 bl _sh_ljs_param str x0, [x20, 16] mov x0, x19 add x1, x20, 16 mov w2, 658 ldr x3, [RO_DATA] add x3, x3, 16 bl _sh_ljs_get_by_id_rjs str x0, [x20, 24] mov x1, 100 str x1, [x20, 16] mov x0, x19 add x1, x20, 24 add x2, x20, 16 bl _sh_ljs_mul_rjs fmov d1, x0 fmov d0, 1 fsub d0, d1, d0 function call function call function call -
Function<getprop> LoadParam r0, 1 GetByIdShort r1, r0, 1, "prop” LoadConstUInt8
r0, 100 Mul r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 mov x0, x19 mov w1, 1 bl _sh_ljs_param str x0, [x20, 16] mov x0, x19 add x1, x20, 16 mov w2, 658 ldr x3, [RO_DATA] add x3, x3, 16 bl _sh_ljs_get_by_id_rjs str x0, [x20, 24] mov x1, 100 str x1, [x20, 16] mov x0, x19 add x1, x20, 24 add x2, x20, 16 bl _sh_ljs_mul_rjs fmov d1, x0 fmov d0, 1 fsub d0, d1, d0 function call function call function call -
WE CAN DO EVEN BETTER • There were lots of
function calls • Function calls can be relatively expensive • JS is a funny language • Almost everything is valid. But: • Frequent patterns are cheap • Uncommon ones are expensive -
WE CAN DO EVEN BETTER • There were lots of
function calls • Function calls can be relatively expensive • JS is a funny language • Almost everything is valid. But: • Frequent patterns are cheap • Uncommon ones are expensive 123 * 456 "Joe Native" * [2,3] -
FAST AND SLOW PATHS • Split expensive operation in two
parts: • A slow path function call for all complicated and weird cases (“string” * [1,2,3]) • A fast path for the simple and fast cases (123 * 456) -
Mul r1, r1, r0 str x0, [x20, 24] str x1,
[x20, 16] cmp x0, x21 b.hs SLOW_1 fmov d0, x1 fmov d1, x0 fmul d1, d1, d0 CONT_1: ... SLOW_1: mov x0, x19 add x1, x20, 24 add x2, x20, 16 bl _sh_ljs_mul_rjs fmov d1, x0 b CONT_1 FAST AND SLOW PATHS CHECK FAST PATH SLOW PATH -
LoadConstUInt8 r0, 100 Mul r1, r1, r0 LoadConstUInt8 r0, 1
SubN r0, r1, r0 mov x1, 100 str x0, [x20, 24] str x1, [x20, 16] cmp x0, x21 b.hs SLOW_1 fmov d0, x1 fmov d1, x0 fmul d1, d1, d0 fmov d0, 1.0 fsub d0, d1, d0 COMMON EXECUTION TRACE No calls! -
STATIC HERMES • Static Hermes understands type annotations • Emits
bytecode instructions that know the types of their operands • Typed bytecode results in much faster machine instruction sequences -
HERMES TYPED BYTECODE Function<getprop>: LoadParam r0, 1 GetOwnBySlotIdx r1, r0,
0 LoadConstUInt8 r0, 100 MulN r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 Ret r0 type Obj = {prop: number}; function getprop(o: Obj): number { return (o.prop * 100) - 1; } -
TYPED VS UNTYPED BYTECODE LoadParam r0, 1 GetByIdShort r1, r0,
1, "prop" LoadConstUInt8 r0, 100 Mul r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 Ret r0 LoadParam r0, 1 GetOwnBySlotIdx r1, r0, 0 LoadConstUInt8 r0, 100 MulN r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 Ret r0 Typed Untyped -
Function<getprop> LoadParam r0, 1 GetOwnBySlotIdx r1, r0, 0 LoadConstUInt8 r0,
100 MulN r1, r1, r0 LoadConstUInt8 r0, 1 SubN r0, r1, r0 ldur x0, [x20, -72] mov x1, x0 and x1, x1, 0x0000ffffffffffff ldr x1, [x1, 48] mov x0, 100.0 fmov d0, x1 fmov d1, x0 fmul d0, d0, d1 fmov d1, 1.0 fsub d1, d0, d1 -
TAKEAWAYS • Moderate speed ups for untyped code • Can
be used for existing code and npm modules • Great speed ups for when using static types • React Native will use it for framework hot code • Developers could optionally use types to speed up non framework code -
HERMES V2: BUT WHEN? • When will all of this
be enabled in RN by default • As usual, everything is available on our GitHub • We follow a process where we release to RN after we have tested things internally -
HERMES V2: BUT WHEN? • In order of sooner to
later • Better language support (classes, etc) • Bytecode Translation • Static types