Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: speed up package.json parsing #15767

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -120,6 +120,9 @@ function readPackage(requestPath) {
return false;
}

if (json === '')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does V8 make this and json.length === 0 equivalent instruction-wise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s.length === 0 is probably a bit slower. The empty string is a singleton so s === '' is basically a pointer comparison, whereas s.length needs to validate s first (although I expect it will be a fast path protected by a guard once optimized.)

return packageMainCache[requestPath] = undefined;

try {
var pkg = packageMainCache[requestPath] = JSON.parse(json).main;
} catch (e) {
8 changes: 5 additions & 3 deletions src/node_file.cc
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@

#include "req-wrap-inl.h"
#include "string_bytes.h"
#include "string_search.h"

#include <fcntl.h>
#include <sys/types.h>
@@ -466,8 +467,9 @@ void FillStatsArray(double* fields, const uv_stat_t* s) {
}

// Used to speed up module loading. Returns the contents of the file as
// a string or undefined when the file cannot be opened. The speedup
// comes from not creating Error objects on failure.
// a string or undefined when the file cannot be opened. Returns an empty
// string when the file does not contain the substring '"main"' because that
// is the property we care about.
static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
uv_loop_t* loop = env->event_loop();
@@ -516,7 +518,7 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
}

const size_t size = offset - start;
if (size == 0) {
if (size == 0 || size == SearchString(&chars[start], size, "\"main\"")) {
args.GetReturnValue().SetEmptyString();
} else {
Local<String> chars_string =
11 changes: 10 additions & 1 deletion src/string_search.h
Original file line number Diff line number Diff line change
@@ -638,6 +638,7 @@ size_t SearchString(const Char* haystack,
size_t needle_length,
size_t start_index,
bool is_forward) {
if (haystack_length < needle_length) return haystack_length;
// To do a reverse search (lastIndexOf instead of indexOf) without redundant
// code, create two vectors that are reversed views into the input strings.
// For example, v_needle[0] would return the *last* character of the needle.
@@ -646,7 +647,6 @@ size_t SearchString(const Char* haystack,
needle, needle_length, is_forward);
Vector<const Char> v_haystack = Vector<const Char>(
haystack, haystack_length, is_forward);
CHECK(haystack_length >= needle_length);
size_t diff = haystack_length - needle_length;
size_t relative_start_index;
if (is_forward) {
@@ -664,6 +664,15 @@ size_t SearchString(const Char* haystack,
}
return is_forward ? pos : (haystack_length - needle_length - pos);
}

template <size_t N>
size_t SearchString(const char* haystack, size_t haystack_length,
const char (&needle)[N]) {
return SearchString(
reinterpret_cast<const uint8_t*>(haystack), haystack_length,
reinterpret_cast<const uint8_t*>(needle), N - 1, 0, true);
}

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
1 change: 0 additions & 1 deletion test/fixtures/packages/invalid/index.js

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/packages/invalid/package.json

This file was deleted.

5 changes: 5 additions & 0 deletions test/parallel/test-module-binding.js
Original file line number Diff line number Diff line change
@@ -2,8 +2,13 @@
require('../common');
const fixtures = require('../common/fixtures');
const { internalModuleReadFile } = process.binding('fs');
const { readFileSync } = require('fs');
const { strictEqual } = require('assert');

strictEqual(internalModuleReadFile('nosuchfile'), undefined);
strictEqual(internalModuleReadFile(fixtures.path('empty.txt')), '');
strictEqual(internalModuleReadFile(fixtures.path('empty-with-bom.txt')), '');
{
const filename = fixtures.path('require-bin/package.json');
strictEqual(internalModuleReadFile(filename), readFileSync(filename, 'utf8'));
}
8 changes: 0 additions & 8 deletions test/sequential/test-module-loading.js
Original file line number Diff line number Diff line change
@@ -100,14 +100,6 @@ const d2 = require('../fixtures/b/d');
assert.notStrictEqual(threeFolder, three);
}

console.error('test package.json require() loading');
assert.throws(
function() {
require('../fixtures/packages/invalid');
},
/^SyntaxError: Error parsing .+: Unexpected token , in JSON at position 1$/
);

assert.strictEqual(require('../fixtures/packages/index').ok, 'ok',
'Failed loading package');
assert.strictEqual(require('../fixtures/packages/main').ok, 'ok',