diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2015-05-26 13:23:26 +0200 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2015-05-27 21:21:01 +0200 |
commit | b14fd1a7202cd8001d1f6aed848445bad99ee15c (patch) | |
tree | eef47e3ab4ce63db0491171630e7cba61dce6487 /src | |
parent | 98649fd31a50d68221a6aa6f58610efb5fae79c6 (diff) | |
download | node-new-b14fd1a7202cd8001d1f6aed848445bad99ee15c.tar.gz |
lib: speed up require(), phase 1
Replace calls to fs.statSync() with an internal variant that does not
create Error or Stat objects that put strain on the garbage collector.
A secondary benefit is that it improves start-up times in the debugger
because it no longer emits thousands of exception debug events.
PR-URL: https://github.com/nodejs/io.js/pull/1801
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/node_file.cc | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/node_file.cc b/src/node_file.cc index 71cf31829e..4e00f15e26 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -433,6 +433,26 @@ Local<Value> BuildStatsObject(Environment* env, const uv_stat_t* s) { return handle_scope.Escape(stats); } +// Used to speed up module loading. Returns 0 if the path refers to +// a file, 1 when it's a directory or < 0 on error (usually -ENOENT.) +// The speedup comes from not creating thousands of Stat and Error objects. +static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) { + Environment* env = Environment::GetCurrent(args); + + CHECK(args[0]->IsString()); + node::Utf8Value path(env->isolate(), args[0]); + + uv_fs_t req; + int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr); + if (rc == 0) { + const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr); + rc = !!(s->st_mode & S_IFDIR); + } + uv_fs_req_cleanup(&req); + + args.GetReturnValue().Set(rc); +} + static void Stat(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); @@ -1141,6 +1161,7 @@ void InitFs(Handle<Object> target, env->SetMethod(target, "rmdir", RMDir); env->SetMethod(target, "mkdir", MKDir); env->SetMethod(target, "readdir", ReadDir); + env->SetMethod(target, "internalModuleStat", InternalModuleStat); env->SetMethod(target, "stat", Stat); env->SetMethod(target, "lstat", LStat); env->SetMethod(target, "fstat", FStat); |