Skip to content

Commit

Permalink
feat: parallel call stream_chunks of childern
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Feb 1, 2024
1 parent a440626 commit 5d4a6ab
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 45 deletions.
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ substring = "1"
smallvec = "1.11.2"
memchr = "2.6.4"
str_indices = "0.4.3"
rayon = "1.8.1"

codspeed-criterion-compat = { version = "2.3.3", default-features = false, optional = true }

Expand Down
117 changes: 72 additions & 45 deletions src/concat_source.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::{
borrow::Cow,
cell::RefCell,
hash::{Hash, Hasher},
borrow::Cow, cell::RefCell, hash::{Hash, Hasher}, sync::{Arc, Mutex}
};

use rustc_hash::FxHashMap as HashMap;
use rayon::prelude::*;

use crate::{
helpers::{get_map, GeneratedInfo, OnChunk, OnName, OnSource, StreamChunks},
Expand Down Expand Up @@ -124,6 +123,12 @@ impl PartialEq for ConcatSource {
}
}

enum Callback {
Source(u32, String, Option<String>),
Chunk(Option<String>, Mapping),
Name(u32, String),
}

impl StreamChunks for ConcatSource {
fn stream_chunks(
&self,
Expand All @@ -140,26 +145,47 @@ impl StreamChunks for ConcatSource {
let mut current_column_offset = 0;
let mut source_mapping: HashMap<String, u32> = HashMap::default();
let mut name_mapping: HashMap<String, u32> = HashMap::default();
let mut need_to_cloas_mapping = false;
for item in &self.children {
let mut need_to_close_mapping = false;

let result = self.children.par_iter().map(|item| {
let callbacks = RefCell::new(vec![]);
let generated_info = item.stream_chunks(
options,
&mut |chunk, mapping| {
callbacks.borrow_mut().push(Callback::Chunk(chunk.map(|c| c.to_string()), mapping));
},
&mut |i, source, source_content| {
callbacks.borrow_mut().push(Callback::Source(i, source.to_string(), source_content.map(|c| c.to_string())));
},
&mut |i, name| {
callbacks.borrow_mut().push(Callback::Name(i, name.to_string()));
},
);
(generated_info, callbacks.take())
}).collect::<Vec<_>>();

for (generated_info, callbacks) in result {
let GeneratedInfo {
generated_line,
generated_column,
} = generated_info;

let source_index_mapping: RefCell<HashMap<u32, u32>> =
RefCell::new(HashMap::default());
RefCell::new(HashMap::default());
let name_index_mapping: RefCell<HashMap<u32, u32>> =
RefCell::new(HashMap::default());
let mut last_mapping_line = 0;
let GeneratedInfo {
generated_line,
generated_column,
} = item.stream_chunks(
options,
&mut |chunk, mapping| {
let line = mapping.generated_line + current_line_offset;

for callback in callbacks.into_iter() {
match callback {
Callback::Chunk(chunk, mapping) => {
let line = mapping.generated_line + current_line_offset;
let column = if mapping.generated_line == 1 {
mapping.generated_column + current_column_offset
} else {
mapping.generated_column
};
if need_to_cloas_mapping {
if need_to_close_mapping {
if mapping.generated_line != 1 || mapping.generated_column != 0 {
on_chunk(
None,
Expand All @@ -170,7 +196,7 @@ impl StreamChunks for ConcatSource {
},
);
}
need_to_cloas_mapping = false;
need_to_close_mapping = false;
}
let result_source_index =
mapping.original.as_ref().and_then(|original| {
Expand Down Expand Up @@ -213,7 +239,7 @@ impl StreamChunks for ConcatSource {
(result_source_index, &mapping.original)
{
on_chunk(
chunk,
chunk.as_ref().map(|c| c.as_str()),
Mapping {
generated_line: line,
generated_column: column,
Expand All @@ -227,41 +253,42 @@ impl StreamChunks for ConcatSource {
);
} else {
on_chunk(
chunk,
chunk.as_ref().map(|c| c.as_str()),
Mapping {
generated_line: line,
generated_column: column,
original: None,
},
);
}
},
&mut |i, source, source_content| {
let mut global_index = source_mapping.get(source).copied();
if global_index.is_none() {
let len = source_mapping.len() as u32;
source_mapping.insert(source.to_owned(), len);
on_source(len, source, source_content);
global_index = Some(len);
}
source_index_mapping
.borrow_mut()
.insert(i, global_index.unwrap());
},
&mut |i, name| {
let mut global_index = name_mapping.get(name).copied();
if global_index.is_none() {
let len = name_mapping.len() as u32;
name_mapping.insert(name.to_owned(), len);
on_name(len, name);
global_index = Some(len);
},
Callback::Source(i, source, source_content) => {
let mut global_index = source_mapping.get(&source).copied();
if global_index.is_none() {
let len = source_mapping.len() as u32;
source_mapping.insert(source.to_owned(), len);
on_source(len, &source, source_content.as_ref().map(|c| c.as_str()));
global_index = Some(len);
}
source_index_mapping
.borrow_mut()
.insert(i, global_index.unwrap());
},
Callback::Name(i, name) => {
let mut global_index = name_mapping.get(&name).copied();
if global_index.is_none() {
let len = name_mapping.len() as u32;
name_mapping.insert(name.to_owned(), len);
on_name(len, &name);
global_index = Some(len);
}
name_index_mapping
.borrow_mut()
.insert(i, global_index.unwrap());
}
name_index_mapping
.borrow_mut()
.insert(i, global_index.unwrap());
},
);
if need_to_cloas_mapping && (generated_line != 1 || generated_column != 0)
}
}
if need_to_close_mapping && (generated_line != 1 || generated_column != 0)
{
on_chunk(
None,
Expand All @@ -271,14 +298,14 @@ impl StreamChunks for ConcatSource {
original: None,
},
);
need_to_cloas_mapping = false;
need_to_close_mapping = false;
}
if generated_line > 1 {
current_column_offset = generated_column;
} else {
current_column_offset += generated_column;
}
need_to_cloas_mapping = need_to_cloas_mapping
need_to_close_mapping = need_to_close_mapping
|| (options.final_source && last_mapping_line == generated_line);
current_line_offset += generated_line - 1;
}
Expand Down

0 comments on commit 5d4a6ab

Please sign in to comment.