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

perf(runtime): optimize Deno.file open & stream #15496

Merged
merged 4 commits into from Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions cli/bench/deno_common.js
Expand Up @@ -23,6 +23,11 @@ Deno.bench("perf_now", { n: 5e5 }, () => {
performance.now();
});

Deno.bench("open_file_sync", () => {
const file = Deno.openSync("./cli/bench/testdata/128k.bin");
file.close();
});

// A common "language feature", that should be fast
// also a decent representation of a non-trivial JSON-op
{
Expand Down
2 changes: 1 addition & 1 deletion cli/bench/http/deno_flash_send_file.js
Expand Up @@ -7,7 +7,7 @@ const { serve } = Deno;
const path = new URL("../testdata/128k.bin", import.meta.url).pathname;

function handler() {
const file = Deno.openSync(path, { read: true });
const file = Deno.openSync(path);
return new Response(file.readable);
}

Expand Down
15 changes: 11 additions & 4 deletions ext/web/06_streams.js
Expand Up @@ -658,7 +658,9 @@
* @returns {ReadableStream<Uint8Array>}
*/
function readableStreamForRid(rid, unrefCallback) {
const stream = new ReadableStream({
Copy link
Member Author

Choose a reason for hiding this comment

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

something to note: ReadableStream initialization triggers a pretty serious deopt in V8. From a flamegraph of the send file server on main:

image

const stream = webidl.createBranded(ReadableStream);
stream[_maybeRid] = rid;
const underlyingSource = {
type: "bytes",
async pull(controller) {
const v = controller.byobRequest.view;
Expand All @@ -685,9 +687,15 @@
core.tryClose(rid);
},
autoAllocateChunkSize: DEFAULT_CHUNK_SIZE,
});
};
initializeReadableStream(stream);
setUpReadableByteStreamControllerFromUnderlyingSource(
stream,
underlyingSource,
underlyingSource,
0,
);

stream[_maybeRid] = rid;
return stream;
}

Expand All @@ -714,7 +722,6 @@
}
return true;
}

/**
* @template T
* @param {{ [_queue]: Array<ValueWithSize<T | _close>>, [_queueTotalSize]: number }} container
Expand Down
16 changes: 10 additions & 6 deletions runtime/js/40_files.js
Expand Up @@ -33,26 +33,30 @@

function openSync(
path,
options = { read: true },
options,
) {
checkOpenOptions(options);
if (options) checkOpenOptions(options);
const mode = options?.mode;
const rid = ops.op_open_sync(
{ path: pathFromURL(path), options, mode },
pathFromURL(path),
options,
mode,
);

return new FsFile(rid);
}

async function open(
path,
options = { read: true },
options,
) {
checkOpenOptions(options);
if (options) checkOpenOptions(options);
const mode = options?.mode;
const rid = await core.opAsync(
"op_open_async",
{ path: pathFromURL(path), options, mode },
pathFromURL(path),
options,
mode,
);

return new FsFile(rid);
Expand Down
77 changes: 54 additions & 23 deletions runtime/ops/fs.rs
Expand Up @@ -128,15 +128,18 @@ pub struct OpenOptions {
create_new: bool,
}

#[inline]
fn open_helper(
state: &mut OpState,
args: &OpenArgs,
path: &str,
mode: Option<u32>,
options: Option<&OpenOptions>,
) -> Result<(PathBuf, std::fs::OpenOptions), AnyError> {
let path = Path::new(&args.path).to_path_buf();
let path = Path::new(path).to_path_buf();

let mut open_options = std::fs::OpenOptions::new();

if let Some(mode) = args.mode {
if let Some(mode) = mode {
// mode only used if creating the file on Unix
// if not specified, defaults to 0o666
#[cfg(unix)]
Expand All @@ -149,33 +152,48 @@ fn open_helper(
}

let permissions = state.borrow_mut::<Permissions>();
let options = &args.options;

if options.read {
permissions.read.check(&path)?;
}
match options {
None => {
permissions.read.check(&path)?;
open_options
.read(true)
.create(false)
.write(false)
.truncate(false)
.append(false)
.create_new(false);
}
Some(options) => {
if options.read {
permissions.read.check(&path)?;
}

if options.write || options.append {
permissions.write.check(&path)?;
}
if options.write || options.append {
permissions.write.check(&path)?;
}

open_options
.read(options.read)
.create(options.create)
.write(options.write)
.truncate(options.truncate)
.append(options.append)
.create_new(options.create_new);
open_options
.read(options.read)
.create(options.create)
.write(options.write)
.truncate(options.truncate)
.append(options.append)
.create_new(options.create_new);
}
}

Ok((path, open_options))
}

#[op]
fn op_open_sync(
state: &mut OpState,
args: OpenArgs,
path: String,
mode: Option<u32>,
options: Option<OpenOptions>,
) -> Result<ResourceId, AnyError> {
let (path, open_options) = open_helper(state, &args)?;
let (path, open_options) = open_helper(state, &path, mode, options.as_ref())?;
let std_file = open_options.open(&path).map_err(|err| {
Error::new(err.kind(), format!("{}, open '{}'", err, path.display()))
})?;
Expand All @@ -187,9 +205,12 @@ fn op_open_sync(
#[op]
async fn op_open_async(
state: Rc<RefCell<OpState>>,
args: OpenArgs,
path: String,
mode: Option<u32>,
options: Option<OpenOptions>,
) -> Result<ResourceId, AnyError> {
let (path, open_options) = open_helper(&mut state.borrow_mut(), &args)?;
let (path, open_options) =
open_helper(&mut state.borrow_mut(), &path, mode, options.as_ref())?;
let std_file = tokio::task::spawn_blocking(move || {
open_options.open(path.clone()).map_err(|err| {
Error::new(err.kind(), format!("{}, open '{}'", err, path.display()))
Expand Down Expand Up @@ -238,7 +259,12 @@ fn op_write_file_sync(
args: WriteFileArgs,
) -> Result<(), AnyError> {
let (open_args, data) = args.into_open_args_and_data();
let (path, open_options) = open_helper(state, &open_args)?;
let (path, open_options) = open_helper(
state,
&open_args.path,
open_args.mode,
Some(&open_args.options),
)?;
write_file(&path, open_options, &open_args, data)
}

Expand All @@ -256,7 +282,12 @@ async fn op_write_file_async(
None => None,
};
let (open_args, data) = args.into_open_args_and_data();
let (path, open_options) = open_helper(&mut *state.borrow_mut(), &open_args)?;
let (path, open_options) = open_helper(
&mut *state.borrow_mut(),
&open_args.path,
open_args.mode,
Some(&open_args.options),
)?;
let write_future = tokio::task::spawn_blocking(move || {
write_file(&path, open_options, &open_args, data)
});
Expand Down