Skip to content

Commit 0d3ca2b

Browse files
authored
perf: ConcatSource source (#199)
1 parent b1eb535 commit 0d3ca2b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/concat_source.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,16 @@ impl ConcatSource {
163163

164164
impl Source for ConcatSource {
165165
fn source(&self) -> SourceValue {
166-
let rope = self.rope();
167-
SourceValue::String(Cow::Owned(rope.to_string()))
166+
let children = self.optimized_children();
167+
if children.len() == 1 {
168+
children[0].source()
169+
} else {
170+
let mut buf = String::with_capacity(self.size());
171+
for child in children {
172+
buf.push_str(&child.source().into_string_lossy());
173+
}
174+
SourceValue::String(Cow::Owned(buf))
175+
}
168176
}
169177

170178
fn rope(&self) -> Rope<'_> {
@@ -186,7 +194,7 @@ impl Source for ConcatSource {
186194
if children.len() == 1 {
187195
children[0].buffer()
188196
} else {
189-
let mut buffer = vec![];
197+
let mut buffer = Vec::with_capacity(self.size());
190198
self.to_writer(&mut buffer).unwrap();
191199
Cow::Owned(buffer)
192200
}

0 commit comments

Comments
 (0)