diff options
| author | Luca Casonato <hello@lcas.dev> | 2022-10-09 16:49:25 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-09 14:49:25 +0000 |
| commit | 3b6b75bb46840a897a310dfd3fcbbd05618f3c5b (patch) | |
| tree | 2d4d06cef295f9bcd5c1829f5aa41c11e8aa0a6e /core/ops_builtin.rs | |
| parent | a622c5df27e908bff152ce7374c47dabfdba0bba (diff) | |
feat(core): improve resource read & write traits (#16115)
This commit introduces two new buffer wrapper types to `deno_core`. The
main benefit of these new wrappers is that they can wrap a number of
different underlying buffer types. This allows for a more flexible read
and write API on resources that will require less copying of data
between different buffer representations.
- `BufView` is a read-only view onto a buffer. It can be backed by
`ZeroCopyBuf`, `Vec<u8>`, and `bytes::Bytes`.
- `BufViewMut` is a read-write view onto a buffer. It can be cheaply
converted into a `BufView`. It can be backed by `ZeroCopyBuf` or
`Vec<u8>`.
Both new buffer views have a cursor. This means that the start point of
the view can be constrained to write / read from just a slice of the
view. Only the start point of the slice can be adjusted. The end point
is fixed. To adjust the end point, the underlying buffer needs to be
truncated.
Readable resources have been changed to better cater to resources that
do not support BYOB reads. The basic `read` method now returns a
`BufView` instead of taking a `ZeroCopyBuf` to fill. This allows the
operation to return buffers that the resource has already allocated,
instead of forcing the caller to allocate the buffer. BYOB reads are
still very useful for resources that support them, so a new `read_byob`
method has been added that takes a `BufViewMut` to fill. `op_read`
attempts to use `read_byob` if the resource supports it, which falls
back to `read` and performs an additional copy if it does not. For
Rust->JS reads this change should have no impact, but for Rust->Rust
reads, this allows the caller to avoid an additional copy in many
scenarios. This combined with the support for `BufView` to be backed by
`bytes::Bytes` allows us to avoid one data copy when piping from a
`fetch` response into an `ext/http` response.
Writable resources have been changed to take a `BufView` instead of a
`ZeroCopyBuf` as an argument. This allows for less copying of data in
certain scenarios, as described above. Additionally a new
`Resource::write_all` method has been added that takes a `BufView` and
continually attempts to write the resource until the entire buffer has
been written. Certain resources like files can override this method to
provide a more efficient `write_all` implementation.
Diffstat (limited to 'core/ops_builtin.rs')
| -rw-r--r-- | core/ops_builtin.rs | 74 |
1 files changed, 64 insertions, 10 deletions
diff --git a/core/ops_builtin.rs b/core/ops_builtin.rs index 7393d4b69..41741bf28 100644 --- a/core/ops_builtin.rs +++ b/core/ops_builtin.rs @@ -2,6 +2,8 @@ use crate::error::format_file_name; use crate::error::type_error; use crate::include_js_files; +use crate::io::BufMutView; +use crate::io::BufView; use crate::ops_metrics::OpMetrics; use crate::resources::ResourceId; use crate::Extension; @@ -166,7 +168,8 @@ async fn op_read( buf: ZeroCopyBuf, ) -> Result<u32, Error> { let resource = state.borrow().resource_table.get_any(rid)?; - resource.read_return(buf).await.map(|(n, _)| n as u32) + let view = BufMutView::from(buf); + resource.read_byob(view).await.map(|(n, _)| n as u32) } #[op] @@ -175,18 +178,67 @@ async fn op_read_all( 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); + // The number of bytes we attempt to grow the buffer by each time it fills + // up and we have more data to read. We start at 64 KB. The grow_len is + // doubled if the nread returned from a single read is equal or greater than + // the grow_len. This allows us to reduce allocations for resources that can + // read large chunks of data at a time. + let mut grow_len: usize = 64 * 1024; + + let (min, maybe_max) = resource.size_hint(); + // Try to determine an optimial starting buffer size for this resource based + // on the size hint. + let initial_size = match (min, maybe_max) { + (min, Some(max)) if min == max => min as usize, + (_min, Some(max)) if (max as usize) < grow_len => max as usize, + (min, _) if (min as usize) < grow_len => grow_len, + (min, _) => min as usize, + }; + + let mut buf = BufMutView::new(initial_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()); + // if the buffer does not have much remaining space, we may have to grow it. + if buf.len() < grow_len { + let vec = buf.get_mut_vec(); + match maybe_max { + Some(max) if vec.len() >= max as usize => { + // no need to resize the vec, because the vec is already large enough + // to accomodate the maximum size of the read data. + } + Some(max) if (max as usize) < vec.len() + grow_len => { + // grow the vec to the maximum size of the read data + vec.resize(max as usize, 0); + } + _ => { + // grow the vec by grow_len + vec.resize(vec.len() + grow_len, 0); + } + } + } + let (n, new_buf) = resource.clone().read_byob(buf).await?; + buf = new_buf; + buf.advance_cursor(n); + if n == 0 { + break; + } + if n >= grow_len { + // we managed to read more or equal data than fits in a single grow_len in + // a single go, so let's attempt to read even more next time. this reduces + // allocations for resources that can read large chunks of data at a time. + grow_len *= 2; } - buffer.extend_from_slice(&tmp[..nread]); } + + let nread = buf.reset_cursor(); + let mut vec = buf.unwrap_vec(); + // If the buffer is larger than the amount of data read, shrink it to the + // amount of data read. + if nread < vec.len() { + vec.truncate(nread); + } + + Ok(ZeroCopyBuf::from(vec)) } #[op] @@ -196,7 +248,9 @@ async fn op_write( buf: ZeroCopyBuf, ) -> Result<u32, Error> { let resource = state.borrow().resource_table.get_any(rid)?; - resource.write(buf).await.map(|n| n as u32) + let view = BufView::from(buf); + let resp = resource.write(view).await?; + Ok(resp.nwritten() as u32) } #[op] |
