perf(profile-store): reuse buffers while preparing a refresh

This commit is contained in:
Tunglies
2026-12-04 01:48:33 +08:00
parent 2cdd0dd701
commit 5edc035120
+9 -11
View File
@@ -182,7 +182,6 @@ impl Session {
let old_index = fs::read(&index_path)?;
let current: IProfiles = serde_yaml_ng::from_slice(&old_index)?;
let file = body_filename(candidate, uid)?;
let new_item = candidate.get_item(uid)?;
let mut exact = current
.items
.iter()
@@ -195,6 +194,7 @@ impl Session {
item
} else {
// Loading an old index may assign a UID without rewriting the file.
let new_item = candidate.get_item(uid)?;
let mut legacy = current.items.iter().flatten().filter(|item| {
item.uid.is_none()
&& item.itype.as_deref() == Some("remote")
@@ -231,10 +231,8 @@ impl Session {
if old_item.file.is_none() && old_body_digest.is_some() {
return Err(StoreError::ExistingBodyPath);
}
let index = format!(
"# Profiles Config for Clash Verge\n\n{}",
serde_yaml_ng::to_string(candidate)?
);
let mut index = b"# Profiles Config for Clash Verge\n\n".to_vec();
serde_yaml_ng::to_writer(&mut index, candidate)?;
let decision = Decision {
version: 1,
uid: uid.into(),
@@ -242,7 +240,7 @@ impl Session {
old_body_digest,
old_index_digest: Sha256::digest(&old_index).into(),
body_digest: Sha256::digest(body).into(),
index_digest: Sha256::digest(index.as_bytes()).into(),
index_digest: Sha256::digest(&index).into(),
};
let decision = serde_json::to_vec(&decision)?;
@@ -255,7 +253,7 @@ impl Session {
checkpoint("journal-created")?;
write_new(&journal.join(BODY_NEXT), body, metadata.map(|meta| meta.permissions()))?;
checkpoint("body-staged")?;
write_new(&journal.join(INDEX_NEXT), index.as_bytes(), Some(index_permissions))?;
write_new(&journal.join(INDEX_NEXT), &index, Some(index_permissions))?;
checkpoint("index-staged")?;
write_new(&journal.join(DECISION_BUILDING), &decision, None)?;
platform::replace(journal.join(DECISION_BUILDING), journal.join(DECISION_NEXT))?;
@@ -488,7 +486,7 @@ fn body_filename<'a>(profiles: &'a IProfiles, uid: &str) -> Result<&'a str> {
}
fn unshared_body(dir: &Path, profiles: &IProfiles, file: &str) -> Result<Option<fs::Metadata>> {
let path = dir.join(file);
let mut path = dir.join(file);
let target = optional_regular(&path)?;
let identity = target
.as_ref()
@@ -512,11 +510,11 @@ fn unshared_body(dir: &Path, profiles: &IProfiles, file: &str) -> Result<Option<
}
#[cfg(windows)]
validate_filename(other)?;
let other = dir.join(other);
match fs::metadata(&other) {
path.set_file_name(other);
match fs::metadata(&path) {
Ok(metadata) => {
if let Some(identity) = &identity
&& platform::file_identity(&other, &metadata)? == *identity
&& platform::file_identity(&path, &metadata)? == *identity
{
return Err(StoreError::AliasedBody);
}