From ed1caacfcbf413f906300cad56d5c8b4fb3aa644 Mon Sep 17 00:00:00 2001 From: Anna Henningsen <anna@addaleax.net> 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.) --- 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 f08833b201b19d..49e2a6aa0d93e3 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -223,7 +223,7 @@ inline MaybeLocal<Promise> 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<CloseReq*>(req->data); + CloseReq* close = CloseReq::from_req(req); CHECK_NOT_NULL(close); close->file_handle()->AfterClose(); Isolate* isolate = close->env()->isolate(); @@ -477,7 +477,7 @@ bool FSReqAfterScope::Proceed() { } void AfterNoArgs(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast<FSReqBase*>(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); if (after.Proceed()) @@ -485,7 +485,7 @@ void AfterNoArgs(uv_fs_t* req) { } void AfterStat(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast<FSReqBase*>(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); if (after.Proceed()) { @@ -494,7 +494,7 @@ void AfterStat(uv_fs_t* req) { } void AfterInteger(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast<FSReqBase*>(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); if (after.Proceed()) @@ -502,7 +502,7 @@ void AfterInteger(uv_fs_t* req) { } void AfterOpenFileHandle(uv_fs_t* req) { - FSReqWrap* req_wrap = static_cast<FSReqWrap*>(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); if (after.Proceed()) { @@ -512,7 +512,7 @@ void AfterOpenFileHandle(uv_fs_t* req) { } void AfterStringPath(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast<FSReqBase*>(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); MaybeLocal<Value> link; @@ -531,7 +531,7 @@ void AfterStringPath(uv_fs_t* req) { } void AfterStringPtr(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast<FSReqBase*>(req->data); + FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); MaybeLocal<Value> link; @@ -550,7 +550,7 @@ void AfterStringPtr(uv_fs_t* req) { } void AfterScanDir(uv_fs_t* req) { - FSReqBase* req_wrap = static_cast<FSReqBase*>(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<uv_fs_t> { bool use_bigint() const { return use_bigint_; } + static FSReqBase* from_req(uv_fs_t* req) { + return static_cast<FSReqBase*>(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<Value> reason); + static CloseReq* from_req(uv_fs_t* req) { + return static_cast<CloseReq*>(ReqWrap::from_req(req)); + } + private: Persistent<Promise> promise_; Persistent<Value> ref_;