Skip to content

Commit 00704f5

Browse files
marjakhV8 LUCI CQ
authored andcommitted
[api] Add more efficient API for accesssing ArrayBuffer raw data
Raw data access is already possible via GetBackingStore()->GetData(). This API exposes a more efficient way for accessing JSArrayBuffer::backing_store (which, despite the confusing name, is no the BackingStore but its raw data pointer). Bug: v8:10343 Change-Id: I695cea91e2c3de75ce6c86bac6e413ce6617958b Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3764341 Reviewed-by: Camillo Bruni <[email protected]> Commit-Queue: Marja Hölttä <[email protected]> Cr-Commit-Position: refs/heads/main@{#81745}
1 parent dc0ef86 commit 00704f5

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

include/v8-array-buffer.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ class V8_EXPORT ArrayBuffer : public Object {
256256
*/
257257
std::shared_ptr<BackingStore> GetBackingStore();
258258

259+
/**
260+
* More efficient shortcut for GetBackingStore()->Data(). The returned pointer
261+
* is valid as long as the ArrayBuffer is alive.
262+
*/
263+
void* Data() const;
264+
259265
V8_INLINE static ArrayBuffer* Cast(Value* value) {
260266
#ifdef V8_ENABLE_CHECKS
261267
CheckCast(value);
@@ -414,6 +420,12 @@ class V8_EXPORT SharedArrayBuffer : public Object {
414420
*/
415421
std::shared_ptr<BackingStore> GetBackingStore();
416422

423+
/**
424+
* More efficient shortcut for GetBackingStore()->Data(). The returned pointer
425+
* is valid as long as the ArrayBuffer is alive.
426+
*/
427+
void* Data() const;
428+
417429
V8_INLINE static SharedArrayBuffer* Cast(Value* value) {
418430
#ifdef V8_ENABLE_CHECKS
419431
CheckCast(value);

src/api/api.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4109,6 +4109,11 @@ std::shared_ptr<v8::BackingStore> v8::ArrayBuffer::GetBackingStore() {
41094109
return std::static_pointer_cast<v8::BackingStore>(bs_base);
41104110
}
41114111

4112+
void* v8::ArrayBuffer::Data() const {
4113+
i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
4114+
return self->backing_store();
4115+
}
4116+
41124117
std::shared_ptr<v8::BackingStore> v8::SharedArrayBuffer::GetBackingStore() {
41134118
i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
41144119
std::shared_ptr<i::BackingStore> backing_store = self->GetBackingStore();
@@ -4119,6 +4124,11 @@ std::shared_ptr<v8::BackingStore> v8::SharedArrayBuffer::GetBackingStore() {
41194124
return std::static_pointer_cast<v8::BackingStore>(bs_base);
41204125
}
41214126

4127+
void* v8::SharedArrayBuffer::Data() const {
4128+
i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
4129+
return self->backing_store();
4130+
}
4131+
41224132
void v8::ArrayBuffer::CheckCast(Value* that) {
41234133
i::Handle<i::Object> obj = Utils::OpenHandle(that);
41244134
Utils::ApiCheck(

test/cctest/test-api-array-buffer.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ THREADED_TEST(SkipArrayBufferBackingStoreDuringGC) {
366366

367367
// Should not move the pointer
368368
CHECK_EQ(ab->GetBackingStore()->Data(), store_ptr);
369+
CHECK_EQ(ab->Data(), store_ptr);
369370

370371
CcTest::array_buffer_allocator()->Free(buffer, 100);
371372
}
@@ -394,8 +395,8 @@ THREADED_TEST(SkipArrayBufferDuringScavenge) {
394395
CcTest::CollectGarbage(i::NEW_SPACE); // in survivor space now
395396
CcTest::CollectGarbage(i::NEW_SPACE); // in old gen now
396397

397-
// Use `ab` to silence compiler warning
398398
CHECK_EQ(ab->GetBackingStore()->Data(), store_ptr);
399+
CHECK_EQ(ab->Data(), store_ptr);
399400
}
400401

401402
THREADED_TEST(Regress1006600) {
@@ -418,6 +419,7 @@ THREADED_TEST(ArrayBuffer_NewBackingStore) {
418419
CHECK(!backing_store->IsShared());
419420
Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, backing_store);
420421
CHECK_EQ(backing_store.get(), ab->GetBackingStore().get());
422+
CHECK_EQ(backing_store->Data(), ab->Data());
421423
}
422424

423425
THREADED_TEST(SharedArrayBuffer_NewBackingStore) {
@@ -430,6 +432,7 @@ THREADED_TEST(SharedArrayBuffer_NewBackingStore) {
430432
Local<v8::SharedArrayBuffer> ab =
431433
v8::SharedArrayBuffer::New(isolate, backing_store);
432434
CHECK_EQ(backing_store.get(), ab->GetBackingStore().get());
435+
CHECK_EQ(backing_store->Data(), ab->Data());
433436
}
434437

435438
static void* backing_store_custom_data = nullptr;

0 commit comments

Comments
 (0)