Skip to content

Commit

Permalink
refactor: wip cached source rope
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed Dec 6, 2024
1 parent 05501fa commit 2f2d24a
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 179 deletions.
29 changes: 18 additions & 11 deletions src/cached_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@ impl<T> CachedSource<T> {
}

impl<T: Source> CachedSource<T> {
// fn rope(&self) -> &Rope {
// // self.inner.with_cached_rope(|source_vec| {
// // source_vec.get_or_init(|| self.inner.borrow_inner().rope())
// // })
// self.inner.borrow_inner().rope()
// }
fn get_rope(&self) -> &Rope<'_> {
self
.inner
.with(|cache| cache.cached_rope.get_or_init(|| cache.inner.rope()))
}
}

impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T> {
Expand All @@ -99,7 +98,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T> {
}

fn rope(&self) -> Rope<'_> {
self.inner.borrow_inner().rope().clone()
self.get_rope().clone()
}

fn buffer(&self) -> Cow<[u8]> {
Expand Down Expand Up @@ -138,12 +137,11 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks<'_>
on_source: crate::helpers::OnSource<'_, 'a>,
on_name: crate::helpers::OnName<'_, 'a>,
) -> crate::helpers::GeneratedInfo {
todo!();
let cached_map = self.inner.borrow_cached_maps().entry(options.clone());
match cached_map {
Entry::Occupied(entry) => {
// let source = self.rope();
let source = "";
let source = self.get_rope();
if let Some(map) = entry.get() {
#[allow(unsafe_code)]
// SAFETY: We guarantee that once a `SourceMap` is stored in the cache, it will never be removed.
Expand All @@ -154,11 +152,20 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks<'_>
let map =
unsafe { std::mem::transmute::<&SourceMap, &'a SourceMap>(map) };
stream_chunks_of_source_map(
source, map, on_chunk, on_source, on_name, options,
source.clone(),
map,
on_chunk,
on_source,
on_name,
options,
)
} else {
stream_chunks_of_raw_source(
source, options, on_chunk, on_source, on_name,
source.clone(),
options,
on_chunk,
on_source,
on_name,
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/concat_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Source for ConcatSource {
let mut rope = crate::rope::Rope::new();
for child in children {
let child_rope = child.rope();
rope.extend(child_rope.chunks());
rope.extend(child_rope.chunks().map(|(s, _)| s));
}
rope
}
Expand Down
Loading

0 comments on commit 2f2d24a

Please sign in to comment.