diff options
author | Marcos Casagrande <marcoscvp90@gmail.com> | 2022-10-04 15:48:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-04 15:48:50 +0200 |
commit | 569287b15b6482a39f2c816f103574c3b35351f8 (patch) | |
tree | ff8433fc87613e3016ff7a188ee34aa3fc7d81c4 /core/ops_builtin.rs | |
parent | 0b4a6c4d084df54e827bc7767ce8653e06c45e93 (diff) |
perf(ext/fetch): consume body using ops (#16038)
This commit adds a fast path to `Request` and `Response` that
make consuming request bodies much faster when using `Body#text`,
`Body#arrayBuffer`, and `Body#blob`, if the body is a FastStream.
Because the response bodies for `fetch` are FastStream, this speeds up
consuming `fetch` response bodies significantly.
Diffstat (limited to 'core/ops_builtin.rs')
-rw-r--r-- | core/ops_builtin.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/core/ops_builtin.rs b/core/ops_builtin.rs index 6ca2a132c..7393d4b69 100644 --- a/core/ops_builtin.rs +++ b/core/ops_builtin.rs @@ -34,6 +34,7 @@ pub(crate) fn init_builtins() -> Extension { op_add::decl(), // // TODO(@AaronO): track IO metrics for builtin streams op_read::decl(), + op_read_all::decl(), op_write::decl(), op_shutdown::decl(), op_metrics::decl(), @@ -169,6 +170,26 @@ async fn op_read( } #[op] +async fn op_read_all( + state: Rc<RefCell<OpState>>, + rid: ResourceId, +) -> Result<ZeroCopyBuf, Error> { + let resource = state.borrow().resource_table.get_any(rid)?; + let (min, maximum) = resource.size_hint(); + let size = maximum.unwrap_or(min) as usize; + + let mut buffer = Vec::with_capacity(size); + loop { + let tmp = ZeroCopyBuf::new_temp(vec![0u8; 64 * 1024]); + let (nread, tmp) = resource.clone().read_return(tmp).await?; + if nread == 0 { + return Ok(buffer.into()); + } + buffer.extend_from_slice(&tmp[..nread]); + } +} + +#[op] async fn op_write( state: Rc<RefCell<OpState>>, rid: ResourceId, |