Skip to content

Commit 07c6fb9

Browse files
apapirovskiMylesBorins
authored andcommitted
src: use AliasedBuffer for TickInfo
Backport-PR-URL: #19006 PR-URL: #17881 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 8474f86 commit 07c6fb9

File tree

4 files changed

+17
-31
lines changed

4 files changed

+17
-31
lines changed

lib/internal/process/next_tick.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function setupNextTick() {
2929
] = process._setupNextTick(_tickCallback);
3030

3131
// *Must* match Environment::TickInfo::Fields in src/env.h.
32-
const kScheduled = 0;
32+
const kHasScheduled = 0;
3333

3434
const nextTickQueue = {
3535
head: null,
@@ -40,7 +40,7 @@ function setupNextTick() {
4040
this.tail.next = entry;
4141
} else {
4242
this.head = entry;
43-
tickInfo[kScheduled] = 1;
43+
tickInfo[kHasScheduled] = 1;
4444
}
4545
this.tail = entry;
4646
},
@@ -50,7 +50,7 @@ function setupNextTick() {
5050
const ret = this.head.data;
5151
if (this.head === this.tail) {
5252
this.head = this.tail = null;
53-
tickInfo[kScheduled] = 0;
53+
tickInfo[kHasScheduled] = 0;
5454
} else {
5555
this.head = this.head.next;
5656
}

src/env-inl.h

+6-11
Original file line numberDiff line numberDiff line change
@@ -217,21 +217,15 @@ inline bool Environment::AsyncCallbackScope::in_makecallback() const {
217217
return env_->makecallback_cntr_ > 1;
218218
}
219219

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

225-
inline uint8_t* Environment::TickInfo::fields() {
223+
inline AliasedBuffer<uint8_t, v8::Uint8Array>& Environment::TickInfo::fields() {
226224
return fields_;
227225
}
228226

229-
inline int Environment::TickInfo::fields_count() const {
230-
return kFieldsCount;
231-
}
232-
233-
inline uint8_t Environment::TickInfo::scheduled() const {
234-
return fields_[kScheduled];
227+
inline bool Environment::TickInfo::has_scheduled() const {
228+
return fields_[kHasScheduled] == 1;
235229
}
236230

237231
inline void Environment::AssignToContext(v8::Local<v8::Context> context,
@@ -269,6 +263,7 @@ inline Environment::Environment(IsolateData* isolate_data,
269263
v8::Local<v8::Context> context)
270264
: isolate_(context->GetIsolate()),
271265
isolate_data_(isolate_data),
266+
tick_info_(context->GetIsolate()),
272267
timer_base_(uv_now(isolate_data->event_loop())),
273268
printed_error_(false),
274269
trace_sync_io_(false),

src/env.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -453,20 +453,19 @@ class Environment {
453453

454454
class TickInfo {
455455
public:
456-
inline uint8_t* fields();
457-
inline int fields_count() const;
458-
inline uint8_t scheduled() const;
456+
inline AliasedBuffer<uint8_t, v8::Uint8Array>& fields();
457+
inline bool has_scheduled() const;
459458

460459
private:
461460
friend class Environment; // So we can call the constructor.
462-
inline TickInfo();
461+
inline explicit TickInfo(v8::Isolate* isolate);
463462

464463
enum Fields {
465-
kScheduled,
464+
kHasScheduled,
466465
kFieldsCount
467466
};
468467

469-
uint8_t fields_[kFieldsCount];
468+
AliasedBuffer<uint8_t, v8::Uint8Array> fields_;
470469

471470
DISALLOW_COPY_AND_ASSIGN(TickInfo);
472471
};

src/node.cc

+3-11
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ using v8::SealHandleScope;
169169
using v8::String;
170170
using v8::TryCatch;
171171
using v8::Uint32Array;
172-
using v8::Uint8Array;
173172
using v8::Undefined;
174173
using v8::V8;
175174
using v8::Value;
@@ -1152,13 +1151,6 @@ void SetupNextTick(const FunctionCallbackInfo<Value>& args) {
11521151
env->context(),
11531152
FIXED_ONE_BYTE_STRING(env->isolate(), "_setupNextTick")).FromJust();
11541153

1155-
// Values use to cross communicate with processNextTick.
1156-
uint8_t* const fields = env->tick_info()->fields();
1157-
uint8_t const fields_count = env->tick_info()->fields_count();
1158-
1159-
Local<ArrayBuffer> array_buffer =
1160-
ArrayBuffer::New(env->isolate(), fields, sizeof(*fields) * fields_count);
1161-
11621154
v8::Local<v8::Function> run_microtasks_fn =
11631155
env->NewFunctionTemplate(RunMicrotasks)->GetFunction(env->context())
11641156
.ToLocalChecked();
@@ -1167,7 +1159,7 @@ void SetupNextTick(const FunctionCallbackInfo<Value>& args) {
11671159

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

11731165
args.GetReturnValue().Set(ret);
@@ -1286,7 +1278,7 @@ void InternalCallbackScope::Close() {
12861278

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

1289-
if (tick_info->scheduled() == 0) {
1281+
if (!tick_info->has_scheduled()) {
12901282
env_->isolate()->RunMicrotasks();
12911283
}
12921284

@@ -1297,7 +1289,7 @@ void InternalCallbackScope::Close() {
12971289
CHECK_EQ(env_->trigger_async_id(), 0);
12981290
}
12991291

1300-
if (tick_info->scheduled() == 0) {
1292+
if (!tick_info->has_scheduled()) {
13011293
return;
13021294
}
13031295

0 commit comments

Comments
 (0)