Skip to content
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
2 changes: 2 additions & 0 deletions changelog.d/11205-fact-write-no-clone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- section: Changed -->
- Cold `ripr check` file-fact cache writes no longer deep-clone each file's parsed facts and cache key before encoding; the entry bytes and integrity digest are unchanged (#11205).
78 changes: 70 additions & 8 deletions crates/ripr/src/analysis/seam_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2976,11 +2976,15 @@ impl RepoFileFactCache {
stage: FileFactStoreStage::CreateDir,
message: format!("create file fact cache dir failed: {err}"),
})?;
let envelope = FileFactCacheEnvelope::new(key.clone(), facts.clone());
let bytes = codec::encode_file_facts(&envelope).map_err(|message| FileFactStoreError {
stage: FileFactStoreStage::Encode,
message,
})?;
// Borrow the key and facts: the owned envelope deep-cloned the whole
// `FileFacts` (and four key strings) for every cold write only to
// serialize it once for the digest and once for the entry.
let envelope = FileFactCacheEnvelopeRef::new(key, facts);
let bytes =
codec::encode_file_facts_ref(&envelope).map_err(|message| FileFactStoreError {
stage: FileFactStoreStage::Encode,
message,
})?;
crate::atomic_file::write_cache(&self.entry_path(key), &bytes, "file fact cache").map_err(
|message| FileFactStoreError {
stage: FileFactStoreStage::Write,
Expand Down Expand Up @@ -3111,6 +3115,36 @@ struct FileFactCacheEnvelope {
file_facts: FileFacts,
}

/// Borrowing write-side twin of [`FileFactCacheEnvelope`]. Field names, order
/// and the omitted `payload_digest` match the owned envelope exactly, so the
/// digest and the entry bytes are identical; it only avoids cloning the facts.
#[derive(serde::Serialize)]
struct FileFactCacheEnvelopeRef<'a> {
file_fact_cache_schema_version: &'a str,
analyzer_version: &'a str,
file_path: &'a Path,
content_hash: &'a str,
file_facts: &'a FileFacts,
}

impl<'a> FileFactCacheEnvelopeRef<'a> {
fn new(key: &'a RepoFileFactCacheKey, file_facts: &'a FileFacts) -> Self {
Self {
file_fact_cache_schema_version: &key.schema_version,
analyzer_version: &key.analyzer_version,
file_path: &key.file_path,
content_hash: &key.content_hash,
file_facts,
}
}

fn expected_digest(&self) -> Result<String, String> {
semantic_body_digest(FILE_FACT_ENVELOPE_DIGEST_DOMAIN, self)
}
}

const FILE_FACT_ENVELOPE_DIGEST_DOMAIN: &str = "ripr-cache:FileFactCacheEnvelope:v1";

/// The envelope fields the inventory needs; serde skips the rest.
#[derive(serde::Deserialize)]
struct FileFactCacheHeader {
Expand Down Expand Up @@ -3222,6 +3256,7 @@ impl KnownFilePaths {
}

impl FileFactCacheEnvelope {
#[cfg(test)]
fn new(key: RepoFileFactCacheKey, file_facts: FileFacts) -> Self {
Self {
payload_digest: None,
Expand Down Expand Up @@ -3655,7 +3690,7 @@ impl FileFactCacheEnvelope {
/// Unsigned typed body binds identity and all serialized served fields in fixed order.
/// Nested serde-skipped derived state is excluded by the existing serialization contract.
fn expected_digest(&self) -> Result<String, String> {
semantic_body_digest("ripr-cache:FileFactCacheEnvelope:v1", self)
semantic_body_digest(FILE_FACT_ENVELOPE_DIGEST_DOMAIN, self)
}
fn validate_integrity(&self) -> Result<(), String> {
let expected = self.expected_digest()?;
Expand Down Expand Up @@ -3724,8 +3759,8 @@ mod codec {
#[cfg(test)]
use super::CountCacheEnvelope;
use super::{
CacheEnvelope, CorpusFingerprintEnvelope, FileFactCacheEnvelope, ShardedCacheEnvelope,
ShardedCacheManifest,
CacheEnvelope, CorpusFingerprintEnvelope, FileFactCacheEnvelope, FileFactCacheEnvelopeRef,
ShardedCacheEnvelope, ShardedCacheManifest,
};

#[cfg(test)]
Expand Down Expand Up @@ -3769,10 +3804,17 @@ mod codec {
serde_json::from_slice(bytes).map_err(|err| format!("decode counts failed: {err}"))
}

#[cfg(test)]
pub(super) fn encode_file_facts(envelope: &FileFactCacheEnvelope) -> Result<Vec<u8>, String> {
super::encode_integrity_body(envelope, envelope.expected_digest()?)
}

pub(super) fn encode_file_facts_ref(
envelope: &FileFactCacheEnvelopeRef<'_>,
) -> Result<Vec<u8>, String> {
super::encode_integrity_body(envelope, envelope.expected_digest()?)
}

pub(super) fn decode_file_facts(bytes: &[u8]) -> Result<FileFactCacheEnvelope, String> {
serde_json::from_slice(bytes).map_err(|err| format!("decode file facts failed: {err}"))
}
Expand Down Expand Up @@ -5512,6 +5554,26 @@ mod tests {
Ok(())
}

#[test]
fn store_file_facts_writes_the_owned_envelope_bytes() -> Result<(), String> {
use crate::analysis::syntax::{RaRustSyntaxAdapter, RustSyntaxAdapter};
// The store path borrows the key and facts instead of cloning them
// into an owned envelope; the entry on disk must stay byte-identical
// to what the owned envelope encodes.
let scratch = integrity_scratch("store-owned-bytes")?;
let cache = RepoFileFactCache::at_dir(scratch.0.clone());
let file = Path::new("src/lib.rs");
let source = "pub fn f(x: u32) -> u32 { if x > 0 { x + 1 } else { 0 } }\n\
#[test] fn t() { assert_eq!(f(1), 2); }\n";
let facts = RaRustSyntaxAdapter.summarize_file(file, source)?;
let key = RepoFileFactCacheKey::new(file, source.as_bytes());
cache.store_file_facts(&key, &facts)?;
let owned = FileFactCacheEnvelope::new(key.clone(), facts);
let on_disk = std::fs::read(cache.entry_path(&key)).map_err(|err| err.to_string())?;
assert_eq!(on_disk, codec::encode_file_facts(&owned)?);
Ok(())
}

#[test]
fn file_fact_entry_with_unknown_probe_shape_kind_loads_corrupt_ignored() -> Result<(), String> {
use crate::analysis::syntax::{RaRustSyntaxAdapter, RustSyntaxAdapter};
Expand Down
11 changes: 9 additions & 2 deletions crates/ripr/src/analysis/seam_cache/digest_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,15 @@ fn real_file_facts_keep_original_cache_bytes_and_integrity() -> Result<(), Strin
let facts = crate::analysis::syntax::RaRustSyntaxAdapter.summarize_file(path, &source)?;
assert_eq!(facts.functions.len(), 128);
assert_eq!(facts.tests.len(), 64);
let envelope =
FileFactCacheEnvelope::new(RepoFileFactCacheKey::new(path, source.as_bytes()), facts);
let key = RepoFileFactCacheKey::new(path, source.as_bytes());
// The borrowing write path must emit the owned envelope's exact digest
// and bytes without cloning the facts.
let borrowed = FileFactCacheEnvelopeRef::new(&key, &facts);
let borrowed_bytes = codec::encode_file_facts_ref(&borrowed)?;
let borrowed_digest = borrowed.expected_digest()?;
let envelope = FileFactCacheEnvelope::new(key, facts);
assert_eq!(borrowed_digest, envelope.expected_digest()?);
assert_eq!(borrowed_bytes, codec::encode_file_facts(&envelope)?);
let digest = reference_digest("ripr-cache:FileFactCacheEnvelope:v1", &envelope)?;
let expected = serde_json::to_vec_pretty(&ChecksummedBody {
body: &envelope,
Expand Down
Loading