Skip to content

Commit 8b8798f

Browse files
committed
perf: rope iter
1 parent 04b5c66 commit 8b8798f

File tree

8 files changed

+230
-142
lines changed

8 files changed

+230
-142
lines changed

src/cached_source.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
struct CachedData {
2222
hash: OnceLock<u64>,
2323
size: OnceLock<usize>,
24-
source: OnceLock<(Vec<&'static str>, usize)>,
24+
source: OnceLock<Vec<&'static str>>,
2525
columns_map: OnceLock<Option<SourceMap>>,
2626
line_only_map: OnceLock<Option<SourceMap>>,
2727
}
@@ -84,25 +84,25 @@ impl CachedSource {
8484

8585
impl Source for CachedSource {
8686
fn source(&self) -> SourceValue {
87-
let (chunks, len) = self.cache.source.get_or_init(|| {
87+
let chunks = self.cache.source.get_or_init(|| {
8888
#[allow(unsafe_code)]
8989
// SAFETY: CachedSource guarantees that the underlying source outlives the cache,
9090
// so transmuting Vec<&str> to Vec<&'static str> is safe in this context.
9191
// This allows us to store string slices in the cache without additional allocations.
9292
unsafe {
93-
std::mem::transmute::<(Vec<&str>, usize), (Vec<&'static str>, usize)>(
94-
self.rope(),
93+
std::mem::transmute::<Vec<&str>, Vec<&'static str>>(
94+
self.rope().collect(),
9595
)
9696
}
9797
});
98-
let mut string = String::with_capacity(*len);
98+
let mut string = String::with_capacity(self.size());
9999
for chunk in chunks {
100100
string.push_str(chunk);
101101
}
102102
SourceValue::String(Cow::Owned(string))
103103
}
104104

105-
fn rope(&self) -> (Vec<&str>, usize) {
105+
fn rope(&self) -> Box<dyn Iterator<Item = &str> + '_> {
106106
self.inner.rope()
107107
}
108108

src/concat_source.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,12 @@ impl Source for ConcatSource {
175175
}
176176
}
177177

178-
fn rope(&self) -> (Vec<&str>, usize) {
178+
fn rope(&self) -> Box<dyn Iterator<Item = &str> + '_> {
179179
let children = self.optimized_children();
180180
if children.len() == 1 {
181181
children[0].rope()
182182
} else {
183-
let mut merged_chunks = vec![];
184-
let mut merged_len = 0;
185-
for child in children {
186-
let (chunks, len) = child.rope();
187-
merged_chunks.extend(chunks);
188-
merged_len += len;
189-
}
190-
(merged_chunks, merged_len)
183+
Box::new(children.iter().flat_map(|child| child.rope()))
191184
}
192185
}
193186

src/original_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ impl Source for OriginalSource {
5656
SourceValue::String(Cow::Borrowed(&self.value))
5757
}
5858

59-
fn rope(&self) -> (Vec<&str>, usize) {
60-
(vec![self.value.as_ref()], self.value.len())
59+
fn rope(&self) -> Box<dyn Iterator<Item = &str> + '_> {
60+
Box::new(std::iter::once(self.value.as_ref()))
6161
}
6262

6363
fn buffer(&self) -> Cow<[u8]> {

src/raw_source.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ impl Source for RawStringSource {
6464
SourceValue::String(Cow::Borrowed(&self.0))
6565
}
6666

67-
fn rope(&self) -> (Vec<&str>, usize) {
68-
(vec![self.0.as_ref()], self.0.len())
67+
fn rope(&self) -> Box<dyn Iterator<Item = &str> + '_> {
68+
Box::new(std::iter::once(self.0.as_ref()))
6969
}
7070

7171
fn buffer(&self) -> Cow<[u8]> {
@@ -214,9 +214,8 @@ impl Source for RawBufferSource {
214214
SourceValue::Buffer(Cow::Borrowed(&self.value))
215215
}
216216

217-
fn rope(&self) -> (Vec<&str>, usize) {
218-
let s = self.get_or_init_value_as_string();
219-
(vec![s], s.len())
217+
fn rope(&self) -> Box<dyn Iterator<Item = &str> + '_> {
218+
Box::new(std::iter::once(self.get_or_init_value_as_string()))
220219
}
221220

222221
fn buffer(&self) -> Cow<[u8]> {

0 commit comments

Comments
 (0)