Skip to content

Commit 8b53f3c

Browse files
bnoordhuisevanlucas
authored andcommitted
src: speed up module loading, don't resize buffer
Don't bother shrinking the read buffer on the final read because we dispose it immediately afterwards. Avoids some unnecessary memory allocation and copying. PR-URL: #9132 Reviewed-By: James M Snell <[email protected]>
1 parent 362c307 commit 8b53f3c

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/node_file.cc

+7-11
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,11 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
546546
return;
547547
}
548548

549+
const size_t kBlockSize = 32 << 10;
549550
std::vector<char> chars;
550551
int64_t offset = 0;
551-
for (;;) {
552-
const size_t kBlockSize = 32 << 10;
552+
ssize_t numchars;
553+
do {
553554
const size_t start = chars.size();
554555
chars.resize(start + kBlockSize);
555556

@@ -558,32 +559,27 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
558559
buf.len = kBlockSize;
559560

560561
uv_fs_t read_req;
561-
const ssize_t numchars =
562-
uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr);
562+
numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr);
563563
uv_fs_req_cleanup(&read_req);
564564

565565
CHECK_GE(numchars, 0);
566-
if (static_cast<size_t>(numchars) < kBlockSize) {
567-
chars.resize(start + numchars);
568-
break;
569-
}
570566
offset += numchars;
571-
}
567+
} while (static_cast<size_t>(numchars) == kBlockSize);
572568

573569
uv_fs_t close_req;
574570
CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
575571
uv_fs_req_cleanup(&close_req);
576572

577573
size_t start = 0;
578-
if (chars.size() >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
574+
if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
579575
start = 3; // Skip UTF-8 BOM.
580576
}
581577

582578
Local<String> chars_string =
583579
String::NewFromUtf8(env->isolate(),
584580
&chars[start],
585581
String::kNormalString,
586-
chars.size() - start);
582+
offset - start);
587583
args.GetReturnValue().Set(chars_string);
588584
}
589585

0 commit comments

Comments
 (0)