Skip to content

Commit 2e729d8

Browse files
committed
perf(buffer): unify Buffer::write_bytes with write_bytesview
`Buffer::write_bytes(value : Bytes)` and `Buffer::write_bytesview(value : BytesView)` were doing the same thing with different parameter types. Expand the former to `BytesView` and make it delegate — existing `Bytes` callers keep working via coercion, and the implementation is now one line instead of two parallel blit loops kept in sync. `write_bytesview` stays for now (callers may have reached for the explicit name); in a future cleanup it can be deprecated once downstream migrates to just using `write_bytes`. Continues the view-safe expansion theme (#3459, #3460, #3461).
1 parent a098d73 commit 2e729d8

2 files changed

Lines changed: 6 additions & 9 deletions

File tree

buffer/buffer.mbt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ pub fn new(size_hint? : Int = 0) -> Buffer {
133133

134134
///|
135135
/// Create a buffer from a bytes.
136-
pub fn from_bytes(bytes : Bytes) -> Buffer {
136+
pub fn from_bytes(bytes : BytesView) -> Buffer {
137137
let val_len = bytes.length()
138138
let buf = new(size_hint=val_len)
139139
// inline write_bytes, skip grow_if_necessary check
140140
// SAFETY: known bytes size
141-
buf.data.blit_from_bytes(0, bytes, 0, val_len)
141+
buf.data.blit_from_bytes(0, bytes.data(), bytes.start_offset(), val_len)
142142
buf.len = val_len
143143
buf
144144
}
@@ -737,11 +737,8 @@ pub fn Buffer::write_object(self : Buffer, value : &Show) -> Unit {
737737
/// )
738738
/// }
739739
/// ```
740-
pub fn Buffer::write_bytes(self : Buffer, value : Bytes) -> Unit {
741-
let val_len = value.length()
742-
self.grow_if_necessary(self.len + val_len)
743-
self.data.blit_from_bytes(self.len, value, 0, val_len)
744-
self.len += val_len
740+
pub fn Buffer::write_bytes(self : Buffer, value : BytesView) -> Unit {
741+
self.write_bytesview(value)
745742
}
746743

747744
///|

buffer/pkg.generated.mbti

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
// Values
99
pub fn from_array(ArrayView[Byte]) -> Buffer
1010

11-
pub fn from_bytes(Bytes) -> Buffer
11+
pub fn from_bytes(BytesView) -> Buffer
1212

1313
pub fn from_iter(Iter[Byte]) -> Buffer
1414

@@ -28,7 +28,7 @@ pub fn Buffer::to_bytes(Self) -> Bytes
2828
pub fn Buffer::to_repr(Self) -> @debug.Repr
2929
pub fn Buffer::view(Self) -> ArrayView[Byte]
3030
pub fn Buffer::write_byte(Self, Byte) -> Unit
31-
pub fn Buffer::write_bytes(Self, Bytes) -> Unit
31+
pub fn Buffer::write_bytes(Self, BytesView) -> Unit
3232
pub fn Buffer::write_bytesview(Self, BytesView) -> Unit
3333
pub fn Buffer::write_char_utf16be(Self, Char) -> Unit
3434
pub fn Buffer::write_char_utf16le(Self, Char) -> Unit

0 commit comments

Comments
 (0)