Skip to content

Commit 65cd82a

Browse files
committed
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 b12d267 commit 65cd82a

File tree

13 files changed

+2527
-1252
lines changed

13 files changed

+2527
-1252
lines changed

lib/buffer.js

Lines changed: 4 additions & 1146 deletions
Large diffs are not rendered by default.

lib/internal/buffer_new.js

Lines changed: 1020 additions & 0 deletions
Large diffs are not rendered by default.

lib/internal/buffer_old.js

Lines changed: 1149 additions & 0 deletions
Large diffs are not rendered by default.

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
'lib/vm.js',
7171
'lib/zlib.js',
7272

73+
'lib/internal/buffer_old.js',
74+
'lib/internal/buffer_new.js',
7375
'lib/internal/freelist.js',
7476
'lib/internal/smalloc.js',
7577
'lib/internal/repl.js',

src/env.h

Lines changed: 1 addition & 0 deletions
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

Lines changed: 16 additions & 9 deletions
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"
@@ -146,6 +147,8 @@ static uv_async_t dispatch_debug_messages_async;
146147
static Isolate* node_isolate = nullptr;
147148
static v8::Platform* default_platform;
148149

150+
bool using_old_buffer = false;
151+
149152
class ArrayBufferAllocator : public ArrayBuffer::Allocator {
150153
public:
151154
// Impose an upper limit to avoid out of memory errors that bring down
@@ -165,23 +168,17 @@ ArrayBufferAllocator ArrayBufferAllocator::the_singleton;
165168

166169

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

175174

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

182179

183180
void ArrayBufferAllocator::Free(void* data, size_t length) {
184-
delete[] static_cast<char*>(data);
181+
free(data);
185182
}
186183

187184

@@ -2844,6 +2841,11 @@ void SetupProcessObject(Environment* env,
28442841
// after LoadEnvironment() has run.
28452842
}
28462843

2844+
// --use-old_buffer
2845+
if (using_old_buffer) {
2846+
READONLY_PROPERTY(process, "useOldBuffer", True(env->isolate()));
2847+
}
2848+
28472849
size_t exec_path_len = 2 * PATH_MAX;
28482850
char* exec_path = new char[exec_path_len];
28492851
Local<String> exec_path_value;
@@ -3072,6 +3074,7 @@ static void PrintHelp() {
30723074
" --trace-deprecation show stack traces on deprecations\n"
30733075
" --trace-sync-io show stack trace when use of sync IO\n"
30743076
" is detected after the first tick\n"
3077+
" --use-old-buffer Revert to old Buffer implementation\n"
30753078
" --v8-options print v8 command line options\n"
30763079
#if defined(NODE_HAVE_I18N_SUPPORT)
30773080
" --icu-data-dir=dir set ICU data load path to dir\n"
@@ -3208,6 +3211,10 @@ static void ParseArgs(int* argc,
32083211
#endif
32093212
} else if (strcmp(arg, "--expose-internals") == 0 ||
32103213
strcmp(arg, "--expose_internals") == 0) {
3214+
} else if (strcmp(arg, "--use-old-buffer") == 0 ||
3215+
strcmp(arg, "--use_old_buffer") == 0) {
3216+
using_old_buffer = true;
3217+
32113218
// consumed in js
32123219
} else {
32133220
// V8 option. Pass through as-is.

0 commit comments

Comments
 (0)