diff options
| author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2022-04-27 07:03:44 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-27 16:03:44 +0200 |
| commit | 8b8b21b553fba03124934363e77e636adaeb4745 (patch) | |
| tree | dc21d5b6df8dbbd9f38980e00d76dcfec5921863 /runtime/ops | |
| parent | 9853c96cc4686a6cd1ffa1e9081c012b8df72ff7 (diff) | |
perf(runtime): read entire files in single ops (#14261)
Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
Diffstat (limited to 'runtime/ops')
| -rw-r--r-- | runtime/ops/fs.rs | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/runtime/ops/fs.rs b/runtime/ops/fs.rs index ddd7f9ca2..501c2ac5f 100644 --- a/runtime/ops/fs.rs +++ b/runtime/ops/fs.rs @@ -95,6 +95,10 @@ pub fn init() -> Extension { op_futime_async::decl(), op_utime_sync::decl(), op_utime_async::decl(), + op_readfile_sync::decl(), + op_readfile_text_sync::decl(), + op_readfile_async::decl(), + op_readfile_text_async::decl(), ]) .build() } @@ -2008,3 +2012,79 @@ fn op_cwd(state: &mut OpState) -> Result<String, AnyError> { let path_str = into_string(path.into_os_string())?; Ok(path_str) } + +#[op] +fn op_readfile_sync( + state: &mut OpState, + path: String, +) -> Result<ZeroCopyBuf, AnyError> { + let permissions = state.borrow_mut::<Permissions>(); + let path = Path::new(&path); + permissions.read.check(path)?; + Ok(std::fs::read(path)?.into()) +} + +#[op] +fn op_readfile_text_sync( + state: &mut OpState, + path: String, +) -> Result<String, AnyError> { + let permissions = state.borrow_mut::<Permissions>(); + let path = Path::new(&path); + permissions.read.check(path)?; + Ok(std::fs::read_to_string(path)?) +} + +#[op] +async fn op_readfile_async( + state: Rc<RefCell<OpState>>, + path: String, + cancel_rid: Option<ResourceId>, +) -> Result<ZeroCopyBuf, AnyError> { + { + let path = Path::new(&path); + let mut state = state.borrow_mut(); + state.borrow_mut::<Permissions>().read.check(path)?; + } + let fut = tokio::task::spawn_blocking(move || { + let path = Path::new(&path); + Ok(std::fs::read(path).map(ZeroCopyBuf::from)?) + }); + if let Some(cancel_rid) = cancel_rid { + let cancel_handle = state + .borrow_mut() + .resource_table + .get::<CancelHandle>(cancel_rid); + if let Ok(cancel_handle) = cancel_handle { + return fut.or_cancel(cancel_handle).await??; + } + } + fut.await? +} + +#[op] +async fn op_readfile_text_async( + state: Rc<RefCell<OpState>>, + path: String, + cancel_rid: Option<ResourceId>, +) -> Result<String, AnyError> { + { + let path = Path::new(&path); + let mut state = state.borrow_mut(); + state.borrow_mut::<Permissions>().read.check(path)?; + } + let fut = tokio::task::spawn_blocking(move || { + let path = Path::new(&path); + Ok(String::from_utf8(std::fs::read(path)?)?) + }); + if let Some(cancel_rid) = cancel_rid { + let cancel_handle = state + .borrow_mut() + .resource_table + .get::<CancelHandle>(cancel_rid); + if let Ok(cancel_handle) = cancel_handle { + return fut.or_cancel(cancel_handle).await??; + } + } + fut.await? +} |
