Skip to content

Commit 4460f02

Browse files
committed
perf: reduce allocation pressure by changing usage of Iterator::fold to concat
1 parent 513bdfb commit 4460f02

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/concat_source.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,22 @@ impl ConcatSource {
7878

7979
impl Source for ConcatSource {
8080
fn source(&self) -> Cow<str> {
81-
let all = self.children.iter().fold(String::new(), |mut acc, cur| {
82-
acc.push_str(&cur.source());
83-
acc
84-
});
81+
let all = self.children.iter().map(|child| child.source()).collect();
8582
Cow::Owned(all)
8683
}
8784

8885
fn buffer(&self) -> Cow<[u8]> {
89-
let all = self.children.iter().fold(Vec::new(), |mut acc, cur| {
90-
acc.extend(&*cur.buffer());
91-
acc
92-
});
86+
let all = self
87+
.children
88+
.iter()
89+
.map(|child| child.buffer())
90+
.collect::<Vec<_>>()
91+
.concat();
9392
Cow::Owned(all)
9493
}
9594

9695
fn size(&self) -> usize {
97-
self.children.iter().fold(0, |mut acc, cur| {
98-
acc += cur.size();
99-
acc
100-
})
96+
self.children.iter().map(|child| child.size()).sum()
10197
}
10298

10399
fn map(&self, options: &MapOptions) -> Option<SourceMap> {

0 commit comments

Comments
 (0)