Skip to content

Conversation

lucacasonato
Copy link
Member

@lucacasonato lucacasonato commented Sep 30, 2022

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.

@bartlomieju
Copy link
Member

@lucacasonato do you have some benchmarks for this change?

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM, nice work

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM again :)

Copy link
Member

@littledivy littledivy left a comment

Choose a reason for hiding this comment

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

LGTM!

@lucacasonato
Copy link
Member Author

Ran some benchmarks:

  • Reading a 80mb file using await Deno.readAll() is identical (within 1%)
  • Reading a 80mb file using Response(file.readable).arrayBuffer() is 12% faster
    • Now ~10% faster than await Deno.readAll(), previously ~15% slower
  • fetching a 100mb file, and calling resp.arrayBuffer() on it is 5-6% faster
  • File server (File->http) throughput is identical (within 1%)
  • Proxy server (fetch->resp) throughput is 4% higher

While benchmarking I thought of another optimisation that can make streaming HTTP req/resp bodies from Rust -> JS maybe around 5% faster. Also another optimisation to make the file server ~3% faster. I'll do these in a follow up.

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.
@lucacasonato lucacasonato enabled auto-merge (squash) October 9, 2022 14:28
@lucacasonato lucacasonato merged commit 3b6b75b into main Oct 9, 2022
bartlomieju pushed a commit that referenced this pull request Oct 17, 2022
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.
@lucacasonato lucacasonato deleted the resource_read_write branch November 4, 2022 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants