Skip to content
Open
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
21 changes: 17 additions & 4 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ using v8::Undefined;
using v8::Value;
using worker::Worker;

constexpr size_t kManagedBufferCacheSize = 64 * 1024;

int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64;
void* const ContextEmbedderTag::kNodeContextTagPtr = const_cast<void*>(
static_cast<const void*>(&ContextEmbedderTag::kNodeContextTag));
Expand Down Expand Up @@ -746,10 +748,16 @@ void Environment::add_refs(int64_t diff) {
}

uv_buf_t Environment::allocate_managed_buffer(const size_t suggested_size) {
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
isolate(),
suggested_size,
BackingStoreInitializationMode::kUninitialized);
std::unique_ptr<BackingStore> bs;
if (suggested_size == kManagedBufferCacheSize &&
managed_buffer_cache_ != nullptr) {
bs = std::move(managed_buffer_cache_);
} else {
bs = ArrayBuffer::NewBackingStore(
isolate(),
suggested_size,
BackingStoreInitializationMode::kUninitialized);
}
uv_buf_t buf = uv_buf_init(static_cast<char*>(bs->Data()), bs->ByteLength());
released_allocated_buffers_.emplace(buf.base, std::move(bs));
return buf;
Expand All @@ -767,6 +775,11 @@ std::unique_ptr<BackingStore> Environment::release_managed_buffer(
return bs;
}

void Environment::recycle_managed_buffer(std::unique_ptr<BackingStore> bs) {
if (bs != nullptr && bs->ByteLength() == kManagedBufferCacheSize)
managed_buffer_cache_ = std::move(bs);
}

std::string Environment::GetExecPath(const std::vector<std::string>& argv) {
char exec_path_buf[2 * PATH_MAX];
size_t exec_path_len = sizeof(exec_path_buf);
Expand Down
6 changes: 6 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,9 @@ class Environment final : public MemoryRetainer {

uv_buf_t allocate_managed_buffer(const size_t suggested_size);
std::unique_ptr<v8::BackingStore> release_managed_buffer(const uv_buf_t& buf);
// Only buffers that were not exposed externally may be recycled.
void recycle_managed_buffer(
std::unique_ptr<v8::BackingStore> backing_store);

void AddUnmanagedFd(int fd);
void RemoveUnmanagedFd(int fd);
Expand Down Expand Up @@ -1257,6 +1260,9 @@ class Environment final : public MemoryRetainer {
std::unordered_map<char*, std::unique_ptr<v8::BackingStore>>
released_allocated_buffers_;

// Retains at most one unexposed read buffer for reuse.
std::unique_ptr<v8::BackingStore> managed_buffer_cache_;

v8::CpuProfiler* cpu_profiler_ = nullptr;
std::vector<v8::ProfilerId> pending_profiles_;
};
Expand Down
2 changes: 2 additions & 0 deletions src/stream_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ void EmitToJSStreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
std::unique_ptr<BackingStore> bs = env->release_managed_buffer(buf_);

if (nread <= 0) {
env->recycle_managed_buffer(std::move(bs));
if (nread < 0)
stream->CallJSOnreadMethod(nread, Local<ArrayBuffer>());
return;
Expand All @@ -708,6 +709,7 @@ void EmitToJSStreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
bs = ArrayBuffer::NewBackingStore(
isolate, nread, BackingStoreInitializationMode::kUninitialized);
memcpy(bs->Data(), old_bs->Data(), nread);
env->recycle_managed_buffer(std::move(old_bs));
}

stream->CallJSOnreadMethod(nread, ArrayBuffer::New(isolate, std::move(bs)));
Expand Down
Loading