Closed Lukasa closed 6 years ago
This would be useful for apple/swift-nio-http2#10, which uses ByteBufferView
s heavily and tends to copy them around.
Er. Isn't ByteBufferView
already a ContiguousCollection
…? The code certainly seems to indicate that:
public struct ByteBufferView: ContiguousCollection, RandomAccessCollection {
Hah, yup, sure is. Ignore me then!
Currently if you want to write a
ByteBufferView
to aByteBuffer
, it will fall into this method:This ends up dropping into the very complex
mutating func _set<S: Sequence>(bytes: S, at index: _Index) -> _Capacity where S.Element == UInt8
, which is a very complex method that potentially performs multiple resizings and many bounds checks.This is all unnecessary, as
ByteBufferView
is backed by aByteBuffer
. This means that we can instead callwrite(buffer:)
with the appropriate details, and use the fast-path that that method uses to treat the collection as aContiguousCollection
.Note that we don't need to (or want to) actually conform the
ByteBufferView
toContiguousCollection
. Instead we just want the same magic we've done withByteBuffer
to apply here: take our special knowledge of the layout of this type and use it to obtain a performance improvement.