浏览器的影⼦子 • 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
句句柄之“局部句句柄” • 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 堆上的对象
句句柄之“持久句句柄” • 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.
“句句柄区域” • 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 个句句柄
~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
“上下⽂文” • 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.
“上下⽂文” • 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.
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.
线程池 • 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()