Skip to content

Commit e9cae8a

Browse files
committed
into_wrapper to avoid redundant Vec
1 parent 5b9d7cf commit e9cae8a

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

vm/src/obj/objbytearray.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,10 @@ impl PyByteArray {
451451

452452
#[pymethod(name = "splitlines")]
453453
fn splitlines(&self, options: pystr::SplitLinesArgs, vm: &VirtualMachine) -> PyResult {
454-
let as_bytes = self
454+
let lines = self
455455
.borrow_value()
456-
.splitlines(options)
457-
.iter()
458-
.map(|x| vm.ctx.new_bytearray(x.to_vec()))
459-
.collect::<Vec<PyObjectRef>>();
460-
Ok(vm.ctx.new_list(as_bytes))
456+
.splitlines(options, |x| vm.ctx.new_bytearray(x.to_vec()));
457+
Ok(vm.ctx.new_list(lines))
461458
}
462459

463460
#[pymethod(name = "zfill")]

vm/src/obj/objbyteinner.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,12 @@ impl PyByteInner {
961961
res
962962
}
963963

964-
pub fn splitlines(&self, options: pystr::SplitLinesArgs) -> Vec<&[u8]> {
965-
self.elements.py_splitlines(self.elements.iter(), options)
964+
pub fn splitlines<FW, W>(&self, options: pystr::SplitLinesArgs, into_wrapper: FW) -> Vec<W>
965+
where
966+
FW: Fn(&[u8]) -> W,
967+
{
968+
self.elements
969+
.py_splitlines(self.elements.iter(), options, into_wrapper)
966970
}
967971

968972
pub fn zfill(&self, width: isize) -> Vec<u8> {

vm/src/obj/objbytes.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,13 +418,10 @@ impl PyBytes {
418418

419419
#[pymethod(name = "splitlines")]
420420
fn splitlines(&self, options: pystr::SplitLinesArgs, vm: &VirtualMachine) -> PyResult {
421-
let as_bytes = self
421+
let lines = self
422422
.inner
423-
.splitlines(options)
424-
.iter()
425-
.map(|x| vm.ctx.new_bytes(x.to_vec()))
426-
.collect::<Vec<PyObjectRef>>();
427-
Ok(vm.ctx.new_list(as_bytes))
423+
.splitlines(options, |x| vm.ctx.new_bytes(x.to_vec()));
424+
Ok(vm.ctx.new_list(lines))
428425
}
429426

430427
#[pymethod(name = "zfill")]

vm/src/obj/objstr.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,10 +803,7 @@ impl PyString {
803803
fn splitlines(&self, args: pystr::SplitLinesArgs, vm: &VirtualMachine) -> PyObjectRef {
804804
vm.ctx.new_list(
805805
self.value
806-
.py_splitlines(self.value.bytes(), args)
807-
.iter()
808-
.map(|s| vm.ctx.new_str(*s))
809-
.collect(),
806+
.py_splitlines(self.value.bytes(), args, |s| vm.new_str(s.to_owned())),
810807
)
811808
}
812809

vm/src/obj/pystr.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,15 @@ pub trait PyCommonString<E> {
298298
}
299299
}
300300

301-
fn py_splitlines<U8>(
301+
fn py_splitlines<U8, FW, W>(
302302
&self,
303303
bytes: impl std::iter::Iterator<Item = U8>,
304304
options: SplitLinesArgs,
305-
) -> Vec<&Self>
305+
into_wrapper: FW,
306+
) -> Vec<W>
306307
where
307308
U8: CopyTo<u8>,
309+
FW: Fn(&Self) -> W,
308310
{
309311
let keep = if options.keepends { 1 } else { 0 };
310312
let mut elements = Vec::new();
@@ -331,10 +333,10 @@ pub trait PyCommonString<E> {
331333
};
332334
let range = last_i..i + end_len;
333335
last_i = i + i_diff;
334-
elements.push(self.get_bytes(range));
336+
elements.push(into_wrapper(self.get_bytes(range)));
335337
}
336338
if last_i != self.bytes_len() {
337-
elements.push(self.get_bytes(last_i..self.bytes_len()));
339+
elements.push(into_wrapper(self.get_bytes(last_i..self.bytes_len())));
338340
}
339341
elements
340342
}

0 commit comments

Comments
 (0)