(evented)
a model of I/O
+
(JavaScript)
a language that was well suited to the style of programming
=
Node.js
Slide 6
Slide 6 text
浏览器的影⼦子
• v8 “上下⽂文”的设计:each window and iframe in a
browser can have its own fresh JavaScript
environment. [提问 node]
• v8 限制堆的⼤大⼩小,浏览器不太可能⽤用⼤大量量内存
• node::Buffer (跳过 V8的内存分配,堆外内存)
与 v8::Uint8Array / TypedArray 的故事
• CESU-8 encoding
Slide 7
Slide 7 text
No content
Slide 8
Slide 8 text
No content
Slide 9
Slide 9 text
C++ 11
(env.cc)
auto close_and_finish = [](Environment* env,
uv_handle_t* handle, void* arg) {
…
}
“隔离区”
• class v8::Isolate {}
• An isolate is a VM instance with its own heap
• ⼀一个隔离区:⼀一个 v8 堆
Slide 24
Slide 24 text
句句柄之“局部句句柄”
• template class Local {}
• Because the garbage collector may move objects,
it is unsafe to point directly to an object.
Slide 25
Slide 25 text
句句柄之“局部句句柄”
• template class Local {}
• An object reference managed by the v8 garbage
collector.
• A local handle is a pointer to an object. All V8
objects are accessed using handles, they are
necessary because of the way the V8 garbage
collector works.
• ⼀一个局部句句柄:⼀一个 v8 堆上的对象
Slide 26
Slide 26 text
句句柄之“持久句句柄”
• Used when keeping a reference to an object for
more than one function call, or when handle
lifetimes do not correspond to C++ scopes.
• A UniquePersistent handle relies on
C++ constructors and destructors to manage the
lifetime of the underlying object.
• A Persistent can be constructed with
its constructor, but must be explicitly cleared with
Persistent::Reset.
Slide 27
Slide 27 text
“句句柄区域”
• class V8_EXPORT HandleScope {}
• A stack-allocated class that governs a number of
local handles.
• a container for any number of handles. When
you've finished with your handles, instead of
deleting each one individually you can simply
delete their scope.
• ⼀一个句句柄区域:N 个句句柄
Slide 28
Slide 28 text
~HandleScope, is called the handle scope is deleted. Objects
referred to by handles within the deleted handle scope are
eligible for removal in the next garbage collection
Slide 29
Slide 29 text
“上下⽂文”
Slide 30
Slide 30 text
“上下⽂文”
• class V8_EXPORT Context {}
• JavaScript provides a set of built-in utility functions
and objects that can be changed by JavaScript code.
• Creation of contexts implies creating the built-in
objects and parsing the built-in JavaScript code (with
cache)
• each window and iframe in a browser can have its
own fresh JavaScript environment.
Slide 31
Slide 31 text
“上下⽂文”
• class V8_EXPORT Context {}
• A sandboxed execution context with its own set of
built-in objects and functions.
• an execution environment that allows separate,
unrelated, JavaScript code to run in a single
instance of V8. You must explicitly specify the
context in which you want any JavaScript code to
be run.
v8 GC 特征
• “stop-the-world”
stops program execution when performing a garbage
collection cycle
• “generational”
processes only part of the object heap in most garbage
collection cycles. This minimises the impact of stopping
the application
• “accurate”
always knows exactly where all objects and pointers are in
memory. This avoids falsely identifying objects as pointers
which can result in memory leaks.
Slide 34
Slide 34 text
No content
Slide 35
Slide 35 text
libuv 初探
Slide 36
Slide 36 text
libuv
• supports Linux via epoll
• supports Windows via Microsoft IOCP
• supports Mac OS X via kqueue
• same API on Linux, Windows, Mac
• Little dependencies (On node-v0.9.0's version of
libuv, the dependency on libev was removed)
“句句柄”
• Handles represent long-lived objects capable of
performing certain operations while active.
• e.g.: a prepare handle gets its callback called once
every loop iteration when active
• e.g.: a TCP server handle get its connection
callback called every time there is a new
connection.
“请求”
• Requests represent (typically) short-lived
operations. These operations can be performed
over a handle
• e.g.: write requests are used to write data on a
handle
• e.g.(standalone): getaddrinfo requests don’t need
a handle they run directly on the loop
事件循环
• tied to a single thread
• all (network) I/O is performed on non-blocking
sockets which are polled using the best
mechanism available on the given platform
• the loop will block waiting for I/O activity on sockets
which have been added to the poller and callbacks
will be fired indicating socket conditions (readable,
writable hangup) so handles can read, write or
perform the desired I/O operation.
Slide 47
Slide 47 text
No content
Slide 48
Slide 48 text
线程池
• a global thread pool on which all loops can queue
work on
• runs blocking file I/O operations
• runs DNS functions (getaddrinfo and getnameinfo)
• runs User specified code via uv_queue_work()
Slide 49
Slide 49 text
(possibly)
• linux AIO (supported in the kernel)
• posix AIO (supported by linux, Mac OS X, BSD,
solaris, AIX etc.)
• Windows’ overlapped I/O