Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer Use Uint8Array #1825

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,150 changes: 4 additions & 1,146 deletions lib/buffer.js

Large diffs are not rendered by default.

1,020 changes: 1,020 additions & 0 deletions lib/internal/buffer_new.js

Large diffs are not rendered by default.

1,149 changes: 1,149 additions & 0 deletions lib/internal/buffer_old.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
'lib/vm.js',
'lib/zlib.js',

'lib/internal/buffer_old.js',
'lib/internal/buffer_new.js',
'lib/internal/freelist.js',
'lib/internal/smalloc.js',
'lib/internal/repl.js',
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ namespace node {
V(async_hooks_post_function, v8::Function) \
V(binding_cache_object, v8::Object) \
V(buffer_constructor_function, v8::Function) \
V(buffer_prototype_object, v8::Object) \
V(context, v8::Context) \
V(domain_array, v8::Array) \
V(fs_stats_constructor_function, v8::Function) \
Expand Down
20 changes: 13 additions & 7 deletions src/js_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using v8::FunctionTemplate;
using v8::Handle;
using v8::HandleScope;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::Value;

Expand Down Expand Up @@ -88,8 +89,11 @@ int JSStream::DoWrite(WriteWrap* w,
HandleScope scope(env()->isolate());

Local<Array> bufs_arr = Array::New(env()->isolate(), count);
for (size_t i = 0; i < count; i++)
bufs_arr->Set(i, Buffer::New(env(), bufs[i].base, bufs[i].len));
Local<Object> buf;
for (size_t i = 0; i < count; i++) {
buf = Buffer::New(env(), bufs[i].base, bufs[i].len).ToLocalChecked();
bufs_arr->Set(i, buf);
}

Local<Value> argv[] = {
w->object(),
Expand Down Expand Up @@ -134,11 +138,13 @@ void JSStream::DoAlloc(const FunctionCallbackInfo<Value>& args) {

uv_buf_t buf;
wrap->OnAlloc(args[0]->Int32Value(), &buf);
args.GetReturnValue().Set(Buffer::New(wrap->env(),
buf.base,
buf.len,
FreeCallback,
nullptr));
Local<Object> vbuf = Buffer::New(
wrap->env(),
buf.base,
buf.len,
FreeCallback,
nullptr).ToLocalChecked();
return args.GetReturnValue().Set(vbuf);
}


Expand Down
25 changes: 16 additions & 9 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "node_http_parser.h"
#include "node_javascript.h"
#include "node_version.h"
#include "node_internals.h"

#if defined HAVE_PERFCTR
#include "node_counters.h"
Expand Down Expand Up @@ -146,6 +147,8 @@ static uv_async_t dispatch_debug_messages_async;
static Isolate* node_isolate = nullptr;
static v8::Platform* default_platform;

bool using_old_buffer = false;

class ArrayBufferAllocator : public ArrayBuffer::Allocator {
public:
// Impose an upper limit to avoid out of memory errors that bring down
Expand All @@ -165,23 +168,17 @@ ArrayBufferAllocator ArrayBufferAllocator::the_singleton;


void* ArrayBufferAllocator::Allocate(size_t length) {
if (length > kMaxLength)
return nullptr;
char* data = new char[length];
memset(data, 0, length);
return data;
return calloc(length, 1);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest replacing this with:

void* ArrayBufferAllocator::Allocate(size_t length) {
  return calloc(length, 1);
}

calloc() returns zeroed memory. For larger allocations, libc can then just mmap the pages (newly mapped pages are all zero) without us zeroing it again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do. Didn't know of there was some reason unknown to me for doing it the current way (which is the same way it's done in d8).



void* ArrayBufferAllocator::AllocateUninitialized(size_t length) {
if (length > kMaxLength)
return nullptr;
return new char[length];
return malloc(length);
}


void ArrayBufferAllocator::Free(void* data, size_t length) {
delete[] static_cast<char*>(data);
free(data);
}


Expand Down Expand Up @@ -2844,6 +2841,11 @@ void SetupProcessObject(Environment* env,
// after LoadEnvironment() has run.
}

// --use-old_buffer
if (using_old_buffer) {
READONLY_PROPERTY(process, "useOldBuffer", True(env->isolate()));
}

size_t exec_path_len = 2 * PATH_MAX;
char* exec_path = new char[exec_path_len];
Local<String> exec_path_value;
Expand Down Expand Up @@ -3072,6 +3074,7 @@ static void PrintHelp() {
" --trace-deprecation show stack traces on deprecations\n"
" --trace-sync-io show stack trace when use of sync IO\n"
" is detected after the first tick\n"
" --use-old-buffer Revert to old Buffer implementation\n"
" --v8-options print v8 command line options\n"
#if defined(NODE_HAVE_I18N_SUPPORT)
" --icu-data-dir=dir set ICU data load path to dir\n"
Expand Down Expand Up @@ -3208,6 +3211,10 @@ static void ParseArgs(int* argc,
#endif
} else if (strcmp(arg, "--expose-internals") == 0 ||
strcmp(arg, "--expose_internals") == 0) {
} else if (strcmp(arg, "--use-old-buffer") == 0 ||
strcmp(arg, "--use_old_buffer") == 0) {
using_old_buffer = true;

// consumed in js
} else {
// V8 option. Pass through as-is.
Expand Down
Loading