Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

derive: replace ahash with rustc-hash #137

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rinja_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ serde = { version = "1.0", optional = true, features = ["derive"] }
memchr = "2"
mime = "0.3"
mime_guess = "2"
once_map = "0.4.18"
once_map = { version = "0.4.18", default-features = false, features = ["std"] }
proc-macro2 = "1"
quote = "1"
rustc-hash = "2.0.0"
syn = "2.0.3"

[dev-dependencies]
Expand Down
12 changes: 7 additions & 5 deletions rinja_derive/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use once_map::sync::OnceMap;
use parser::node::Whitespace;
use parser::{ParseError, Parsed, Syntax};
use proc_macro2::Span;
use rustc_hash::FxBuildHasher;
#[cfg(feature = "config")]
use serde::Deserialize;

Expand Down Expand Up @@ -76,9 +77,10 @@ impl Config {
template_whitespace: Option<&str>,
config_span: Option<Span>,
) -> Result<&'static Config, CompileError> {
static CACHE: ManuallyDrop<OnceLock<OnceMap<OwnedConfigKey, &'static Config>>> =
ManuallyDrop::new(OnceLock::new());
CACHE.get_or_init(OnceMap::new).get_or_try_insert_ref(
static CACHE: ManuallyDrop<
OnceLock<OnceMap<OwnedConfigKey, &'static Config, FxBuildHasher>>,
> = ManuallyDrop::new(OnceLock::new());
CACHE.get_or_init(OnceMap::default).get_or_try_insert_ref(
&ConfigKey {
source: source.into(),
config_path: config_path.map(Cow::Borrowed),
Expand Down Expand Up @@ -232,7 +234,7 @@ impl Config {
#[derive(Debug, Default)]
pub(crate) struct SyntaxAndCache<'a> {
syntax: Syntax<'a>,
cache: OnceMap<OwnedSyntaxAndCacheKey, Arc<Parsed>>,
cache: OnceMap<OwnedSyntaxAndCacheKey, Arc<Parsed>, FxBuildHasher>,
}

impl<'a> Deref for SyntaxAndCache<'a> {
Expand Down Expand Up @@ -270,7 +272,7 @@ impl<'a> SyntaxAndCache<'a> {
fn new(syntax: Syntax<'a>) -> Self {
Self {
syntax,
cache: OnceMap::new(),
cache: OnceMap::default(),
}
}

Expand Down
15 changes: 7 additions & 8 deletions rinja_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use parser::node::{
};
use parser::{Expr, Filter, Node, Target, WithSpan};
use quote::quote;
use rustc_hash::FxBuildHasher;

use crate::config::WhitespaceHandling;
use crate::heritage::{Context, Heritage};
Expand All @@ -29,7 +30,7 @@ pub(crate) struct Generator<'a> {
// The template input state: original struct AST and attributes
input: &'a TemplateInput<'a>,
// All contexts, keyed by the package-relative template path
contexts: &'a HashMap<&'a Arc<Path>, Context<'a>>,
contexts: &'a HashMap<&'a Arc<Path>, Context<'a>, FxBuildHasher>,
// The heritage contains references to blocks and their ancestry
heritage: Option<&'a Heritage<'a>>,
// Variables accessible directly from the current scope (not redirected to context)
Expand All @@ -52,7 +53,7 @@ pub(crate) struct Generator<'a> {
impl<'a> Generator<'a> {
pub(crate) fn new<'n>(
input: &'n TemplateInput<'_>,
contexts: &'n HashMap<&'n Arc<Path>, Context<'n>>,
contexts: &'n HashMap<&'n Arc<Path>, Context<'n>, FxBuildHasher>,
heritage: Option<&'n Heritage<'_>>,
locals: MapChain<'n, Cow<'n, str>, LocalMeta>,
buf_writable_discard: bool,
Expand Down Expand Up @@ -2489,15 +2490,13 @@ impl LocalMeta {
}
}

// type SetChain<'a, T> = MapChain<'a, T, ()>;

#[derive(Debug, Clone)]
pub(crate) struct MapChain<'a, K, V>
where
K: cmp::Eq + hash::Hash,
{
parent: Option<&'a MapChain<'a, K, V>>,
scopes: Vec<HashMap<K, V>>,
scopes: Vec<HashMap<K, V, FxBuildHasher>>,
}

impl<'a, K: 'a, V: 'a> MapChain<'a, K, V>
Expand All @@ -2507,7 +2506,7 @@ where
fn with_parent<'p>(parent: &'p MapChain<'_, K, V>) -> MapChain<'p, K, V> {
MapChain {
parent: Some(parent),
scopes: vec![HashMap::new()],
scopes: vec![HashMap::default()],
}
}

Expand Down Expand Up @@ -2542,7 +2541,7 @@ where
}

fn push(&mut self) {
self.scopes.push(HashMap::new());
self.scopes.push(HashMap::default());
}

fn pop(&mut self) {
Expand Down Expand Up @@ -2570,7 +2569,7 @@ impl<'a, K: Eq + hash::Hash, V> Default for MapChain<'a, K, V> {
fn default() -> Self {
Self {
parent: None,
scopes: vec![HashMap::new()],
scopes: vec![HashMap::default()],
}
}
}
Expand Down
23 changes: 12 additions & 11 deletions rinja_derive/src/heritage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::Arc;

use parser::node::{BlockDef, Macro};
use parser::{Node, Parsed, WithSpan};
use rustc_hash::FxBuildHasher;

use crate::config::Config;
use crate::{CompileError, FileInfo};
Expand All @@ -16,7 +17,7 @@ pub(crate) struct Heritage<'a> {
impl Heritage<'_> {
pub(crate) fn new<'n>(
mut ctx: &'n Context<'n>,
contexts: &'n HashMap<&'n Arc<Path>, Context<'n>>,
contexts: &'n HashMap<&'n Arc<Path>, Context<'n>, FxBuildHasher>,
) -> Heritage<'n> {
let mut blocks: BlockAncestry<'n> = ctx
.blocks
Expand All @@ -35,15 +36,15 @@ impl Heritage<'_> {
}
}

type BlockAncestry<'a> = HashMap<&'a str, Vec<(&'a Context<'a>, &'a BlockDef<'a>)>>;
type BlockAncestry<'a> = HashMap<&'a str, Vec<(&'a Context<'a>, &'a BlockDef<'a>)>, FxBuildHasher>;

#[derive(Clone)]
pub(crate) struct Context<'a> {
pub(crate) nodes: &'a [Node<'a>],
pub(crate) extends: Option<Arc<Path>>,
pub(crate) blocks: HashMap<&'a str, &'a BlockDef<'a>>,
pub(crate) macros: HashMap<&'a str, &'a Macro<'a>>,
pub(crate) imports: HashMap<&'a str, Arc<Path>>,
pub(crate) blocks: HashMap<&'a str, &'a BlockDef<'a>, FxBuildHasher>,
pub(crate) macros: HashMap<&'a str, &'a Macro<'a>, FxBuildHasher>,
pub(crate) imports: HashMap<&'a str, Arc<Path>, FxBuildHasher>,
pub(crate) path: Option<&'a Path>,
pub(crate) parsed: &'a Parsed,
}
Expand All @@ -53,9 +54,9 @@ impl Context<'_> {
Context {
nodes: &[],
extends: None,
blocks: HashMap::new(),
macros: HashMap::new(),
imports: HashMap::new(),
blocks: HashMap::default(),
macros: HashMap::default(),
imports: HashMap::default(),
path: None,
parsed,
}
Expand All @@ -67,9 +68,9 @@ impl Context<'_> {
parsed: &'n Parsed,
) -> Result<Context<'n>, CompileError> {
let mut extends = None;
let mut blocks = HashMap::new();
let mut macros = HashMap::new();
let mut imports = HashMap::new();
let mut blocks = HashMap::default();
let mut macros = HashMap::default();
let mut imports = HashMap::default();
let mut nested = vec![parsed.nodes()];
let mut top = true;

