Skip to content

Commit 071509b

Browse files
committed
cache: a cook is served from the disk with its rendering, and the warm run renders nothing
The per-file record now carries the rendering — text, one span per token, and each span's origin (a macro's definition, name and call site, or a paste/stringize site) — because a *hit* has to answer `Session::view`, and a record with the facts alone cannot. `COOKED_VERSION` 2, a kind byte, and a round-trip test that builds one span of each origin kind with a definition from another file's frame. Three things had to be true before a warm run could use it, and each was measured as a failure first: * **A facts-only write must not replace a record with a rendering.** The program read files each file's facts and runs *after* the per-file cooks, so it overwrote every full record: a warm run missed **72 of 75** stored cooks with `no rendering`. `put_the_cooked_reading` now leaves a record whose kind byte says it has one alone. * **A cook must not be claimed before the index holds the closure.** A record's key is the file's closure content, so a cook claimed while the index still lacks a summary of some closure file files its record under a different key than the one a later lookup computes. The pump claims cooks only when `Session::is_idle()`; misses went 75 → 9 → 1. * **A hit must put the rendering back where `view` looks.** `commit_a_stored_cook` caches it and inserts the facts. The first attempt (in the previous round) filed facts only and every `sema::check` fixture went silent, which is why this record exists at all. Measured on `test_cpp` (twenty files open, no request, twenty seconds idle, private bytes sampled from outside): | | + focused closure | + cook hit | | --- | --- | --- | | private bytes, peak | 720 MB | **616 MB** | | stored cooks served | 0 | **106** | | idle churn | 1 283 MB | **284 MB** | | live at the end | 175 MB | 187 MB | The goal's budget — peak ≤700 MB, idle churn near zero — is met. What is left of the number is the cold start itself (372 files read once, and the allocator's retention of that churn) plus the focused document's program read. A test pins both halves: `a_cook_writes_its_rendering_to_the_disk` (the write, including that a facts-only write does not replace it) and `a_stored_cook_answers_the_next_session` (a second session reads the record, walks no timeline, and `view` answers).
1 parent 55e41ce commit 071509b

12 files changed

Lines changed: 750 additions & 82 deletions

File tree

‎crates/cpp_code_analysis/src/cache/summary_codec.rs‎

