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: use AliasedBuffer for TickInfo #17881

Closed
Closed
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
6 changes: 3 additions & 3 deletions lib/internal/process/next_tick.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function setupNextTick() {
] = process._setupNextTick(_tickCallback);

// *Must* match Environment::TickInfo::Fields in src/env.h.
const kScheduled = 0;
const kHasScheduled = 0;

const nextTickQueue = {
head: null,
Expand All @@ -40,7 +40,7 @@ function setupNextTick() {
this.tail.next = entry;
} else {
this.head = entry;
tickInfo[kScheduled] = 1;
tickInfo[kHasScheduled] = 1;
}
this.tail = entry;
},
Expand All @@ -50,7 +50,7 @@ function setupNextTick() {
const ret = this.head.data;
if (this.head === this.tail) {
this.head = this.tail = null;
tickInfo[kScheduled] = 0;
tickInfo[kHasScheduled] = 0;
} else {
this.head = this.head.next;
}
Expand Down
17 changes: 6 additions & 11 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,15 @@ inline bool Environment::AsyncCallbackScope::in_makecallback() const {
return env_->makecallback_cntr_ > 1;
}

inline Environment::TickInfo::TickInfo() {
for (int i = 0; i < kFieldsCount; ++i)
fields_[i] = 0;
}
inline Environment::TickInfo::TickInfo(v8::Isolate* isolate)
: fields_(isolate, kFieldsCount) {}

inline uint8_t* Environment::TickInfo::fields() {
inline AliasedBuffer<uint8_t, v8::Uint8Array>& Environment::TickInfo::fields() {
return fields_;
}

inline int Environment::TickInfo::fields_count() const {
return kFieldsCount;
}

inline uint8_t Environment::TickInfo::scheduled() const {
return fields_[kScheduled];
inline bool Environment::TickInfo::has_scheduled() const {
return fields_[kHasScheduled] == 1;
}

inline void Environment::AssignToContext(v8::Local<v8::Context> context,
Expand Down Expand Up @@ -269,6 +263,7 @@ inline Environment::Environment(IsolateData* isolate_data,
v8::Local<v8::Context> context)
: isolate_(context->GetIsolate()),
isolate_data_(isolate_data),
tick_info_(context->GetIsolate()),
timer_base_(uv_now(isolate_data->event_loop())),
using_domains_(false),
printed_error_(false),
Expand Down
11 changes: 5 additions & 6 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,20 +455,19 @@ class Environment {

class TickInfo {
public:
inline uint8_t* fields();
inline int fields_count() const;
inline uint8_t scheduled() const;
inline AliasedBuffer<uint8_t, v8::Uint8Array>& fields();
inline bool has_scheduled() const;

private:
friend class Environment; // So we can call the constructor.
inline TickInfo();
inline explicit TickInfo(v8::Isolate* isolate);

enum Fields {
kScheduled,
kHasScheduled,
kFieldsCount
};

uint8_t fields_[kFieldsCount];
AliasedBuffer<uint8_t, v8::Uint8Array> fields_;

DISALLOW_COPY_AND_ASSIGN(TickInfo);
};
Expand Down
14 changes: 3 additions & 11 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ using v8::SealHandleScope;
using v8::String;
using v8::TryCatch;
using v8::Uint32Array;
using v8::Uint8Array;
using v8::Undefined;
using v8::V8;
using v8::Value;
Expand Down Expand Up @@ -875,13 +874,6 @@ void SetupNextTick(const FunctionCallbackInfo<Value>& args) {
env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "_setupNextTick")).FromJust();

// Values use to cross communicate with processNextTick.
uint8_t* const fields = env->tick_info()->fields();
uint8_t const fields_count = env->tick_info()->fields_count();

Local<ArrayBuffer> array_buffer =
ArrayBuffer::New(env->isolate(), fields, sizeof(*fields) * fields_count);

v8::Local<v8::Function> run_microtasks_fn =
env->NewFunctionTemplate(RunMicrotasks)->GetFunction(env->context())
.ToLocalChecked();
Expand All @@ -890,7 +882,7 @@ void SetupNextTick(const FunctionCallbackInfo<Value>& args) {

Local<Array> ret = Array::New(env->isolate(), 2);
ret->Set(env->context(), 0,
Uint8Array::New(array_buffer, 0, fields_count)).FromJust();
env->tick_info()->fields().GetJSArray()).FromJust();
ret->Set(env->context(), 1, run_microtasks_fn).FromJust();

args.GetReturnValue().Set(ret);
Expand Down Expand Up @@ -1019,7 +1011,7 @@ void InternalCallbackScope::Close() {

Environment::TickInfo* tick_info = env_->tick_info();

if (tick_info->scheduled() == 0) {
if (!tick_info->has_scheduled()) {
env_->isolate()->RunMicrotasks();
}

Expand All @@ -1030,7 +1022,7 @@ void InternalCallbackScope::Close() {
CHECK_EQ(env_->trigger_async_id(), 0);
}

if (tick_info->scheduled() == 0) {
if (!tick_info->has_scheduled()) {
return;
}

Expand Down