From 00948a6d680f855dcd0b60628a4b97186496a58c Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Thu, 16 Sep 2021 20:28:15 +0200 Subject: perf(runtime/fs): optimize readFile by using a single large buffer (#12057) * perf(runtime/fs): optimize readFile by using a single large buffer * handle extended/truncated files during read Allocate an extra byte in our read buffer to detect "overflow" then fallback to unsized readAll for remainder of extended file, this is a slowpath that should rarely happen in practice --- runtime/js/40_read_file.js | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) (limited to 'runtime/js/40_read_file.js') diff --git a/runtime/js/40_read_file.js b/runtime/js/40_read_file.js index 75cfd8074..53ac9b08a 100644 --- a/runtime/js/40_read_file.js +++ b/runtime/js/40_read_file.js @@ -4,13 +4,13 @@ ((window) => { const core = window.Deno.core; const { open, openSync } = window.__bootstrap.files; - const { readAllInner, readAllSync } = window.__bootstrap.io; + const { readAllSyncSized, readAllInnerSized } = window.__bootstrap.io; function readFileSync(path) { const file = openSync(path); try { - const contents = readAllSync(file); - return contents; + const { size } = file.statSync(); + return readAllSyncSized(file, size); } finally { file.close(); } @@ -19,31 +19,19 @@ async function readFile(path, options) { const file = await open(path); try { - const contents = await readAllInner(file, options); - return contents; + const { size } = await file.stat(); + return await readAllInnerSized(file, size, options); } finally { file.close(); } } function readTextFileSync(path) { - const file = openSync(path); - try { - const contents = readAllSync(file); - return core.decode(contents); - } finally { - file.close(); - } + return core.decode(readFileSync(path)); } async function readTextFile(path, options) { - const file = await open(path); - try { - const contents = await readAllInner(file, options); - return core.decode(contents); - } finally { - file.close(); - } + return core.decode(await readFile(path, options)); } window.__bootstrap.readFile = { -- cgit v1.2.3