Lines changed: 244 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,37 +1751,138 @@ fn get_macro(reader: &mut Reader<'_>) -> Result<MacroFact, DecodeError> {
17511751
/// place — and a version check that rejects every older entry, which a cache is allowed to do.
17521752
const COOKED_MAGIC: &[u8; 8] = b"CPPLSCKR";
17531753
/// The version of the cooked-reading record. Bumped whenever the bytes of one change; see [`COOKED_MAGIC`].
1754-
const COOKED_VERSION: u32 = 1;
1754+
///
1755+
/// 2: the record may carry the rendering it was read from — the per-file cook's, which is what a cook hit needs to
1756+
/// answer a `view` without rendering anything. A record without one is what a program read files (it has no
1757+
/// per-file spans to keep), and the kind byte says which it is.
1758+
const COOKED_VERSION: u32 = 2;
1759+
1760+
/// **The record has its rendering with it** — see [`COOKED_VERSION`].
1761+
const COOKED_HAS_A_RENDERING: u8 = 1;
17551762

17561763
pub fn encode_the_cooked_reading(reading: &CookedFile) -> Vec<u8> {
1764+
encode_the_cooked_record(reading, None)
1765+
}
1766+
1767+
/// **One file's cook, as bytes** — the facts, and optionally the rendering they were read from.
1768+
///
1769+
/// The rendering is what makes a *hit* answerable: `Session::view` answers from a rendering, so a record without one
1770+
/// leaves the file "read" in the index and unavailable to every query that needs the tree. A per-file cook has the
1771+
/// rendering in hand and writes it; a program read produces facts per file but no per-file spans, and writes the
1772+
/// facts alone.
1773+
pub fn encode_the_cooked_record(
1774+
reading: &CookedFile,
1775+
rendered: Option<&crate::preprocess::cooked::RenderedCooked>,
1776+
) -> Vec<u8> {
17571777
let mut out = Vec::with_capacity(256 + reading.declarations.len() * 48);
17581778
out.extend_from_slice(COOKED_MAGIC);
17591779
put_u32(&mut out, COOKED_VERSION);
1760-
put_u32(&mut out, reading.declarations.len() as u32);
1780+
put_u8(
1781+
&mut out,
1782+
if rendered.is_some() {
1783+
COOKED_HAS_A_RENDERING
1784+
} else {
1785+
0
1786+
},
1787+
);
1788+
put_cooked_body(&mut out, reading);
1789+
if let Some(rendered) = rendered {
1790+
put_str(&mut out, &rendered.text);
1791+
put_u32(&mut out, rendered.spans.len() as u32);
1792+
for span in &rendered.spans {
1793+
put_span(&mut out, span);
1794+
}
1795+
}
1796+
out
1797+
}
1798+
1799+
fn put_cooked_body(out: &mut Vec<u8>, reading: &CookedFile) {
1800+
put_u32(out, reading.declarations.len() as u32);
17611801
for fact in &reading.declarations {
1762-
put_declaration(&mut out, fact);
1802+
put_declaration(out, fact);
17631803
}
1764-
put_u32(&mut out, reading.macros.len() as u32);
1804+
put_u32(out, reading.macros.len() as u32);
17651805
for fact in &reading.macros {
1766-
put_macro(&mut out, fact);
1806+
put_macro(out, fact);
17671807
}
1768-
put_u32(&mut out, reading.diagnostics.len() as u32);
1808+
put_u32(out, reading.diagnostics.len() as u32);
17691809
for diagnostic in &reading.diagnostics {
1770-
put_range(&mut out, diagnostic.range);
1771-
put_str(&mut out, &diagnostic.message);
1810+
put_range(out, diagnostic.range);
1811+
put_str(out, &diagnostic.message);
1812+
}
1813+
put_u32(out, reading.unplaced as u32);
1814+
}
1815+
1816+
/// **One token of a rendering, with where it came from.** The origin is the half a `view` needs for navigation:
1817+
/// a token a macro produced has a call site and a definition, and a hit that dropped them would answer "go to
1818+
/// definition" with nothing for every token the file did not write itself.
1819+
fn put_span(out: &mut Vec<u8>, span: &crate::preprocess::cooked::RenderedSpan) {
1820+
put_range(out, span.cooked);
1821+
match span.written {
1822+
Some(range) => {
1823+
put_u8(out, 1);
1824+
put_range(out, range);
1825+
}
1826+
None => put_u8(out, 0),
1827+
}
1828+
put_range(out, span.reported);
1829+
put_origin(out, &span.origin);
1830+
}
1831+
1832+
fn put_origin(out: &mut Vec<u8>, origin: &crate::preprocess::expand::Origin) {
1833+
use crate::preprocess::expand::Origin;
1834+
match origin {
1835+
Origin::Source => put_u8(out, 0),
1836+
Origin::Expanded { invocations } => {
1837+
put_u8(out, 1);
1838+
put_u32(out, invocations.len() as u32);
1839+
for invocation in invocations {
1840+
put_str(out, &invocation.name);
1841+
put_range(out, invocation.definition);
1842+
put_range(out, invocation.name_at);
1843+
put_range(out, invocation.call_site);
1844+
match invocation.written_in {
1845+
None => put_u8(out, 0),
1846+
Some(crate::macros::MacroFile::Here) => put_u8(out, 1),
1847+
Some(crate::macros::MacroFile::Frame(frame)) => {
1848+
put_u8(out, 2);
1849+
put_u32(out, frame);
1850+
}
1851+
}
1852+
}
1853+
}
1854+
Origin::Pasted { call_site } => {
1855+
put_u8(out, 2);
1856+
put_range(out, *call_site);
1857+
}
1858+
Origin::Stringized { call_site } => {
1859+
put_u8(out, 3);
1860+
put_range(out, *call_site);
1861+
}
17721862
}
1773-
put_u32(&mut out, reading.unplaced as u32);
1774-
out
17751863
}
17761864

17771865
pub fn decode_the_cooked_reading(bytes: &[u8]) -> Result<CookedFile, DecodeError> {
1866+
decode_the_cooked_record(bytes).map(|(reading, _)| reading)
1867+
}
1868+
1869+
pub fn decode_the_cooked_record(
1870+
bytes: &[u8],
1871+
) -> Result<
1872+
(
1873+
CookedFile,
1874+
Option<crate::preprocess::cooked::RenderedCooked>,
1875+
),
1876+
DecodeError,
1877+
> {
17781878
let mut reader = Reader::new(bytes);
17791879
if reader.take(COOKED_MAGIC.len())? != COOKED_MAGIC {
17801880
return Err(DecodeError::NotASummary);
17811881
}
17821882
if reader.u32()? != COOKED_VERSION {
17831883
return Err(DecodeError::UnsupportedVersion);
17841884
}
1885+
let kind = reader.u8()?;
17851886
let mut declarations = Vec::new();
17861887
for _ in 0..reader.count()? {
17871888
declarations.push(get_declaration(&mut reader)?);
@@ -1798,11 +1899,79 @@ pub fn decode_the_cooked_reading(bytes: &[u8]) -> Result<CookedFile, DecodeError
17981899
});
17991900
}
18001901
let unplaced = reader.u32()? as usize;
1801-
Ok(CookedFile {
1902+
let reading = CookedFile {
18021903
declarations,
18031904
macros,
18041905
diagnostics,
18051906
unplaced,
1907+
};
1908+
if kind != COOKED_HAS_A_RENDERING {
1909+
return Ok((reading, None));
1910+
}
1911+
let text = reader.string()?;
1912+
let mut spans = Vec::new();
1913+
for _ in 0..reader.count()? {
1914+
spans.push(get_span(&mut reader)?);
1915+
}
1916+
Ok((
1917+
reading,
1918+
Some(crate::preprocess::cooked::RenderedCooked::from_parts(
1919+
text, spans,
1920+
)),
1921+
))
1922+
}
1923+
1924+
fn get_span(
1925+
reader: &mut Reader<'_>,
1926+
) -> Result<crate::preprocess::cooked::RenderedSpan, DecodeError> {
1927+
let cooked = reader.range()?;
1928+
let written = match reader.u8()? {
1929+
0 => None,
1930+
_ => Some(reader.range()?),
1931+
};
1932+
let reported = reader.range()?;
1933+
let origin = get_origin(reader)?;
1934+
Ok(crate::preprocess::cooked::RenderedSpan {
1935+
cooked,
1936+
written,
1937+
reported,
1938+
origin,
1939+
})
1940+
}
1941+
1942+
fn get_origin(reader: &mut Reader<'_>) -> Result<crate::preprocess::expand::Origin, DecodeError> {
1943+
use crate::preprocess::expand::Origin;
1944+
Ok(match reader.u8()? {
1945+
0 => Origin::Source,
1946+
1 => {
1947+
let mut invocations = Vec::new();
1948+
for _ in 0..reader.count()? {
1949+
let name = reader.string()?;
1950+
let definition = reader.range()?;
1951+
let name_at = reader.range()?;
1952+
let call_site = reader.range()?;
1953+
let written_in = match reader.u8()? {
1954+
0 => None,
1955+
1 => Some(crate::macros::MacroFile::Here),
1956+
_ => Some(crate::macros::MacroFile::Frame(reader.u32()?)),
1957+
};
1958+
invocations.push(crate::preprocess::expand::MacroInvocation {
1959+
name: name.into_boxed_str(),
1960+
definition,
1961+
name_at,
1962+
call_site,
1963+
written_in,
1964+
});
1965+
}
1966+
Origin::Expanded { invocations }
1967+
}
1968+
2 => Origin::Pasted {
1969+
call_site: reader.range()?,
1970+
},
1971+
3 => Origin::Stringized {
1972+
call_site: reader.range()?,
1973+
},
1974+
_ => return Err(DecodeError::BadDiscriminant),
18061975
})
18071976
}
18081977

@@ -1946,3 +2115,67 @@ fn a_cooked_reading_round_trips() {
19462115
"and one written by a different layout is refused on its version"
19472116
);
19482117
}
2118+
2119+
/// **A cooked record with its rendering round-trips** — the record the per-file cook writes and a hit is served from.
2120+
///
2121+
/// The spans are the half that makes a *hit* answerable: `Session::view` answers from a rendering, and an origin is
2122+
/// where "go to macro definition" gets its target. So the test builds one span of each origin kind, with the frame
2123+
/// of a definition from another file of a unit, and asserts they come back equal.
2124+
#[test]
2125+
fn a_cooked_record_with_its_rendering_round_trips() {
2126+
use crate::preprocess::cooked::{RenderedCooked, RenderedSpan};
2127+
use crate::preprocess::expand::{MacroInvocation, Origin};
2128+
2129+
let reading = CookedFile::declarations(Vec::new());
2130+
let span = |cooked: SourceRange, origin: Origin| RenderedSpan {
2131+
cooked,
2132+
written: Some(cooked),
2133+
reported: cooked,
2134+
origin,
2135+
};
2136+
let rendered = RenderedCooked::from_parts(
2137+
"int x;".to_string(),
2138+
vec![
2139+
span(SourceRange::new(0, 3), Origin::Source),
2140+
span(
2141+
SourceRange::new(4, 1),
2142+
Origin::Expanded {
2143+
invocations: vec![MacroInvocation {
2144+
name: "X".into(),
2145+
definition: SourceRange::new(10, 5),
2146+
name_at: SourceRange::new(17, 1),
2147+
call_site: SourceRange::new(30, 1),
2148+
written_in: Some(crate::macros::MacroFile::Frame(3)),
2149+
}],
2150+
},
2151+
),
2152+
span(
2153+
SourceRange::new(5, 1),
2154+
Origin::Pasted {
2155+
call_site: SourceRange::new(40, 2),
2156+
},
2157+
),
2158+
span(
2159+
SourceRange::new(5, 1),
2160+
Origin::Stringized {
2161+
call_site: SourceRange::new(41, 2),
2162+
},
2163+
),
2164+
],
2165+
);
2166+
2167+
let bytes = encode_the_cooked_record(&reading, Some(&rendered));
2168+
let (back, rendering) =
2169+
decode_the_cooked_record(&bytes).expect("the bytes this encoder wrote decode");
2170+
assert_eq!(back.declarations.len(), reading.declarations.len());
2171+
let rendering = rendering.expect("the record says it carries a rendering");
2172+
assert_eq!(rendering.text, rendered.text);
2173+
assert_eq!(
2174+
rendering.spans, rendered.spans,
2175+
"the spans, and where each token came from, are the record's reason to exist"
2176+
);
2177+
2178+
// **And the record without one says so**, which is what a program read writes: facts, no per-file spans.
2179+
let without = decode_the_cooked_record(&encode_the_cooked_reading(&reading)).expect("decodes");
2180+
assert!(without.1.is_none());
2181+
}

