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

perf(mem): optimise domain set mem footprint #620

Merged
merged 9 commits into from
Oct 12, 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
8 changes: 4 additions & 4 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions clash_lib/src/app/dns/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct FallbackFilter {
pub domain: Vec<String>,
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct DoHConfig {
pub addr: SocketAddr,
Expand All @@ -54,7 +54,7 @@ pub struct DoHConfig {
pub hostname: Option<String>,
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct DoH3Config {
pub addr: SocketAddr,
Expand All @@ -63,7 +63,7 @@ pub struct DoH3Config {
pub hostname: Option<String>,
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct DoTConfig {
pub addr: SocketAddr,
Expand All @@ -74,7 +74,7 @@ pub struct DoTConfig {
pub type DnsServerKey = Option<String>;
pub type DnsServerCert = Option<String>;

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct DNSListenAddr {
pub udp: Option<SocketAddr>,
pub tcp: Option<SocketAddr>,
Expand Down
4 changes: 2 additions & 2 deletions clash_lib/src/app/dns/resolver/enhanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl EnhancedResolver {
}

pub async fn new(
cfg: &Config,
cfg: Config,
store: ThreadSafeCacheFile,
mmdb: Arc<Mmdb>,
) -> Self {
Expand All @@ -110,7 +110,7 @@ impl EnhancedResolver {
Some(default_resolver.clone()),
)
.await,
hosts: cfg.hosts.clone(),
hosts: cfg.hosts,
fallback: if !cfg.fallback.is_empty() {
Some(
make_clients(
Expand Down
2 changes: 1 addition & 1 deletion clash_lib/src/app/dns/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{app::profile::ThreadSafeCacheFile, common::mmdb::Mmdb};
use super::{Config, ThreadSafeDNSResolver};

pub async fn new(
cfg: &Config,
cfg: Config,
store: Option<ThreadSafeCacheFile>,
mmdb: Option<Arc<Mmdb>>,
) -> ThreadSafeDNSResolver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use crate::{
},
router::{map_rule_type, RuleMatcher},
},
common::{errors::map_io_error, geodata::GeoData, mmdb::Mmdb, trie},
common::{
errors::map_io_error, geodata::GeoData, mmdb::Mmdb, succinct_set, trie,
},
config::internal::rule::RuleType,
session::Session,
Error,
Expand Down Expand Up @@ -52,7 +54,8 @@ impl Display for RuleSetBehavior {
}

enum RuleContent {
Domain(trie::StringTrie<bool>),
// the left will converted into a right
Domain(succinct_set::DomainSet),
Ipcidr(Box<CidrTrie>),
Classical(Vec<Box<dyn RuleMatcher>>),
}
Expand Down Expand Up @@ -91,7 +94,7 @@ impl RuleProviderImpl {
let inner = Arc::new(tokio::sync::RwLock::new(Inner {
content: match behovior {
RuleSetBehavior::Domain => {
RuleContent::Domain(trie::StringTrie::new())
RuleContent::Domain(succinct_set::DomainSet::default())
}
RuleSetBehavior::Ipcidr => {
RuleContent::Ipcidr(Box::new(CidrTrie::new()))
Expand Down Expand Up @@ -150,9 +153,7 @@ impl RuleProvider for RuleProviderImpl {

match inner {
Ok(inner) => match &inner.content {
RuleContent::Domain(trie) => {
trie.search(&sess.destination.host()).is_some()
}
RuleContent::Domain(set) => set.has(&sess.destination.host()),
RuleContent::Ipcidr(trie) => trie.contains(
sess.destination
.ip()
Expand Down Expand Up @@ -243,7 +244,8 @@ fn make_rules(
) -> Result<RuleContent, Error> {
match behavior {
RuleSetBehavior::Domain => {
Ok(RuleContent::Domain(make_domain_rules(rules)?))
let s = make_domain_rules(rules)?;
Ok(RuleContent::Domain(s.into()))
}
RuleSetBehavior::Ipcidr => {
Ok(RuleContent::Ipcidr(Box::new(make_ip_cidr_rules(rules)?)))
Expand Down
1 change: 1 addition & 0 deletions clash_lib/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod geodata;
pub mod http;
pub mod io;
pub mod mmdb;
pub mod succinct_set;
pub mod timed_future;
pub mod tls;
pub mod trie;
Expand Down
Loading
Loading