Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a667296
stream: preserve mutable chunks in Web Stream adapters
seungwoo505 Jul 18, 2026
16b663d
test: cover mutable Web Stream adapter writes
seungwoo505 Jul 18, 2026
531ada5
stream: simplify mutable chunk fallback
seungwoo505 Sep 28, 2026
72a0cbb
test: refine mutable Web Stream adapter coverage
seungwoo505 Sep 28, 2026
7e150d6
stream: use Float16Array from primordials
seungwoo505 Sep 29, 2026
09d8636
stream: optimize private BufferSource copies
seungwoo505 Oct 1, 2026
13e12b7
stream: optimize Web Stream write completion
seungwoo505 Oct 1, 2026
7ad3b90
stream: preserve native HTTP write validation
seungwoo505 Oct 1, 2026
ac63d9d
stream: preserve errors on premature writable close
seungwoo505 Oct 1, 2026
50ed406
test: deduplicate Web Stream adapter regressions
seungwoo505 Oct 1, 2026
7796966
doc: clarify mutable Web Stream chunk reuse
seungwoo505 Oct 1, 2026
1fa14c0
benchmark: add async writev adapter benchmark
seungwoo505 Oct 2, 2026
ca58486
stream: preserve Writable.toWeb batching and context
seungwoo505 Oct 5, 2026
4be4f8e
test: cover writable-to-web batching and context
seungwoo505 Oct 5, 2026
3830906
stream: update writable-to-web context handling
seungwoo505 Oct 6, 2026
21f40fc
buffer: pool independent backing stores for byte copies
seungwoo505 Nov 20, 2026
12377e8
stream: preserve independent chunks in Writable.toWeb
seungwoo505 Nov 20, 2026
4193b94
stream: streamline writable stream request queuing
seungwoo505 Nov 20, 2026
cd2096b
stream: preserve copy buffers for shared writes
seungwoo505 Nov 21, 2026
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
24 changes: 24 additions & 0 deletions benchmark/webstreams/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const bench = common.createBenchmark(main, {
'readable-to-web',
'readable-from-web',
'writable-to-web',
'writable-to-web-async-writev',
'writable-from-web',
],
});
Expand Down Expand Up @@ -68,6 +69,26 @@ async function writableToWeb(n) {
bench.end(n);
}

async function writableToWebAsyncWritev(n) {
const chunk = Buffer.alloc(1024);
const streamWritable = new Writable({
highWaterMark: 64 * 1024,
// Defer completion so subsequent writes can be batched in writev().
write(chunk, encoding, callback) {
setImmediate(callback);
},
writev(chunks, callback) {
setImmediate(callback);
},
});
const writer = Writable.toWeb(streamWritable).getWriter();
bench.start();
for (let i = 0; i < n; i++)
await writer.write(chunk);
await writer.close();
bench.end(n);
}

