Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/bench_complex_replace_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36737,7 +36737,7 @@ pub fn benchmark_complex_replace_source_map_cached_source_stream_chunks(
cached_source.map(&ObjectPool::default(), &MapOptions::default());

b.iter(|| {
black_box(cached_source.stream_chunks(
black_box(cached_source.stream_chunks().stream(
&ObjectPool::default(),
&MapOptions::default(),
&mut |_chunk, _mapping| {},
Expand Down
86 changes: 58 additions & 28 deletions src/cached_source.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
borrow::Cow,
cell::OnceCell,
hash::{Hash, Hasher},
sync::{Arc, OnceLock},
};
Expand All @@ -9,10 +10,9 @@ use rustc_hash::FxHasher;
use crate::{
helpers::{
stream_and_get_source_and_map, stream_chunks_of_raw_source,
stream_chunks_of_source_map, StreamChunks,
stream_chunks_of_source_map, Chunks, GeneratedInfo, StreamChunks,
},
object_pool::ObjectPool,
rope::Rope,
source::SourceValue,
BoxSource, MapOptions, Source, SourceExt, SourceMap,
};
Expand Down Expand Up @@ -86,10 +86,6 @@ impl Source for CachedSource {
self.inner.source()
}

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

fn buffer(&self) -> Cow<[u8]> {
let mut buffer = vec![];
self.to_writer(&mut buffer).unwrap();
Expand Down Expand Up @@ -125,44 +121,68 @@ impl Source for CachedSource {
}
}

impl StreamChunks for CachedSource {
fn stream_chunks<'a>(
struct CachedSourceChunks<'source> {
chunks: Box<dyn Chunks + 'source>,
cache: Arc<CachedData>,
inner: &'source dyn Source,
source: OnceCell<Cow<'source, str>>,
}

impl<'a> CachedSourceChunks<'a> {
fn new(cache_source: &'a CachedSource) -> Self {
Self {
chunks: cache_source.inner.stream_chunks(),
cache: cache_source.cache.clone(),
inner: &cache_source.inner,
source: OnceCell::new(),
}
}
}

impl Chunks for CachedSourceChunks<'_> {
fn stream<'a>(
&'a self,
object_pool: &'a ObjectPool,
options: &MapOptions,
on_chunk: crate::helpers::OnChunk<'_, 'a>,
on_source: crate::helpers::OnSource<'_, 'a>,
on_name: crate::helpers::OnName<'_, 'a>,
) -> crate::helpers::GeneratedInfo {
) -> GeneratedInfo {
let cell = if options.columns {
&self.cache.columns_map
} else {
&self.cache.line_only_map
};
match cell.get() {
Some(map) => {
let source = self.rope();
let source = self
.source
.get_or_init(|| self.inner.source().into_string_lossy());
if let Some(map) = map {
stream_chunks_of_source_map(
options,
object_pool,
source,
source.as_ref(),
map,
on_chunk,
on_source,
on_name,
)
} else {
stream_chunks_of_raw_source(
source, options, on_chunk, on_source, on_name,
source.as_ref(),
options,
on_chunk,
on_source,
on_name,
)
}
}
None => {
let (generated_info, map) = stream_and_get_source_and_map(
options,
object_pool,
&self.inner,
self.chunks.as_ref(),
on_chunk,
on_source,
on_name,
Expand All @@ -174,6 +194,12 @@ impl StreamChunks for CachedSource {
}
}

impl StreamChunks for CachedSource {
fn stream_chunks<'a>(&'a self) -> Box<dyn Chunks + 'a> {
Box::new(CachedSourceChunks::new(self))
}
}

impl Clone for CachedSource {
fn clone(&self) -> Self {
Self {
Expand Down Expand Up @@ -323,22 +349,26 @@ mod tests {
let mut on_chunk_count = 0;
let mut on_source_count = 0;
let mut on_name_count = 0;
let generated_info = source.stream_chunks(
&ObjectPool::default(),
&map_options,
&mut |_chunk, _mapping| {
on_chunk_count += 1;
},
&mut |_source_index, _source, _source_content| {
on_source_count += 1;
},
&mut |_name_index, _name| {
on_name_count += 1;
},
);
let generated_info = {
let object_pool = ObjectPool::default();
let chunks = source.stream_chunks();
chunks.stream(
&object_pool,
&map_options,
&mut |_chunk, _mapping| {
on_chunk_count += 1;
},
&mut |_source_index, _source, _source_content| {
on_source_count += 1;
},
&mut |_name_index, _name| {
on_name_count += 1;
},
)
};

let cached_source = CachedSource::new(source);
cached_source.stream_chunks(
cached_source.stream_chunks().stream(
&ObjectPool::default(),
&map_options,
&mut |_chunk, _mapping| {},
Expand All @@ -349,7 +379,7 @@ mod tests {
let mut cached_on_chunk_count = 0;
let mut cached_on_source_count = 0;
let mut cached_on_name_count = 0;
let cached_generated_info = cached_source.stream_chunks(
let cached_generated_info = cached_source.stream_chunks().stream(
&ObjectPool::default(),
&map_options,
&mut |_chunk, _mapping| {
Expand Down
75 changes: 41 additions & 34 deletions src/concat_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use std::{
use rustc_hash::FxHashMap as HashMap;

use crate::{
helpers::{get_map, GeneratedInfo, OnChunk, OnName, OnSource, StreamChunks},
helpers::{get_map, Chunks, GeneratedInfo, StreamChunks},
linear_map::LinearMap,
object_pool::ObjectPool,
source::{Mapping, OriginalLocation},
BoxSource, MapOptions, RawStringSource, Rope, Source, SourceExt, SourceMap,
BoxSource, MapOptions, RawStringSource, Source, SourceExt, SourceMap,
SourceValue,
};

Expand Down Expand Up @@ -175,20 +175,6 @@ impl Source for ConcatSource {
}
}

fn rope(&self) -> Rope<'_> {
let children = self.optimized_children();
if children.len() == 1 {
children[0].rope()
} else {
let mut rope = Rope::new();
for child in children {
let child_rope = child.rope();
rope.append(child_rope);
}
rope
}
}

fn buffer(&self) -> Cow<[u8]> {
let children = self.optimized_children();
if children.len() == 1 {
Expand All @@ -208,12 +194,14 @@ impl Source for ConcatSource {
.sum()
}

fn map(
&self,
object_pool: &ObjectPool,
fn map<'a>(
&'a self,
object_pool: &'a ObjectPool,
options: &MapOptions,
) -> Option<SourceMap> {
get_map(object_pool, self, options)
let chunks = self.stream_chunks();
let result = get_map(object_pool, chunks.as_ref(), options);
result
}

fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
Expand All @@ -240,19 +228,32 @@ impl PartialEq for ConcatSource {
}
impl Eq for ConcatSource {}

impl StreamChunks for ConcatSource {
fn stream_chunks<'a>(
&'a self,
object_pool: &'a ObjectPool,
options: &MapOptions,
on_chunk: OnChunk<'_, 'a>,
on_source: OnSource<'_, 'a>,
on_name: OnName<'_, 'a>,
) -> crate::helpers::GeneratedInfo {
let children = self.optimized_children();
struct ConcatSourceChunks<'source> {
children_chunks: Vec<Box<dyn Chunks + 'source>>,
}

if children.len() == 1 {
return children[0].stream_chunks(
impl<'source> ConcatSourceChunks<'source> {
fn new(concat_source: &'source ConcatSource) -> Self {
let children = concat_source.optimized_children();
let children_chunks = children
.iter()
.map(|child| child.stream_chunks())
.collect::<Vec<_>>();
Self { children_chunks }
}
}

impl Chunks for ConcatSourceChunks<'_> {
fn stream<'b>(
&'b self,
object_pool: &'b ObjectPool,
options: &MapOptions,
on_chunk: crate::helpers::OnChunk<'_, 'b>,
on_source: crate::helpers::OnSource<'_, 'b>,
on_name: crate::helpers::OnName<'_, 'b>,
) -> GeneratedInfo {
if self.children_chunks.len() == 1 {
return self.children_chunks[0].stream(
object_pool,
options,
on_chunk,
Expand All @@ -271,14 +272,14 @@ impl StreamChunks for ConcatSource {
let name_index_mapping: RefCell<LinearMap<u32>> =
RefCell::new(LinearMap::default());

for item in children {
for child_handle in &self.children_chunks {
source_index_mapping.borrow_mut().clear();
name_index_mapping.borrow_mut().clear();
let mut last_mapping_line = 0;
let GeneratedInfo {
generated_line,
generated_column,
} = item.stream_chunks(
} = child_handle.stream(
object_pool,
options,
&mut |chunk, mapping| {
Expand Down Expand Up @@ -418,6 +419,12 @@ impl StreamChunks for ConcatSource {
}
}

impl StreamChunks for ConcatSource {
fn stream_chunks<'a>(&'a self) -> Box<dyn Chunks + 'a> {
Box::new(ConcatSourceChunks::new(self))
}
}

fn optimize(children: &mut Vec<BoxSource>) -> Vec<BoxSource> {
let original_children = std::mem::take(children);

Expand Down
3 changes: 0 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ pub type Result<T> = result::Result<T, Error>;
pub enum Error {
/// a JSON parsing related failure
BadJson(simd_json::Error),
/// rope related failure
Rope(&'static str),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::BadJson(err) => write!(f, "bad json: {err}"),
Error::Rope(err) => write!(f, "rope error: {err}"),
}
}
}
Expand Down
Loading