Expand Down
7 changes: 4 additions & 3 deletions rinja_derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use mime::Mime;
use once_map::OnceMap;
use parser::{Node, Parsed};
use proc_macro2::Span;
use rustc_hash::FxBuildHasher;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;

Expand Down Expand Up @@ -144,7 +145,7 @@ impl TemplateInput<'_> {

pub(crate) fn find_used_templates(
&self,
map: &mut HashMap<Arc<Path>, Arc<Parsed>>,
map: &mut HashMap<Arc<Path>, Arc<Parsed>, FxBuildHasher>,
) -> Result<(), CompileError> {
let (source, source_path) = match &self.source {
Source::Source(s) => (s.clone(), None),
Expand Down Expand Up @@ -571,9 +572,9 @@ pub(crate) fn get_template_source(
tpl_path: &Arc<Path>,
import_from: Option<(&Arc<Path>, &str, &str)>,
) -> Result<Arc<str>, CompileError> {
static CACHE: OnceLock<OnceMap<Arc<Path>, Arc<str>>> = OnceLock::new();
static CACHE: OnceLock<OnceMap<Arc<Path>, Arc<str>, FxBuildHasher>> = OnceLock::new();

CACHE.get_or_init(OnceMap::new).get_or_try_insert_ref(
CACHE.get_or_init(OnceMap::default).get_or_try_insert_ref(
tpl_path,
(),
Arc::clone,
Expand Down
6 changes: 3 additions & 3 deletions rinja_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn build_skeleton(ast: &syn::DeriveInput) -> Result<String, CompileError> {
let template_args = TemplateArgs::fallback();
let config = Config::new("", None, None, None)?;
let input = TemplateInput::new(ast, config, &template_args)?;
let mut contexts = HashMap::new();
let mut contexts = HashMap::default();
let parsed = parser::Parsed::default();
contexts.insert(&input.path, Context::empty(&parsed));
Generator::new(
Expand Down Expand Up @@ -168,10 +168,10 @@ fn build_template_inner(
)?;
let input = TemplateInput::new(ast, config, template_args)?;

let mut templates = HashMap::new();
let mut templates = HashMap::default();
input.find_used_templates(&mut templates)?;

let mut contexts = HashMap::new();
let mut contexts = HashMap::default();
for (path, parsed) in &templates {
contexts.insert(path, Context::new(input.config, path, parsed)?);
}
Expand Down
3 changes: 2 additions & 1 deletion rinja_derive_standalone/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ serde = { version = "1.0", optional = true, features = ["derive"] }
memchr = "2"
mime = "0.3"
mime_guess = "2"
once_map = "0.4.18"
once_map = { version = "0.4.18", default-features = false, features = ["std"] }
proc-macro2 = "1"
quote = "1"
rustc-hash = "2.0.0"
syn = "2"

[dev-dependencies]
Expand Down
Loading