diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-09-16 20:28:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-16 20:28:15 +0200 |
commit | 00948a6d680f855dcd0b60628a4b97186496a58c (patch) | |
tree | c8e739c26b9e65612c6e527d55760bfabb2d03e3 /runtime/js/40_read_file.js | |
parent | 868f38d4528bae508fdb222402441ba374db0721 (diff) |
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
Diffstat (limited to 'runtime/js/40_read_file.js')
-rw-r--r-- | runtime/js/40_read_file.js | 26 |
1 files changed, 7 insertions, 19 deletions
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 = { |