Home > Article > Web Front-end > In-depth understanding of the buffer cache area in Nodejs
This article will take you to understand the buffer cache area in NodeJs and introduce the implementation of Buffer in Node.js. Let’s take a look!
Involved knowledge points
Let’s talk about JavaScript first The interface and background of ArrayBuffer, the following content comes from ECMAScript 6 Getting Started with ArrayBuffer .
ArrayBuffer object, TypedArray view and DataView view are an interface for JavaScript to operate binary data. These objects have long existed as a separate specification (released in February 2011), and ES6 has incorporated them into the ECMAScript specification and added new methods. They all process binary data using array syntax, so they are collectively called binary arrays.
The original design purpose of this interface is related to the WebGL project. The so-called WebGL refers to the communication interface between the browser and the graphics card. In order to meet the large-scale, real-time data exchange between JavaScript and the graphics card, the data communication between them must be binary, not the traditional text format. If a 32-bit integer is passed in text format, the JavaScript scripts and graphics cards at both ends must perform format conversion, which will be very time-consuming. At this time, if there is a mechanism that can directly operate bytes like the C language and send the 4-byte 32-bit integer to the graphics card intact in binary form, the performance of the script will be greatly improved.
Binary array was born in this context. It is very similar to an array in the C language, allowing developers to directly operate memory in the form of array subscripts, which greatly enhances JavaScript's ability to process binary data, making it possible for developers to perform binary communication with the native interface of the operating system through JavaScript.
After reading this, we know that the ArrayBuffer series interface enables JavaScript to process binary data. Its use is mainly divided into the following steps
Pass ArrayBuffer constructor, creates a memory area with a length of 10
Pass parameters through the Uint8Array constructor to point to ArrayBuffer
The same as operating an array The first byte written data 123
const buf1 = new ArrayBuffer(10); const x1 = new Uint8Array(buf1); x1[0] = 123;
can also be processed using ArrayBuffer related interfaces in Node.js Binary data, after carefully reading the documents of ArrayBuffer and Buffer, we can find that further encapsulation of Buffer can make it easier to get started and achieve better performance. Next, let us take a look at the use of Buffer. Example
Create a memory area with a length of 10 through the alloc method
Write data 123 to the first byte through writeUInt8
Read the first byte of data through readUint8
const buf1 = Buffer.alloc(10); buf1.writeUInt8(123, 0) buf1.readUint8(0)
By static Method alloc creates a Buffer instance
Tips: The method of creating an instance directly through the Buffer constructor has been abandoned due to security issues
Buffer.alloc = function alloc(size, fill, encoding) { assertSize(size); if (fill !== undefined && fill !== 0 && size > 0) { const buf = createUnsafeBuffer(size); return _fill(buf, fill, 0, buf.length, encoding); } return new FastBuffer(size); }; class FastBuffer extends Uint8Array { constructor(bufferOrLength, byteOffset, length) { super(bufferOrLength, byteOffset, length); } }
I found that Buffer is actually Uint8Array, here again In addition, in JavaScript, you can also directly use Uint8Array to operate memory without passing the ArrayBuffer object, such as the following example
Create a memory area with a length of 10 through the Uint8Array constructor
Write data to the first byte just like operating an array 123
const x1 = new Uint8Array(10); x1[0] = 123
Then Buffer in Node.js only uses the Uint8Array class, how to simulate and implement all of the following The behavior of view types, and what other extensions has been made to Buffer?
提供了 alloc, allocUnsafe, allocUnsafeSlow 3个方法去创建一个 Buffer 实例, 上面讲了 alloc 方法没有什么特别, 下面讲一下另外两种方法
与 alloc 不同的是, allocUnsafe 并没有直接返回 FastBuffer, 而是始终从 allocPool 中类似 slice 出来的内存区。
Buffer.allocUnsafe = function allocUnsafe(size) { assertSize(size); return allocate(size); }; function allocate(size) { if (size <= 0) { return new FastBuffer(); } if (size < (Buffer.poolSize >>> 1)) { if (size > (poolSize - poolOffset)) createPool(); const b = new FastBuffer(allocPool, poolOffset, size); poolOffset += size; alignPool(); return b; } return createUnsafeBuffer(size); }
这块内容其实我也是很早之前在读朴灵大佬的深入浅出 Node.js 就有所映像, 为什么这样做了, 原因主要如下
为了高效地使用申请来的内存,Node采用了slab分配机制。slab是一种动态内存管理机制,最早
诞生于SunOS操作系统(Solaris)中,目前在一些*nix操作系统中有广泛的应用,如FreeBSD和Linux。 简单而言,slab就是一块申请好的固定大小的内存区域。slab具有如下3种状态。
当我们需要一个Buffer对象,可以通过以下方式分配指定大小的Buffer对象:
new Buffer(size); Node以8 KB为界限来区分Buffer是大对象还是小对象: Buffer.poolSize = 8 * 1024; 这个8 KB的值也就是每个slab的大小值,在JavaScript层面,以它作为单位单元进行内存的分配。
比起 allocUnsafe 从预先申请好的 allocPool 内存中切割出来的内存区, allocUnsafeSlow 是直接通过 createUnsafeBuffer 先创建的内存区域。从命名可知直接使用 Uint8Array 等都是 Slow 缓慢的。
Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) { assertSize(size); return createUnsafeBuffer(size); };
这个 Unsafe 不安全又是怎么回事了, 其实我们发现直接通过 Uint8Array 申请的内存都是填充了 0 数据的认为都是安全的, 那么 Node.js 又做了什么操作使其没有被填充数据了 ?
let zeroFill = getZeroFillToggle(); function createUnsafeBuffer(size) { zeroFill[0] = 0; try { return new FastBuffer(size); } finally { zeroFill[0] = 1; } }
那么我们只能去探究一下 zeroFill 在创建前后, 类似开关的操作的是如何实现这个功能
zeroFill 的值来自于 getZeroFillToggle 方法返回, 其实现在 src/node_buffer.cc 文件中, 整个看下来也是比较费脑。
简要的分析一下 zeroFill 的设置主要是修改了 zero_fill_field 这个变量的值, zero_fill_field 值主要使用在 Allocate 分配器函数中。
void GetZeroFillToggle(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); NodeArrayBufferAllocator* allocator = env->isolate_data()->node_allocator(); Local<ArrayBuffer> ab; // It can be a nullptr when running inside an isolate where we // do not own the ArrayBuffer allocator. if (allocator == nullptr) { // Create a dummy Uint32Array - the JS land can only toggle the C++ land // setting when the allocator uses our toggle. With this the toggle in JS // land results in no-ops. ab = ArrayBuffer::New(env->isolate(), sizeof(uint32_t)); } else { uint32_t* zero_fill_field = allocator->zero_fill_field(); std::unique_ptr<BackingStore> backing = ArrayBuffer::NewBackingStore(zero_fill_field, sizeof(*zero_fill_field), [](void*, size_t, void*) {}, nullptr); ab = ArrayBuffer::New(env->isolate(), std::move(backing)); } ab->SetPrivate( env->context(), env->untransferable_object_private_symbol(), True(env->isolate())).Check(); args.GetReturnValue().Set(Uint32Array::New(ab, 0, 1)); }
内存分配器的实现
从代码实现可以看到如果 zero_fill_field 值为
void* NodeArrayBufferAllocator::Allocate(size_t size) { void* ret; if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers) ret = UncheckedCalloc(size); else ret = UncheckedMalloc(size); if (LIKELY(ret != nullptr)) total_mem_usage_.fetch_add(size, std::memory_order_relaxed); return ret; }
接着 Allocate 函数的内容
关于 calloc 与 realloc 函数
至此读到这里, 我们知道了 createUnsafeBuffer 创建未被初始化内存的完整实现, 在需要创建时设置 zero_fill_field 为 0 即假值即可, 同步创建成功再把 zero_fill_field 设置为 1 即真值就好了。
inline T* UncheckedCalloc(size_t n) { if (n == 0) n = 1; MultiplyWithOverflowCheck(sizeof(T), n); return static_cast<T*>(calloc(n, sizeof(T))); } template <typename T> inline T* UncheckedMalloc(size_t n) { if (n == 0) n = 1; return UncheckedRealloc<T>(nullptr, n); } template <typename T> T* UncheckedRealloc(T* pointer, size_t n) { size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n); if (full_size == 0) { free(pointer); return nullptr; } void* allocated = realloc(pointer, full_size); if (UNLIKELY(allocated == nullptr)) { // Tell V8 that memory is low and retry. LowMemoryNotification(); allocated = realloc(pointer, full_size); } return static_cast<T*>(allocated); }
通过 Uint8Array 如何写入读取 Int8Array 数据? 如通过 writeInt8 写入一个有符号的 -123 数据。
const buf1 = Buffer.alloc(10); buf1.writeInt8(-123, 0)
对写入的数值范围为 -128 到 127 进行了验证
直接进行赋值操作
其实作为 Uint8Array 对应的 C 语言类型为 unsigned char, 可写入的范围为 0 到 255, 当写入一个有符号的值时如 -123, 其最高位符号位为 1, 其二进制的原码为 11111011, 最终存储在计算机中所有的数值都是用补码。所以其最终存储的补码为 10000101, 10 进制表示为 133。
此时如果通过 readUInt8 去读取数据的话就会发现返回值为 133
如果通过 readInt8 去读取的话, 套用代码的实现 133 | (133 & 2 ** 7) * 0x1fffffe === -123 即满足要求
function writeInt8(value, offset = 0) { return writeU_Int8(this, value, offset, -0x80, 0x7f); } function writeU_Int8(buf, value, offset, min, max) { value = +value; // `checkInt()` can not be used here because it checks two entries. validateNumber(offset, 'offset'); if (value > max || value < min) { throw new ERR_OUT_OF_RANGE('value', `>= ${min} and <= ${max}`, value); } if (buf[offset] === undefined) boundsError(offset, buf.length - 1); buf[offset] = value; return offset + 1; } function readInt8(offset = 0) { validateNumber(offset, 'offset'); const val = this[offset]; if (val === undefined) boundsError(offset, this.length - 1); return val | (val & 2 ** 7) * 0x1fffffe; }
计算机中的有符号数有三种表示方法,即原码、反码和补码。三种表示方法均有符号位和数值位两部分,符号位都是用0表示“正”,用1表示“负”,而数值位,三种表示方法各不相同。在计算机系统中,数值一律用补码来表示和存储。原因在于,使用补码,可以将符号位和数值域统一处理;同时,加法和减法也可以统一处理。
通过 Uint8Array 如何写入读取 Uint16Array 数据?
从下面的代码也是逐渐的看清了 Uint8Array 的实现, 如果写入 16 位的数组, 即会占用两个字节长度的 Uint8Array, 每个字节存储 8 位即可。
function writeU_Int16BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 1); buf[offset++] = (value >>> 8); buf[offset++] = value; return offset; } function readUInt16BE(offset = 0) { validateNumber(offset, 'offset'); const first = this[offset]; const last = this[offset + 1]; if (first === undefined || last === undefined) boundsError(offset, this.length - 2); return first * 2 ** 8 + last; }
BE 指的是大端字节序, LE 指的是小端字节序, 使用何种方式都是可以的。小端字节序写用小端字节序读, 端字节序写就用大端字节序读, 读写规则不一致则会造成乱码, 更多可见 理解字节序。
对于 float32Array 的实现, 相当于直接使用了 float32Array
const float32Array = new Float32Array(1); const uInt8Float32Array = new Uint8Array(float32Array.buffer); function writeFloatForwards(val, offset = 0) { val = +val; checkBounds(this, offset, 3); float32Array[0] = val; this[offset++] = uInt8Float32Array[0]; this[offset++] = uInt8Float32Array[1]; this[offset++] = uInt8Float32Array[2]; this[offset++] = uInt8Float32Array[3]; return offset; } function readFloatForwards(offset = 0) { validateNumber(offset, 'offset'); const first = this[offset]; const last = this[offset + 3]; if (first === undefined || last === undefined) boundsError(offset, this.length - 4); uInt8Float32Array[0] = first; uInt8Float32Array[1] = this[++offset]; uInt8Float32Array[2] = this[++offset]; uInt8Float32Array[3] = last; return float32Array[0]; }
本文主要讲了 Node.js 中 Buffer 的实现, 相比直接使用 Uint8Array 等在性能安全以及使用上方便层度上做了一些改造, 有兴趣的同学可以扩展阅读 gRPC 中的 Protocol Buffers 的实现, 其遵循的是 Varints 编码 与 Zigzag 编码实现。
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of In-depth understanding of the buffer cache area in Nodejs. For more information, please follow other related articles on the PHP Chinese website!