Skip to content

Conversation

cdellacqua
Copy link
Contributor

Content

This pull request provides two new methods: extend_from_slice and the inverse drain_to_slice. Both are only available for items which are Copy (i.e. no custom Drop).

A related refactor

In order to simplify the implementation, I slightly changed the operating mode of copy_from_slice and copy_to_slice: instead of expecting slices with the same length as the buffer, both methods now also accept slices which are smaller.

This refactor allowed me to write the extend and drain methods as wrappers for copy from/to with an extra step for adjusting the read and write pointers.

This change should be backward compatible as current users would observe the same behaviour as before (unless they wrote a test with a should_panic specific to the previous equality check).

Context

The idea proposed by @phip1611 in #134 to have extend_from_slice proved useful in a project of mine and, as I was referring to in #141#issuecomment-2643272235, I had worked on a similar implementation with drain_to_slice too.

Performance

As for #141, having specialised methods for Copy types brings a substantial performance improvement for buffers smaller than a megabyte (and a slight speedup for larger sizes).

E.g. (note: in the first chart the X axis is in microseconds, in the second chart it's in nanoseconds)
image

@cdellacqua
Copy link
Contributor Author

The failing test is unrelated to this PR. It looks like the latest nightly changed the error message for tests/compile-fail/test_const_generic_array_zero_length_new.rs

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.

1 participant