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

src: refactor grow_async_ids_stack #23808

Merged
merged 2 commits into from
Oct 24, 2018
Merged
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
48 changes: 43 additions & 5 deletions src/aliased_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "v8.h"
#include "util-inl.h"
#include "util.h"

namespace node {

Expand All @@ -22,7 +22,9 @@ namespace node {
* The encapsulation herein provides a placeholder where such writes can be
* observed. Any notification APIs will be left as a future exercise.
*/
template <class NativeT, class V8T>
template <class NativeT, class V8T,
// SFINAE NativeT to be scalar
typename = std::enable_if_t<std::is_scalar<NativeT>::value>>
class AliasedBuffer {
public:
AliasedBuffer(v8::Isolate* isolate, const size_t count)
Expand All @@ -33,14 +35,14 @@ class AliasedBuffer {
CHECK_GT(count, 0);
const v8::HandleScope handle_scope(isolate_);

const size_t sizeInBytes = sizeof(NativeT) * count;
const size_t size_in_bytes = sizeof(NativeT) * count;

// allocate native buffer
buffer_ = Calloc<NativeT>(count);

// allocate v8 ArrayBuffer
v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(
isolate_, buffer_, sizeInBytes);
isolate_, buffer_, size_in_bytes);

// allocate v8 TypedArray
v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, count);
Expand All @@ -55,6 +57,7 @@ class AliasedBuffer {
*
* Note that byte_offset must by aligned by sizeof(NativeT).
*/
// TODO(refack): refactor into a non-owning `AliasedBufferView`
AliasedBuffer(v8::Isolate* isolate,
const size_t byte_offset,
const size_t count,
Expand Down Expand Up @@ -96,7 +99,7 @@ class AliasedBuffer {
js_array_.Reset();
}

AliasedBuffer& operator=(AliasedBuffer&& that) {
AliasedBuffer& operator=(AliasedBuffer&& that) noexcept {
this->~AliasedBuffer();
isolate_ = that.isolate_;
count_ = that.count_;
Expand Down Expand Up @@ -226,6 +229,41 @@ class AliasedBuffer {
return count_;
}

// Should only be used to extend the array.
// Should only be used on an owning array, not one created as a sub array of
// an owning `AliasedBuffer`.
void reserve(size_t new_capacity) {
#if defined(DEBUG) && DEBUG
CHECK_GE(new_capacity, count_);
refack marked this conversation as resolved.
Show resolved Hide resolved
CHECK_EQ(byte_offset_, 0);
refack marked this conversation as resolved.
Show resolved Hide resolved
CHECK(free_buffer_);
#endif
const v8::HandleScope handle_scope(isolate_);

const size_t old_size_in_bytes = sizeof(NativeT) * count_;
const size_t new_size_in_bytes = sizeof(NativeT) * new_capacity;

// allocate new native buffer
NativeT* new_buffer = Calloc<NativeT>(new_capacity);
// copy old content
memcpy(new_buffer, buffer_, old_size_in_bytes);

// allocate v8 new ArrayBuffer
v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(
isolate_, new_buffer, new_size_in_bytes);

// allocate v8 TypedArray
v8::Local<V8T> js_array = V8T::New(ab, byte_offset_, new_capacity);

// move over old v8 TypedArray
js_array_ = std::move(v8::Global<V8T>(isolate_, js_array));

// Free old buffer and set new values
free(buffer_);
buffer_ = new_buffer;
count_ = new_capacity;
}

private:
v8::Isolate* isolate_;
size_t count_;
Expand Down
9 changes: 1 addition & 8 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -826,14 +826,7 @@ void Environment::CollectUVExceptionInfo(v8::Local<v8::Value> object,


void Environment::AsyncHooks::grow_async_ids_stack() {
const uint32_t old_capacity = async_ids_stack_.Length() / 2;
const uint32_t new_capacity = old_capacity * 1.5;
AliasedBuffer<double, v8::Float64Array> new_buffer(
env()->isolate(), new_capacity * 2);

for (uint32_t i = 0; i < old_capacity * 2; ++i)
new_buffer[i] = async_ids_stack_[i];
async_ids_stack_ = std::move(new_buffer);
async_ids_stack_.reserve(async_ids_stack_.Length() * 3);

env()->async_hooks_binding()->Set(
env()->context(),
Expand Down
1 change: 1 addition & 0 deletions src/inspector/main_thread_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <functional>
#include <unicode/unistr.h>
#include "util-inl.h"

namespace node {
namespace inspector {
Expand Down
4 changes: 2 additions & 2 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ ContainerOfHelper<Inner, Outer>::operator TypeName*() const {
}

template <typename Inner, typename Outer>
inline ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
Inner* pointer) {
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
Inner* pointer) {
return ContainerOfHelper<Inner, Outer>(field, pointer);
}

Expand Down
4 changes: 2 additions & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ class ContainerOfHelper {
// Calculate the address of the outer (i.e. embedding) struct from
// the interior pointer to a data member.
template <typename Inner, typename Outer>
inline ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
Inner* pointer);
constexpr ContainerOfHelper<Inner, Outer> ContainerOf(Inner Outer::*field,
Inner* pointer);

// If persistent.IsWeak() == false, then do not call persistent.Reset()
// while the returned Local<T> is still in scope, it will destroy the
Expand Down