KEMBAR78
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood" | PDF
V8 + libuv = Node.js
Under the hood
Юрий Шевцов
Все познается в
сравнении
int func2(int arg1, char *arg2) {
char str[100];
/* some other expressions */
}
int func1() {
int foo = 0;
int bar = 3;
char* buzz = malloc(5);
func2(foo, buzz);
}
char* buzz (stack slot with
address)
int bar = 3
int foo = 3
Return address
Params of func1
Stack
int func2(int arg1, char *arg2) {
char str[100];
/* some other expressions */
}
int func1() {
int foo = 0;
int bar = 3;
char* buzz = malloc(5);
func2(foo, buzz);
}
str[99]
str[1] … str[98]
str[0];
Return address (line 10)
arg2 = buzz
arg1 = foo = 3
Data of func1
Stack
Stackframe
● Arguments
● Local variables
● Return address
Everything is object on the Heap
for (let i = 0; i < 42; i++) {}
foo[1]
foo[231]
Except SMI (Small integers)
2 42
570
200
Tagged pointer
32nd bit 1st bit
Data bits
T == 0 - SMI
T == 1 - Pointer T X X X X X ... X X X X X X
Object
representation
Hashtable
VS
Optimised View
Closer look
JSObject
Map
Extra Properties
Elements
Property “a”
Property “b”
FixedArray
Map
Length
Property “c”
Property “d”
FixedArray
Map
Length
Property “1”
Property “2”
Property “3”
32 Slots and extra properties
1. Object gets 32 slots
2. V8 trims unused slots later
3. Extra properties go separately
Elements
● Keys are array indices
● Elements may be represented as simple
array
● Unbox values, if numbers only
Map a.k.a Hidden Class
● Stores object layout ({ property: offset })
● Assignment order matters
● New map created for each object change
● Allows fast property lookup
Maps creation
function Foo(bar, buzz) {
this.bar = bar;
this.buzz = buzz;
}
const foo1 = new Foo(1, 2);
const foo2 = new Foo(3, 4);
foo1.some = 5;
{ } - C0
{ bar } - C1
{ bar, buzz } - C2
{ bar, buzz, some } - C3
Inline caching
Heap Structure
New-space Old-space And more...
Garbage collection
Scavenge | Mark & sweep
Mark
Compact
Asynchronicity
<script type="text/javascript">
setTimeout(() => console.log(1));
Promise.resolve().then(() => console.log(2));
</script>
Two standards
VS
ECMAScript WHATVG
Task types
Tasks:
setTimeout, requestAnimationFrame, I/O, UI
rendering
Microtasks:
Promises, process.nextTick, MutationObserver
Event loop algorithm
1. Run task till completion
2. Run microtasks
3. Run microtask scheduled by another
microtask
Correct answer
2
1
Hint: running script is also a task
V8 compilers
Before
Full-codegen - generates unoptimized machine code
Crankshaft - optimizes hot functions
V8 compilers
Ignition - generates bytecode
Turbofan - optimizes, using Sea-of-Nodes graph
interpreter + compiler
V8 is a library
Bare V8 runs scripts
Isolate* isolate = Isolate::New()
Local<Context> context = Context::New(isolate)
Local<String> source = String::New(isolate, "'Hello , World!'")
Local<Script> script = Script::Compile(context, source)
Local<Value> result = script->Run(context)
V8 API terminology
Isolate - instance of V8 VM
Context - holds pristine copy of global object
Local<?> - handle for JS object in heap
Two templates
Functions
Objects
Function Template
void LogCallback(FunctionCallbackInfo<Value>& args) {}
global->Set("log", FunctionTemplate::New(isolate, LogCallback));
Object Template
Interceptors Accessors
Example
global_templ->SetAccessor("foo", XGetter, XSetter);
void XGetter(Local<String> property) {
info.GetReturnValue().Set(x);
}
void XSetter(Local<String> property, Local<Value> value) {
x = value->Int32Value();
}
Local<ObjectTemplate> global = ObjectTemplate::New(isolate);
do {
uv__update_time(loop);
uv__run_timers(loop);
uv__run_idle(loop);
uv__run_prepare(loop);
uv__io_poll(loop, timeout);
uv__run_check(loop);
uv__run_closing_handles(loop);
} while (uv__loop_alive(loop))
Node.js is...
● V8 initializer
● libuv wrapper
● Module loader
Your module is a function
NativeModule.wrap = function(script) {
return NativeModule.wrapper[0]+script+NativeModule.wrapper[1];
};
NativeModule.wrapper = [
'(function (exports, require, module, __filename, __dirname) { ',
'n});'
];
Native modules
NAN vs N-API
Спасибо за внимание!

Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"