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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/node_buffer.cc
Expand Up @@ -244,7 +244,20 @@ 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, and somebody
// should figure out if this is actually a reasonable contract to uphold
// long-term.
//
// See: https://github.com/nodejs/node/issues/44554
return static_cast<char*>(ui->Buffer()->GetBackingStore()->Data()) +

ui->ByteOffset();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return static_cast<char*>(ui->Buffer()->GetBackingStore()->Data()) +
ui->ByteOffset();
return static_cast<char*>(ui->Buffer()->GetBackingStore()->Data()) +
ui->ByteOffset();

(Not sure what indentation will make format-cpp happy, though.)

}


Expand Down