From 41c7e96f1a81ea416ebb3ba45f2815e0202d6b75 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Sun, 28 Apr 2019 21:31:10 +0200 Subject: Refactor zero-copy buffers for performance and to prevent memory leaks * In order to prevent ArrayBuffers from getting garbage collected by V8, we used to store a v8::Persistent in a map. This patch introduces a custom ArrayBuffer allocator which doesn't use Persistent handles, but instead stores a pointer to the actual ArrayBuffer data alongside with a reference count. Since creating Persistent handles has quite a bit of overhead, this change significantly increases performance. Various HTTP server benchmarks report about 5-10% more requests per second than before. * Previously the Persistent handle that prevented garbage collection had to be released manually, and this wasn't always done, which was causing memory leaks. This has been resolved by introducing a new `PinnedBuf` type in both Rust and C++ that automatically re-enables garbage collection when it goes out of scope. * Zero-copy buffers are now correctly wrapped in an Option if there is a possibility that they're not present. This clears up a correctness issue where we were creating zero-length slices from a null pointer, which is against the rules. --- core/libdeno/modules_test.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'core/libdeno/modules_test.cc') diff --git a/core/libdeno/modules_test.cc b/core/libdeno/modules_test.cc index 0eaa9e8eb..f3a97396f 100644 --- a/core/libdeno/modules_test.cc +++ b/core/libdeno/modules_test.cc @@ -1,13 +1,14 @@ -// Copyright 2018 the Deno authors. All rights reserved. MIT license. +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. #include "test.h" static int exec_count = 0; -void recv_cb(void* user_data, deno_buf buf, deno_buf zero_copy_buf) { +void recv_cb(void* user_data, deno_buf buf, deno_pinned_buf zero_copy_buf) { // We use this to check that scripts have executed. EXPECT_EQ(1u, buf.data_len); EXPECT_EQ(buf.data_ptr[0], 4); - EXPECT_EQ(zero_copy_buf.zero_copy_id, 0u); EXPECT_EQ(zero_copy_buf.data_ptr, nullptr); + EXPECT_EQ(zero_copy_buf.data_len, 0u); + EXPECT_EQ(zero_copy_buf.pin, nullptr); exec_count++; } -- cgit v1.2.3