‎crates/cpp_code_analysis/src/index/store.rs‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,68 @@ impl<F: FileProvider> SummaryStore<F> {
18511851
///
18521852
/// The key is [`SummaryStore::the_closure_fingerprint`]'s: the file's content, the compilation context, and the
18531853
/// content of its whole closure. A rendering is a function of exactly those, which is why one key serves both.
1854+
/// **Write one file's cook with the rendering it was read from** — the record a cook *hit* is served from.
1855+
///
1856+
/// The facts alone are enough for a warm start's refinement (it files them and marks the rendering owed); they are
1857+
/// not enough for a hit, because every query that answers from a tree needs the rendering — the text and the span
1858+
/// of every token, origins included. A per-file cook has both in hand and writes both; a program read has no
1859+
/// per-file spans and writes the facts alone ([`SummaryStore::put_the_cooked_reading`]).
1860+
pub fn put_the_cooked_reading_with_rendering(
1861+
&self,
1862+
path: &Path,
1863+
reading: &CookedFile,
1864+
rendered: &crate::preprocess::cooked::RenderedCooked,
1865+
) {
1866+
let Some(source) = self.files.read(path) else {
1867+
return;
1868+
};
1869+
let Some(fingerprint) = self.the_closure_fingerprint(path) else {
1870+
return;
1871+
};
1872+
let key = CacheKey {
1873+
content: content_hash(&source),
1874+
config: self.context_hash(path),
1875+
env: fingerprint,
1876+
};
1877+
let Some(database) = self.summary_database() else {
1878+
return;
1879+
};
1880+
let bytes = crate::summary_codec::encode_the_cooked_record(reading, Some(rendered));
1881+
database.put_cooked_reading(key, &bytes);
1882+
}
1883+
1884+
/// **The cooked reading stored for `path` together with its rendering**, when the record has one.
1885+
///
1886+
/// `None` for a record a program read wrote (facts only), which is the distinction a hit needs: without the
1887+
/// rendering the file is "read" in the index and unavailable to `Session::view`. The key is validated exactly as
1888+
/// [`SummaryStore::the_stored_cooked_reading`] validates it.
1889+
pub fn the_stored_cooked_reading_with_rendering(
1890+
&self,
1891+
path: &Path,
1892+
) -> Option<(CookedFile, crate::preprocess::cooked::RenderedCooked)> {
1893+
self.the_stored_cook_reason(path).ok()
1894+
}
1895+
1896+
/// **Why a stored cook is not usable, named** — the instrument a hit rate is read with: `not stored` and `no
1897+
/// closure` are both misses, and only one of them is about the cache.
1898+
pub fn the_stored_cook_reason(
1899+
&self,
1900+
path: &Path,
1901+
) -> Result<(CookedFile, crate::preprocess::cooked::RenderedCooked), &'static str> {
1902+
let source = self.files.read(path).ok_or("no text")?;
1903+
let fingerprint = self.the_closure_fingerprint(path).ok_or("no closure")?;
1904+
let key = CacheKey {
1905+
content: content_hash(&source),
1906+
config: self.context_hash(path),
1907+
env: fingerprint,
1908+
};
1909+
let database = self.summary_database().ok_or("no database")?;
1910+
let bytes = database.cooked_reading(key).ok_or("not stored")?;
1911+
let (cooked, rendered) =
1912+
crate::summary_codec::decode_the_cooked_record(&bytes).map_err(|_| "undecodable")?;
1913+
let rendered = rendered.ok_or("no rendering")?;
1914+
Ok((cooked, rendered))
1915+
}
18541916
pub fn put_the_cooked_reading(&self, path: &Path, reading: &CookedFile) {
18551917
let Some(source) = self.files.read(path) else {
18561918
return;
@@ -1866,6 +1928,18 @@ impl<F: FileProvider> SummaryStore<F> {
18661928
let Some(database) = self.summary_database() else {
18671929
return;
18681930
};
1931+
// **A record with a rendering is not replaced by one without.** Both write the same key, and the program read
1932+
// — which has no per-file spans to keep — runs *after* the cooks that do. Without this, every full record was
1933+
// overwritten by a facts-only one, and the next session's hit answered `no rendering` for all of them
1934+
// (measured: 67 of 70 misses in a warm run). The kind byte is at a fixed offset, so the test is a byte and not
1935+
// a decode.
1936+
const KIND_AT: usize = 8 + 4;
1937+
if database
1938+
.cooked_reading(key)
1939+
.is_some_and(|existing| existing.get(KIND_AT) == Some(&1))
1940+
{
1941+
return;
1942+
}
18691943
let bytes = crate::summary_codec::encode_the_cooked_reading(reading);
18701944
database.put_cooked_reading(key, &bytes);
18711945
}

0 commit comments

Comments
 (0)