function writableFromWeb(n) {
const chunk = Buffer.alloc(1024);
const writableStream = new WritableStream({
Expand Down Expand Up @@ -99,6 +120,9 @@ function main({ n, kind }) {
case 'writable-to-web':
writableToWeb(n);
break;
case 'writable-to-web-async-writev':
writableToWebAsyncWritev(n);
break;
case 'writable-from-web':
writableFromWeb(n);
break;
Expand Down
1 change: 1 addition & 0 deletions deps/v8/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3839,6 +3839,7 @@ v8_cluster_source_set("v8_initializers") {
"src/builtins/builtins-string-tsa.cc",
"src/builtins/builtins-typed-array-gen.cc",
"src/builtins/builtins-typed-array-gen.h",
"src/builtins/small-byte-copy-gen.h",
"src/builtins/builtins-utils-gen.h",
"src/builtins/growable-fixed-array-gen.cc",
"src/builtins/growable-fixed-array-gen.h",
Expand Down
129 changes: 125 additions & 4 deletions deps/v8/src/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include "src/api/api-arguments.h"
#include "src/api/api-inl.h"
#include "src/api/api-natives.h"
#include "src/api/node-copy-batch.h"
#include "src/api/node-inline-backing-store.h"
#include "src/base/bit-field.h"
#include "src/base/hashing.h"
#include "src/base/logging.h"
Expand Down Expand Up @@ -76,9 +78,12 @@
#include "src/handles/persistent-handles.h"
#include "src/handles/shared-object-conveyor-handles.h"
#include "src/handles/traced-handles-inl.h"
#include "src/heap/array-buffer-sweeper.h"
#include "src/heap/heap-allocator-inl.h"
#include "src/heap/heap-inl.h"
#include "src/heap/heap-layout-inl.h"
#include "src/heap/heap-write-barrier.h"
#include "src/heap/incremental-marking.h"
#include "src/heap/safepoint.h"
#include "src/heap/visit-object.h"
#include "src/init/bootstrapper.h"
Expand Down Expand Up @@ -9273,10 +9278,127 @@ size_t v8::ArrayBufferBase::CopyArrayBufferBytes(size_t source_start,
namespace {
std::shared_ptr<i::BackingStore> ToInternal(
std::shared_ptr<i::BackingStoreBase> backing_store) {
return std::static_pointer_cast<i::BackingStore>(backing_store);
return std::static_pointer_cast<i::BackingStore>(std::move(backing_store));
}

bool TryAllocateNodeCopyShells(i::Isolate* isolate,
i::DirectHandle<i::FixedArray> wrappers,
unsigned count) {
#if defined(V8_NODE_CAN_INLINE_BACKING_STORE) && !V8_VERIFY_WRITE_BARRIERS && \
!defined(V8_USE_MEMORY_SANITIZER) && !defined(THREAD_SANITIZER) && \
!defined(V8_IS_TSAN) && !defined(V8_COMPRESS_POINTERS)
using Buffer = i::JSArrayBuffer;
constexpr int size = Buffer::kSizeWithEmbedderFields;
static_assert(size > 0 && size == ALIGN_TO_ALLOCATION_ALIGNMENT(size));
static_assert(64 * size <= i::kMaxRegularHeapObjectSize);

auto* heap = isolate->heap();
if (!heap->CanSafepoint()) return false;
heap->main_thread_local_heap()->Safepoint();
if (!i::v8_flags.enable_allocation_folding || isolate->serializer_enabled() ||
!heap->new_space() || heap->IsInGC() || heap->force_oom() ||
!heap->IsInlineAllocationEnabled() ||
heap->has_heap_object_allocation_tracker() ||
heap->incremental_marking()->IsMarking() ||
i::v8_flags.single_generation || i::v8_flags.sticky_mark_bits ||
i::v8_flags.enable_unconditional_write_barriers) {
return false;
}
#ifdef V8_ENABLE_ALLOCATION_TIMEOUT
if (heap->allocator()->get_allocation_timeout_for_testing().has_value()) {
return false;
}
#endif
const auto map =
isolate->raw_native_context()->array_buffer_fun()->initial_map();
if (map->instance_type() != i::JS_ARRAY_BUFFER_TYPE ||
map->instance_size() != size || map->GetInObjectProperties() != 0 ||
map->IsInobjectSlackTrackingInProgress() || map->is_deprecated()) {
return false;
}

const unsigned bytes = count * size;
auto* lab = heap->allocator()->new_space_allocator();
if (!i::HeapLayout::InYoungGeneration(*wrappers) ||
!lab->allocation_info().CanIncrementTop(bytes)) {
return false;
}

CHECK(i::AllowHeapAllocationInRelease::IsAllowed());
i::DisallowGarbageCollectionInRelease no_gc;
auto allocation = lab->AllocateRaw(i::SafeHeapObjectSize(bytes),
i::kTaggedAligned,
i::AllocationOrigin::kRuntime,
i::AllocationHint());
CHECK(!allocation.IsFailure());
const i::Address first = allocation.ToAddress();
const auto raw = i::HeapObject::FromAddress(first);
raw->set_map_after_allocation(isolate, map, i::SKIP_WRITE_BARRIER);
auto shell = i::Cast<Buffer>(raw);
shell->set_raw_properties_or_hash(
i::ReadOnlyRoots(isolate).empty_fixed_array(), i::SKIP_WRITE_BARRIER);
shell->initialize_elements();
shell->Setup(i::SharedFlag{false}, i::ResizableFlag{false}, nullptr, isolate);

for (unsigned filled = 1; filled < count;) {
const unsigned copies = std::min(filled, count - filled);
i::Heap::CopyBlock(first + filled * size, first,
i::SafeHeapObjectSize(copies * size));
filled += copies;
}
for (unsigned i = 0; i < count; ++i) {
wrappers->set(i, i::HeapObject::FromAddress(first + i * size),
i::SKIP_WRITE_BARRIER);
}
return true;
#else
return false;
#endif
}

} // namespace

Local<v8::Array> internal::NewNodeCopyBatch(
v8::Isolate* isolate, std::unique_ptr<v8::BackingStore>* stores,
unsigned count, size_t size,
const std::shared_ptr<NodeCopyStoreRecycler>& recycler) {
CHECK_GE(count, 2u);
CHECK_LE(count, 64u);
CHECK_GT(size, 64u);
CHECK_LE(size, 8192u);
CHECK_LE(size, 65536 / count);
auto* engine = reinterpret_cast<i::Isolate*>(isolate);
ApiRuntimeCallStatsScope rcs_scope(engine, RCCId::kAPI_ArrayBuffer_New);
EnterV8NoScriptNoExceptionScope api_scope(engine);
i::Factory* factory = engine->factory();
i::DirectHandle<i::FixedArray> wrappers = factory->NewFixedArray(count);
for (unsigned i = 0; i < count; ++i) {
CHECK_NOT_NULL(stores[i].get());
CHECK_EQ(stores[i]->ByteLength(), size);
}
if (!TryAllocateNodeCopyShells(engine, wrappers, count)) {
for (unsigned i = 0; i < count; ++i) {
auto shell = factory->NewJSArrayBuffer(nullptr);
wrappers->set(i, *shell);
}
}
unsigned installed = 0;
if (NodeInlineBackingStore::IsSupported()) {
installed = engine->heap()->array_buffer_sweeper()->AttachPreparedNodeCopies(
wrappers, stores, count, size, recycler);
}
for (unsigned i = installed; i < count; ++i) {
auto return_store = [recycler](v8::BackingStore* value) {
recycler->Return(std::unique_ptr<v8::BackingStore>(value));
};
std::shared_ptr<v8::BackingStore> owner(stores[i].release(), return_store);
auto buffer = v8::ArrayBuffer::New(isolate, std::move(owner));
wrappers->set(i, *Utils::OpenDirectHandle(*buffer));
}
return Utils::ToLocal(factory->NewJSArrayWithElements(
wrappers, i::PACKED_ELEMENTS, count));
}

Maybe<bool> v8::ArrayBuffer::Detach(v8::Local<v8::Value> key) {
auto obj = Utils::OpenDirectHandle(this);
i::Isolate* i_isolate = i::Isolate::Current();
Expand Down Expand Up @@ -9386,11 +9508,10 @@ Local<ArrayBuffer> v8::ArrayBuffer::New(
Utils::ApiCheck(
!i_backing_store->is_shared(), "v8_ArrayBuffer_New",
"Cannot construct ArrayBuffer with a BackingStore of SharedArrayBuffer");
const i::BackingStore* store = i_backing_store.get();
i::DirectHandle<i::JSArrayBuffer> obj =
i_isolate->factory()->NewJSArrayBuffer(std::move(i_backing_store));
if (obj->backing_store() &&
static_cast<i::BackingStore*>(obj->GetBackingStore().get())
->is_immutable()) {
if (obj->backing_store() && store->is_immutable()) {
obj->MakeImmutable(i_isolate);
}
return Utils::ToLocalUnshared(obj);
Expand Down
36 changes: 36 additions & 0 deletions deps/v8/src/api/node-copy-batch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2026 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_SRC_API_NODE_COPY_BATCH_H_
#define V8_SRC_API_NODE_COPY_BATCH_H_

#include <cstddef>
#include <memory>

#include "../../include/v8-local-handle.h"
#include "../../include/v8config.h"

namespace v8 {
class Array;
class BackingStore;
class Isolate;

namespace internal {
class NodeCopyStoreRecycler {
public:
virtual ~NodeCopyStoreRecycler() = default;
virtual void Return(std::unique_ptr<v8::BackingStore> store) = 0;
};

#if V8_HAS_ATTRIBUTE_VISIBILITY && !defined(V8_OS_WIN)
__attribute__((visibility("hidden")))
#endif
Local<v8::Array> NewNodeCopyBatch(
v8::Isolate* isolate, std::unique_ptr<v8::BackingStore>* stores,
unsigned count, size_t size,
const std::shared_ptr<NodeCopyStoreRecycler>& recycler);
} // namespace internal
} // namespace v8

#endif // V8_SRC_API_NODE_COPY_BATCH_H_
42 changes: 42 additions & 0 deletions deps/v8/src/api/node-inline-backing-store.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2026 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_API_NODE_INLINE_BACKING_STORE_H_
#define V8_API_NODE_INLINE_BACKING_STORE_H_

#include "../../include/v8-array-buffer.h"

#if defined(__clang__)
#if __has_feature(address_sanitizer) || __has_feature(hwaddress_sanitizer)
#define V8_NODE_INSTRUMENTED_NEW
#endif
#endif
#if !defined(DEBUG) && !defined(_DEBUG) && !defined(V8_ENABLE_SANDBOX) && \
!defined(V8_USE_ADDRESS_SANITIZER) && !defined(V8_USE_HWADDRESS_SANITIZER) && \
!defined(__SANITIZE_ADDRESS__) && !defined(LEAK_SANITIZER) && \
!defined(V8_NODE_INSTRUMENTED_NEW) && defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
#define V8_NODE_CAN_INLINE_BACKING_STORE
#endif
#undef V8_NODE_INSTRUMENTED_NEW

namespace v8::internal {

class
#if V8_HAS_ATTRIBUTE_VISIBILITY && !defined(V8_OS_WIN)
__attribute__((visibility("hidden")))
#endif
NodeInlineBackingStore {
public:
using Account =
void (*)(v8::ArrayBuffer::Allocator*, void*, size_t, bool) noexcept;

static bool IsSupported();
static bool NewMany(v8::Isolate* isolate, size_t length, Account account,
v8::BackingStore::DeleterCallback raw_release,
std::unique_ptr<v8::BackingStore>* slots, size_t count);
};

} // namespace v8::internal

#endif // V8_API_NODE_INLINE_BACKING_STORE_H_
Loading
Loading