Skip to content

Commit

Permalink
feat: use fxhasher (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Feb 20, 2023
1 parent 493d395 commit d954a57
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ include = ["/src/**/*.rs", "/*.toml", "/LICENSE", "/README.md"]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
dyn-clone = "1"
hashbrown = "0.12"
rustc-hash = "1"
once_cell = "1"
parking_lot = "0.12"
dashmap = "5"
Expand Down
11 changes: 8 additions & 3 deletions src/cached_source.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::{borrow::Cow, hash::Hash, sync::Arc};
use std::{
borrow::Cow,
hash::{BuildHasherDefault, Hash},
sync::Arc,
};

use dashmap::DashMap;
use hashbrown::hash_map::DefaultHashBuilder;
use once_cell::sync::OnceCell;
use rustc_hash::FxHasher;

use crate::{helpers::StreamChunks, MapOptions, Source, SourceMap};

Expand Down Expand Up @@ -44,7 +48,8 @@ pub struct CachedSource<T> {
inner: Arc<T>,
cached_buffer: OnceCell<Vec<u8>>,
cached_source: OnceCell<Arc<str>>,
cached_maps: DashMap<MapOptions, Option<SourceMap>, DefaultHashBuilder>,
cached_maps:
DashMap<MapOptions, Option<SourceMap>, BuildHasherDefault<FxHasher>>,
}

impl<T> CachedSource<T> {
Expand Down
10 changes: 5 additions & 5 deletions src/concat_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
hash::{Hash, Hasher},
};

use hashbrown::HashMap;
use rustc_hash::FxHashMap as HashMap;

use crate::{
helpers::{get_map, GeneratedInfo, OnChunk, OnName, OnSource, StreamChunks},
Expand Down Expand Up @@ -138,14 +138,14 @@ impl StreamChunks for ConcatSource {
}
let mut current_line_offset = 0;
let mut current_column_offset = 0;
let mut source_mapping: HashMap<String, u32> = HashMap::new();
let mut name_mapping: HashMap<String, u32> = HashMap::new();
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 source_index_mapping: RefCell<HashMap<u32, u32>> =
RefCell::new(HashMap::new());
RefCell::new(HashMap::default());
let name_index_mapping: RefCell<HashMap<u32, u32>> =
RefCell::new(HashMap::new());
RefCell::new(HashMap::default());
let mut last_mapping_line = 0;
let GeneratedInfo {
generated_line,
Expand Down
24 changes: 12 additions & 12 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow::BorrowMut, cell::RefCell, sync::Arc};

use hashbrown::HashMap;
use rustc_hash::FxHashMap as HashMap;
use substring::Substring;

use crate::{
Expand Down Expand Up @@ -899,29 +899,29 @@ pub fn stream_chunks_of_combined_source_map(
let inner_source: RefCell<Option<ArcStr>> =
RefCell::new(inner_source.map(Into::into));
let source_mapping: RefCell<HashMap<ArcStr, u32>> =
RefCell::new(HashMap::new());
let mut name_mapping: HashMap<ArcStr, u32> = HashMap::new();
RefCell::new(HashMap::default());
let mut name_mapping: HashMap<ArcStr, u32> = HashMap::default();
let source_index_mapping: RefCell<HashMap<i64, i64>> =
RefCell::new(HashMap::new());
RefCell::new(HashMap::default());
let name_index_mapping: RefCell<HashMap<i64, i64>> =
RefCell::new(HashMap::new());
RefCell::new(HashMap::default());
let name_index_value_mapping: RefCell<HashMap<i64, ArcStr>> =
RefCell::new(HashMap::new());
RefCell::new(HashMap::default());
let inner_source_index: RefCell<i64> = RefCell::new(-2);
let inner_source_index_mapping: RefCell<HashMap<i64, i64>> =
RefCell::new(HashMap::new());
RefCell::new(HashMap::default());
let inner_source_index_value_mapping: RefCell<
HashMap<i64, (ArcStr, Option<ArcStr>)>,
> = RefCell::new(HashMap::new());
> = RefCell::new(HashMap::default());
let inner_source_contents: RefCell<HashMap<i64, Option<ArcStr>>> =
RefCell::new(HashMap::new());
RefCell::new(HashMap::default());
let inner_source_content_lines: RefCell<
HashMap<i64, Option<Arc<Vec<ArcStr>>>>,
> = RefCell::new(HashMap::new());
> = RefCell::new(HashMap::default());
let inner_name_index_mapping: RefCell<HashMap<i64, i64>> =
RefCell::new(HashMap::new());
RefCell::new(HashMap::default());
let inner_name_index_value_mapping: RefCell<HashMap<i64, ArcStr>> =
RefCell::new(HashMap::new());
RefCell::new(HashMap::default());
let inner_source_map_line_data: RefCell<Vec<SourceMapLineData>> =
RefCell::new(Vec::new());
let find_inner_mapping = |line: i64, column: i64| -> Option<u32> {
Expand Down
6 changes: 3 additions & 3 deletions src/replace_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::{
sync::Arc,
};

use hashbrown::HashMap;
use parking_lot::Mutex;
use rustc_hash::FxHashMap as HashMap;
use substring::Substring;

use crate::{
Expand Down Expand Up @@ -169,9 +169,9 @@ impl<T: Source> StreamChunks for ReplaceSource<T> {
let source_content_lines: RefCell<Vec<Option<Vec<String>>>> =
RefCell::new(Vec::new());
let name_mapping: RefCell<HashMap<String, u32>> =
RefCell::new(HashMap::new());
RefCell::new(HashMap::default());
let name_index_mapping: RefCell<HashMap<u32, u32>> =
RefCell::new(HashMap::new());
RefCell::new(HashMap::default());

// check if source_content[line][col] is equal to expect
// Why this is needed?
Expand Down

1 comment on commit d954a57

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: d954a57 Previous: 7dde6b8 Ratio
benchmark_concat_generate_base64 25951 ns/iter (± 482) 22588 ns/iter (± 227) 1.15
benchmark_concat_generate_base64_with_cache 17675 ns/iter (± 124) 15006 ns/iter (± 66) 1.18
benchmark_concat_generate_string 11217 ns/iter (± 61) 10678 ns/iter (± 108) 1.05
benchmark_concat_generate_string_with_cache 2993 ns/iter (± 34) 3332 ns/iter (± 18) 0.90

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.