Skip to content

Commit

Permalink
rwlock cache
Browse files Browse the repository at this point in the history
  • Loading branch information
CPunisher committed Dec 23, 2024
1 parent 0801a52 commit 9683283
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@ impl TryFrom<RawRuleSetCondition> for rspack_core::RuleSetConditionMatch {
type Error = rspack_error::Error;

fn try_from(x: RawRuleSetCondition) -> rspack_error::Result<Self> {
Ok(Self {
condition: x.try_into()?,
match_when_empty: None,
})
Ok(Self::new(x.try_into()?))
}
}

Expand Down
28 changes: 25 additions & 3 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rspack_macros::MergeFrom;
use rspack_regex::RspackRegex;
use rspack_util::{try_all, try_any, MergeFrom};
use rustc_hash::FxHashMap as HashMap;
use tokio::sync::RwLock;

use crate::{Compilation, Filename, Module, ModuleType, PublicPath, Resolve};

Expand Down Expand Up @@ -560,8 +561,8 @@ impl Default for CssExportsConvention {
}
}

pub type DescriptionData = HashMap<String, RuleSetCondition>;
pub type With = HashMap<String, RuleSetCondition>;
pub type DescriptionData = HashMap<String, RuleSetConditionMatch>;
pub type With = HashMap<String, RuleSetConditionMatch>;

pub type RuleSetConditionFnMatcher =
Box<dyn Fn(DataRef) -> BoxFuture<'static, Result<bool>> + Sync + Send>;
Expand Down Expand Up @@ -655,13 +656,34 @@ impl RuleSetCondition {
#[derive(Debug)]
pub struct RuleSetConditionMatch {
pub condition: RuleSetCondition,
pub match_when_empty: Option<bool>,
match_when_empty: RwLock<Option<bool>>,
}

impl RuleSetConditionMatch {
pub fn new(condition: RuleSetCondition) -> Self {
Self {
condition,
match_when_empty: RwLock::new(None),
}
}

pub async fn try_match(&self, data: DataRef<'_>) -> Result<bool> {
self.condition.try_match(data).await
}

pub async fn match_when_empty(&self) -> Result<bool> {
if let Some(match_when_empty) = self.match_when_empty.read().await.as_ref() {
return Ok(*match_when_empty);
}

Ok(
*self
.match_when_empty
.write()
.await
.insert(self.condition.match_when_empty().await?),
)
}
}

#[derive(Debug, Default)]
Expand Down

0 comments on commit 9683283

Please sign in to comment.