perf(runtime-bundle): share destination strings with tristr

Share destination storage within bundle planning and preserve the Service wire strings and provider readback.
This commit is contained in:
Tunglies
2026-11-30 18:13:50 +08:00
parent c71b9c579d
commit 96c926c33f
4 changed files with 30 additions and 21 deletions
Generated
+1
View File
@@ -1227,6 +1227,7 @@ dependencies = [
"sha2 0.11.2",
"thiserror 2.0.20",
"tokio",
"tristr",
]
[[package]]
@@ -10,6 +10,7 @@ hex = "0.4.3"
log = { workspace = true }
serde_yaml_ng = { workspace = true }
sha2 = { workspace = true }
tristr = { workspace = true }
tokio = { workspace = true, features = ["fs"] }
[lints]
+23 -16
View File
@@ -7,6 +7,7 @@ use serde_yaml_ng::{Mapping, Value};
use sha2::{Digest as _, Sha256};
use std::collections::{BTreeMap, HashSet};
use std::path::{Component, Path, PathBuf, Prefix};
use tristr::TriStr;
pub const GEO_ASSETS: &[&str] = &[
"Country.mmdb",
@@ -22,13 +23,13 @@ pub const GEO_ASSETS: &[&str] = &[
pub struct RemoteProviderRef {
pub section: &'static str,
pub name: String,
pub destination: String,
pub destination: TriStr,
pub url: String,
}
pub struct RuntimeAsset {
pub source: String,
pub destination: String,
pub destination: TriStr,
}
pub struct RuntimeBundlePlan {
@@ -77,8 +78,8 @@ struct ProviderPathOwners {
pub fn resolve_provider_path_conflicts(config: &mut Mapping, config_root: &Path) -> Result<()> {
let config_root = std::fs::canonicalize(config_root)?;
let mut paths = BTreeMap::<String, ProviderPathOwners>::new();
let mut reserved: HashSet<String> = GEO_ASSETS.iter().map(|name| (*name).to_owned()).collect();
let mut paths = BTreeMap::<TriStr, ProviderPathOwners>::new();
let mut reserved: HashSet<TriStr> = GEO_ASSETS.iter().copied().map(TriStr::from).collect();
for section in ["proxy-providers", "rule-providers"] {
let Some(providers) = config.get(section).and_then(Value::as_mapping) else {
continue;
@@ -98,6 +99,7 @@ pub fn resolve_provider_path_conflicts(config: &mut Mapping, config_root: &Path)
let Ok(destination) = destination else {
continue;
};
let destination = TriStr::from(destination.as_str());
reserved.insert(destination.clone());
let owners = paths.entry(destination).or_default();
match (is_remote, provider.get("url").and_then(Value::as_str)) {
@@ -131,7 +133,7 @@ pub fn resolve_provider_path_conflicts(config: &mut Mapping, config_root: &Path)
Ok(())
}
fn allocate_provider_destination(destination: &str, url: &str, reserved: &mut HashSet<String>) -> String {
fn allocate_provider_destination(destination: &str, url: &str, reserved: &mut HashSet<TriStr>) -> String {
let mut digest = Sha256::new();
digest.update(destination.as_bytes());
digest.update([0]);
@@ -148,7 +150,7 @@ fn allocate_provider_destination(destination: &str, url: &str, reserved: &mut Ha
.with_file_name(format!("cvr-{digest}{suffix}{extension}"))
.to_string_lossy()
.replace('\\', "/");
if reserved.insert(candidate.clone()) {
if reserved.insert(TriStr::from(candidate.as_str())) {
return candidate;
}
sequence += 1;
@@ -181,10 +183,10 @@ fn collect_runtime_parts(mut config: Value, config_root: &Path) -> Result<Runtim
collect_ssh_private_keys(&mut config, &config_root, &mut destinations, &mut assets);
for filename in GEO_ASSETS {
let source = config_root.join(filename);
if source.is_file() && destinations.insert((*filename).to_string()) {
if source.is_file() && destinations.insert(TriStr::from(*filename)) {
assets.push(RuntimeAsset {
source: std::fs::canonicalize(&source)?.to_string_lossy().into_owned(),
destination: (*filename).to_string(),
destination: (*filename).into(),
});
}
}
@@ -202,7 +204,7 @@ fn collect_provider_assets(
config: &mut Value,
section: &'static str,
config_root: &Path,
destinations: &mut HashSet<String>,
destinations: &mut HashSet<TriStr>,
assets: &mut Vec<RuntimeAsset>,
remote_providers: &mut Vec<RemoteProviderRef>,
) -> Result<()> {
@@ -228,11 +230,12 @@ fn collect_provider_assets(
(true, Some(url)) => {
let destination = provider_destination(config_root, raw_path)?;
// Reserve remote destinations too; copied and downloaded files must not collide.
if destinations.insert(destination.clone()) {
let reserved = TriStr::from(destination.as_str());
if destinations.insert(reserved.clone()) {
remote_providers.push(RemoteProviderRef {
section,
name: name.as_str().unwrap_or_default().to_owned(),
destination: destination.clone(),
destination: reserved,
url,
});
} else {
@@ -265,10 +268,11 @@ fn collect_provider_assets(
continue;
};
let destination = destination_below_root(config_root, &source)?;
if destinations.insert(destination.clone()) {
let reserved = TriStr::from(destination.as_str());
if destinations.insert(reserved.clone()) {
assets.push(RuntimeAsset {
source: source.to_string_lossy().into_owned(),
destination: destination.clone(),
destination: reserved,
});
}
destination
@@ -283,7 +287,7 @@ fn collect_provider_assets(
fn collect_ssh_private_keys(
config: &mut Value,
config_root: &Path,
destinations: &mut HashSet<String>,
destinations: &mut HashSet<TriStr>,
assets: &mut Vec<RuntimeAsset>,
) {
let Some(config) = config.as_mapping_mut() else {
@@ -334,7 +338,7 @@ fn collect_ssh_private_keys(
let destination = allocate_provider_destination("ssh-private-key", &source, destinations);
assets.push(RuntimeAsset {
source,
destination: destination.clone(),
destination: TriStr::from(destination.as_str()),
});
proxy.insert(Value::String("private-key".to_owned()), Value::String(destination));
}
@@ -466,7 +470,10 @@ mod tests {
assert_eq!(bundle.remote_providers.len(), 2);
for declared in readback {
assert!(bundle.remote_providers.contains(&declared));
assert_eq!(config[declared.section][&declared.name]["path"], declared.destination);
assert_eq!(
config[declared.section][&declared.name]["path"],
declared.destination.as_str()
);
assert_eq!(
config[declared.section][&declared.name]["url"],
original[declared.section][&declared.name]["url"]
+5 -5
View File
@@ -619,14 +619,14 @@ async fn collect_service_runtime_bundle(config_file: &Path) -> Result<RuntimeBun
.into_iter()
.map(|asset| RuntimeAsset {
source: asset.source,
destination: asset.destination,
destination: asset.destination.into(),
})
.collect(),
remote_providers: plan
.remote_providers
.into_iter()
.map(|provider| RemoteProvider {
destination: provider.destination,
destination: provider.destination.into(),
url: provider.url,
})
.collect(),
@@ -968,7 +968,7 @@ async fn sync_runtime_providers_by_service() -> Result<ProviderSync> {
let loaded_before = provider_load_times().await;
let mut fetched = Vec::new();
for declared in applied_remote_providers(&runtime, &app_dir)? {
let (temp, file) = match create_sync_temp(&app_dir.join(&declared.destination)).await {
let (temp, file) = match create_sync_temp(&app_dir.join(declared.destination.as_str())).await {
Ok(created) => created,
Err(error) => {
logging!(warn, Type::Service, "{error:#}");
@@ -1137,7 +1137,7 @@ async fn publish_fetched(
};
let mut synced = 0;
for cache in fetched {
let target = app_dir.join(&cache.declared.destination);
let target = app_dir.join(cache.declared.destination.as_str());
if !declared.contains(&cache.declared) || same_contents(&cache.temp, &target).await {
remove_temp(&cache.temp).await;
continue;
@@ -1214,7 +1214,7 @@ async fn recheck_identity(
cache: &FetchedCache,
) -> Result<Fetch> {
let request = RuntimeFileRequest {
destination: cache.declared.destination.to_owned(),
destination: cache.declared.destination.as_str().to_owned(),
offset: cache.len,
};
Ok(match read_chunk(credentials, session, &request).await? {