Skip to content

Commit d0f8af0

Browse files
addaleaxtargos
authored andcommitted
src: use offset calc. instead of req->data in node_file
A small refactor – this removes one layer of pointer indirection. (The performance gain is likely negligible, the main point here being that this encapsulates libuv request management a bit more.) PR-URL: #21839 Reviewed-By: Eugene Ostroukhov <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Jon Moss <[email protected]>
1 parent ff5c6dc commit d0f8af0

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/node_file.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ inline MaybeLocal<Promise> FileHandle::ClosePromise() {
221221
closing_ = true;
222222
CloseReq* req = new CloseReq(env(), promise, object());
223223
auto AfterClose = uv_fs_callback_t{[](uv_fs_t* req) {
224-
CloseReq* close = static_cast<CloseReq*>(req->data);
224+
CloseReq* close = CloseReq::from_req(req);
225225
CHECK_NOT_NULL(close);
226226
close->file_handle()->AfterClose();
227227
Isolate* isolate = close->env()->isolate();
@@ -475,15 +475,15 @@ bool FSReqAfterScope::Proceed() {
475475
}
476476

477477
void AfterNoArgs(uv_fs_t* req) {
478-
FSReqBase* req_wrap = static_cast<FSReqBase*>(req->data);
478+
FSReqBase* req_wrap = FSReqBase::from_req(req);
479479
FSReqAfterScope after(req_wrap, req);
480480

481481
if (after.Proceed())
482482
req_wrap->Resolve(Undefined(req_wrap->env()->isolate()));
483483
}
484484

485485
void AfterStat(uv_fs_t* req) {
486-
FSReqBase* req_wrap = static_cast<FSReqBase*>(req->data);
486+
FSReqBase* req_wrap = FSReqBase::from_req(req);
487487
FSReqAfterScope after(req_wrap, req);
488488

489489
if (after.Proceed()) {
@@ -492,15 +492,15 @@ void AfterStat(uv_fs_t* req) {
492492
}
493493

494494
void AfterInteger(uv_fs_t* req) {
495-
FSReqBase* req_wrap = static_cast<FSReqBase*>(req->data);
495+
FSReqBase* req_wrap = FSReqBase::from_req(req);
496496
FSReqAfterScope after(req_wrap, req);
497497

498498
if (after.Proceed())
499499
req_wrap->Resolve(Integer::New(req_wrap->env()->isolate(), req->result));
500500
}
501501

502502
void AfterOpenFileHandle(uv_fs_t* req) {
503-
FSReqWrap* req_wrap = static_cast<FSReqWrap*>(req->data);
503+
FSReqBase* req_wrap = FSReqBase::from_req(req);
504504
FSReqAfterScope after(req_wrap, req);
505505

506506
if (after.Proceed()) {
@@ -510,7 +510,7 @@ void AfterOpenFileHandle(uv_fs_t* req) {
510510
}
511511

512512
void AfterStringPath(uv_fs_t* req) {
513-
FSReqBase* req_wrap = static_cast<FSReqBase*>(req->data);
513+
FSReqBase* req_wrap = FSReqBase::from_req(req);
514514
FSReqAfterScope after(req_wrap, req);
515515

516516
MaybeLocal<Value> link;
@@ -529,7 +529,7 @@ void AfterStringPath(uv_fs_t* req) {
529529
}
530530

531531
void AfterStringPtr(uv_fs_t* req) {
532-
FSReqBase* req_wrap = static_cast<FSReqBase*>(req->data);
532+
FSReqBase* req_wrap = FSReqBase::from_req(req);
533533
FSReqAfterScope after(req_wrap, req);
534534

535535
MaybeLocal<Value> link;
@@ -548,7 +548,7 @@ void AfterStringPtr(uv_fs_t* req) {
548548
}
549549

550550
void AfterScanDir(uv_fs_t* req) {
551-
FSReqBase* req_wrap = static_cast<FSReqBase*>(req->data);
551+
FSReqBase* req_wrap = FSReqBase::from_req(req);
552552
FSReqAfterScope after(req_wrap, req);
553553

554554
if (after.Proceed()) {

src/node_file.h

+8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ class FSReqBase : public ReqWrap<uv_fs_t> {
6868

6969
bool use_bigint() const { return use_bigint_; }
7070

71+
static FSReqBase* from_req(uv_fs_t* req) {
72+
return static_cast<FSReqBase*>(ReqWrap::from_req(req));
73+
}
74+
7175
private:
7276
enum encoding encoding_ = UTF8;
7377
bool has_data_ = false;
@@ -284,6 +288,10 @@ class FileHandle : public AsyncWrap, public StreamBase {
284288

285289
void Reject(Local<Value> reason);
286290

291+
static CloseReq* from_req(uv_fs_t* req) {
292+
return static_cast<CloseReq*>(ReqWrap::from_req(req));
293+
}
294+
287295
private:
288296
Persistent<Promise> promise_;
289297
Persistent<Value> ref_;

0 commit comments

Comments
 (0)