From d0f8af021f5b7aaf2c872ce2616d2e76d5d9cc5e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 16 Jul 2018 22:31:28 +0200 Subject: [PATCH] src: use offset calc. instead of `req->data` in node_file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/21839 Reviewed-By: Eugene Ostroukhov Reviewed-By: Colin Ihrig Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Gus Caplan Reviewed-By: James M Snell Reviewed-By: Tobias Nießen Reviewed-By: Jon Moss --- src/node_file.cc | 16 ++++++++-------- src/node_file.h | 8 ++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index c62697cf312b0a..8414a22ad4cd5f 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -221,7 +221,7 @@ inline MaybeLocal FileHandle::ClosePromise() { closing_ = true; CloseReq* req = new CloseReq(env(), promise, object()); auto AfterClose = uv_fs_callback_t{[](uv_fs_t* req) { - CloseReq* close = static_cast(req->data); + CloseReq* close = CloseReq::from_req(req); CHECK_NOT_NULL(close); close->file_handle()->AfterClose(); Isolate* isolate = close->env()->isolate(); @@ -475,7 +475,7 @@ bool FSReqAfterScope::Proceed() { } void AfterNoArgs(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); if (after.Proceed()) @@ -483,7 +483,7 @@ void AfterNoArgs(uv_fs_t* req) { } void AfterStat(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); if (after.Proceed()) { @@ -492,7 +492,7 @@ void AfterStat(uv_fs_t* req) { } void AfterInteger(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); if (after.Proceed()) @@ -500,7 +500,7 @@ void AfterInteger(uv_fs_t* req) { } void AfterOpenFileHandle(uv_fs_t* req) { - FSReqWrap* req_wrap = static_cast(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); if (after.Proceed()) { @@ -510,7 +510,7 @@ void AfterOpenFileHandle(uv_fs_t* req) { } void AfterStringPath(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); MaybeLocal link; @@ -529,7 +529,7 @@ void AfterStringPath(uv_fs_t* req) { } void AfterStringPtr(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); MaybeLocal link; @@ -548,7 +548,7 @@ void AfterStringPtr(uv_fs_t* req) { } void AfterScanDir(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); if (after.Proceed()) { diff --git a/src/node_file.h b/src/node_file.h index 6b45dc881750a7..141d1d42d744a2 100644 --- a/src/node_file.h +++ b/src/node_file.h @@ -68,6 +68,10 @@ class FSReqBase : public ReqWrap { bool use_bigint() const { return use_bigint_; } + static FSReqBase* from_req(uv_fs_t* req) { + return static_cast(ReqWrap::from_req(req)); + } + private: enum encoding encoding_ = UTF8; bool has_data_ = false; @@ -284,6 +288,10 @@ class FileHandle : public AsyncWrap, public StreamBase { void Reject(Local reason); + static CloseReq* from_req(uv_fs_t* req) { + return static_cast(ReqWrap::from_req(req)); + } + private: Persistent promise_; Persistent ref_;