Skip to content

Commit c735f73

Browse files
trevnorrisrvagg
authored andcommitted
buffer: implement Uint8Array backed Buffer
With V8 4.4 removing the external array data API currently used by Buffer, the new implementation uses the Uint8Array to back Buffer. Buffers now have a maximum size of Smi::kMaxLength, as defined by V8. Which is ~2 GB on 64 bit and ~1 GB on 32 bit. The flag --use-old-buffer allows using the old Buffer implementation. This flag will be removed once V8 4.4 has landed. The two JS Buffer implementations have been split into two files for simplicity. Use getter to return expected .parent/.offset values for backwards compatibility. PR-URL: #1825 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent bec924f commit c735f73

13 files changed

+2527
-1255
lines changed

lib/buffer.js

+4-1,145
Large diffs are not rendered by default.

lib/internal/buffer_new.js

+1,020
Large diffs are not rendered by default.

lib/internal/buffer_old.js

+1,140
Large diffs are not rendered by default.

node.gyp

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@
6969
'lib/v8.js',
7070
'lib/vm.js',
7171
'lib/zlib.js',
72-
7372
'lib/internal/child_process.js',
73+
'lib/internal/buffer_old.js',
74+
'lib/internal/buffer_new.js',
7475
'lib/internal/freelist.js',
7576
'lib/internal/smalloc.js',
7677
'lib/internal/socket_list.js',

src/env.h

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ namespace node {
231231
V(async_hooks_post_function, v8::Function) \
232232
V(binding_cache_object, v8::Object) \
233233
V(buffer_constructor_function, v8::Function) \
234+
V(buffer_prototype_object, v8::Object) \
234235
V(context, v8::Context) \
235236
V(domain_array, v8::Array) \
236237
V(fs_stats_constructor_function, v8::Function) \

src/node.cc

+25-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "node_http_parser.h"
66
#include "node_javascript.h"
77
#include "node_version.h"
8+
#include "node_internals.h"
89

910
#if defined HAVE_PERFCTR
1011
#include "node_counters.h"
@@ -147,6 +148,8 @@ static uv_async_t dispatch_debug_messages_async;
147148
static Isolate* node_isolate = nullptr;
148149
static v8::Platform* default_platform;
149150

151+
bool using_old_buffer = false;
152+
150153
class ArrayBufferAllocator : public ArrayBuffer::Allocator {
151154
public:
152155
// Impose an upper limit to avoid out of memory errors that bring down
@@ -166,23 +169,17 @@ ArrayBufferAllocator ArrayBufferAllocator::the_singleton;
166169

167170

168171
void* ArrayBufferAllocator::Allocate(size_t length) {
169-
if (length > kMaxLength)
170-
return nullptr;
171-
char* data = new char[length];
172-
memset(data, 0, length);
173-
return data;
172+
return calloc(length, 1);
174173
}
175174

176175

177176
void* ArrayBufferAllocator::AllocateUninitialized(size_t length) {
178-
if (length > kMaxLength)
179-
return nullptr;
180-
return new char[length];
177+
return malloc(length);
181178
}
182179

183180

184181
void ArrayBufferAllocator::Free(void* data, size_t length) {
185-
delete[] static_cast<char*>(data);
182+
free(data);
186183
}
187184

188185

@@ -2838,6 +2835,18 @@ void SetupProcessObject(Environment* env,
28382835
READONLY_PROPERTY(process, "traceDeprecation", True(env->isolate()));
28392836
}
28402837

2838+
// --trace-sync-io
2839+
if (trace_sync_io) {
2840+
READONLY_PROPERTY(process, "traceSyncIO", True(env->isolate()));
2841+
// Don't env->set_trace_sync_io(true) because it will be enabled
2842+
// after LoadEnvironment() has run.
2843+
}
2844+
2845+
// --use-old_buffer
2846+
if (using_old_buffer) {
2847+
READONLY_PROPERTY(process, "useOldBuffer", True(env->isolate()));
2848+
}
2849+
28412850
size_t exec_path_len = 2 * PATH_MAX;
28422851
char* exec_path = new char[exec_path_len];
28432852
Local<String> exec_path_value;
@@ -3062,13 +3071,14 @@ static void PrintHelp() {
30623071
" -r, --require module to preload (option can be repeated)\n"
30633072
" --no-deprecation silence deprecation warnings\n"
30643073
" --throw-deprecation throw an exception anytime a deprecated "
3065-
"function is used\n"
3074+
" function is used\n"
30663075
" --trace-deprecation show stack traces on deprecations\n"
30673076
" --trace-sync-io show stack trace when use of sync IO\n"
30683077
" is detected after the first tick\n"
30693078
" --track-heap-objects track heap object allocations for heap "
3070-
"snapshots\n"
3079+
" snapshots\n"
30713080
" --v8-options print v8 command line options\n"
3081+
" --use-old-buffer Revert to old Buffer implementation\n"
30723082
#if defined(NODE_HAVE_I18N_SUPPORT)
30733083
" --icu-data-dir=dir set ICU data load path to dir\n"
30743084
" (overrides NODE_ICU_DATA)\n"
@@ -3206,6 +3216,10 @@ static void ParseArgs(int* argc,
32063216
#endif
32073217
} else if (strcmp(arg, "--expose-internals") == 0 ||
32083218
strcmp(arg, "--expose_internals") == 0) {
3219+
} else if (strcmp(arg, "--use-old-buffer") == 0 ||
3220+
strcmp(arg, "--use_old_buffer") == 0) {
3221+
using_old_buffer = true;
3222+
32093223
// consumed in js
32103224
} else {
32113225
// V8 option. Pass through as-is.

0 commit comments

Comments
 (0)