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: revert GetBackingStore optimization in API #44579

Open
wants to merge 2 commits into
base: v18.x-staging
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,18 @@ bool HasInstance(Local<Object> obj) {
char* Data(Local<Value> val) {
CHECK(val->IsArrayBufferView());
Local<ArrayBufferView> ui = val.As<ArrayBufferView>();
return static_cast<char*>(ui->Buffer()->Data()) + ui->ByteOffset();
// GetBackingStore() is slow, and this would be faster if we just did
// ui->Buffer()->Data(). However these two are not equivalent in the case of
// zero-length buffers: the former returns a "valid" address, while the
// latter returns `NULL`. At least one library in the ecosystem (see the
// referenced issue) abuses zero-length buffers to wrap arbitrary pointers,
// which is broken by this difference. It is unfortunate that every library
// needs to take a performance hit because of this edge-case, so this change
// is only being backported to older Node.js releases.
//
// See: https://github.com/nodejs/node/issues/44554
return static_cast<char*>(ui->Buffer()->GetBackingStore()->Data()) +
ui->ByteOffset();
}


Expand Down