Skip to content

Commit a079c95

Browse files
committed
refactor env use
1 parent b6fa377 commit a079c95

30 files changed

Lines changed: 1336 additions & 353 deletions

‎crates/cpp_code_analysis/src/alloc.rs‎

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,48 @@
33
//! The server installs an allocator that reports every request here; the counter is off unless
44
//! `CPPLS_TRACE_ALLOCS` is in the environment, and the switch is one relaxed load per allocation.
55
//!
6+
//! # Two numbers, and the one that matters
7+
//!
8+
//! [`total`] counts every byte ever asked for and never subtracts: it answers *"how much churn did this stage
9+
//! cause"*, and it is **not** a memory measurement. [`live`] counts what the process is holding — every
10+
//! allocation adds, every free subtracts — and [`peak`] is its high-water mark. Only the second pair says what a
11+
//! machine runs out of.
12+
//!
13+
//! The distinction is not pedantry. Every memory claim in this codebase was read off the first number, and it
14+
//! produced a story that does not survive the second one: **~380 bytes per token of parse tree** was the churn of a
15+
//! 218 KB header's parse, while the tree it actually held was 80 bytes per token and the whole reading's steady state
16+
//! was 151 (`tests/parse_memory_probe.rs`). A ceiling built on that number was regulating something that was never
17+
//! measured.
18+
//!
619
//! # Why this lives in the engine and not in the server
720
//!
8-
//! Because the question is about the engine's own phases: a wave preparation that asks for **663 MB** while it has
21+
//! Because the question is about the engine's own phases: a wave preparation that holds **663 MB** while it has
922
//! nothing to prepare (`test_cpp/src/containers.cpp`, one open file, no requests) is a question about
1023
//! `Session::prepare_wave`'s three calls, and they are private to this crate. The server's side of the same counter
11-
//! is [`cpp_ls::allocated_bytes`], which forwards here.
24+
//! is [`cpp_ls::note_allocation`], which forwards here.
1225
//!
1326
//! It is an instrument, not a statistic: nothing reads it in an answer, and with the variable unset it costs the
1427
//! branch.
1528
1629
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1730

18-
/// Bytes this process has asked the allocator for since the counter was turned on.
19-
static BYTES: AtomicUsize = AtomicUsize::new(0);
31+
/// Bytes this process has asked the allocator for since the counter was turned on — **cumulative, never reduced**.
32+
static TOTAL: AtomicUsize = AtomicUsize::new(0);
33+
34+
/// Bytes this process is **holding right now** — every allocation adds, every free subtracts.
35+
static LIVE: AtomicUsize = AtomicUsize::new(0);
36+
37+
/// The high-water mark of [`LIVE`].
38+
static PEAK: AtomicUsize = AtomicUsize::new(0);
2039

2140
/// Whether [`note`] should count.
2241
static COUNTING: AtomicBool = AtomicBool::new(false);
2342

2443
/// **Turn the counter on from `CPPLS_TRACE_ALLOCS`** — called once, while the server starts.
2544
pub fn turn_on_from_the_environment() {
26-
if std::env::var_os("CPPLS_TRACE_ALLOCS").is_some() {
45+
// The variable is read by [crate::probes::allocs], once, like every other switch — this is the *state* the
46+
// counter keeps, so a trace that is gated on it and the counter that fills it cannot disagree.
47+
if crate::probes::allocs() {
2748
COUNTING.store(true, Ordering::Relaxed);
2849
}
2950
}
@@ -32,14 +53,52 @@ pub fn turn_on_from_the_environment() {
3253
#[inline]
3354
pub fn note(bytes: usize) {
3455
if COUNTING.load(Ordering::Relaxed) {
35-
BYTES.fetch_add(bytes, Ordering::Relaxed);
56+
TOTAL.fetch_add(bytes, Ordering::Relaxed);
57+
let now = LIVE.fetch_add(bytes, Ordering::Relaxed) + bytes;
58+
PEAK.fetch_max(now, Ordering::Relaxed);
3659
}
3760
}
3861

39-
/// The counter's current value — monotone, so a stage measures a difference.
62+
/// **One free**, as the installed allocator reports it — the half that makes [`live`] a measurement rather than a
63+
/// running total.
64+
#[inline]
65+
pub fn released(bytes: usize) {
66+
if COUNTING.load(Ordering::Relaxed) {
67+
LIVE.fetch_sub(bytes, Ordering::Relaxed);
68+
}
69+
}
70+
71+
/// **One reallocation**, which is a release of the old size and an allocation of the new one.
72+
#[inline]
73+
pub fn replaced(old: usize, new: usize) {
74+
if COUNTING.load(Ordering::Relaxed) {
75+
if new >= old {
76+
let grown = new - old;
77+
TOTAL.fetch_add(grown, Ordering::Relaxed);
78+
let now = LIVE.fetch_add(grown, Ordering::Relaxed) + grown;
79+
PEAK.fetch_max(now, Ordering::Relaxed);
80+
} else {
81+
LIVE.fetch_sub(old - new, Ordering::Relaxed);
82+
}
83+
}
84+
}
85+
86+
/// **The bytes held right now** — the number a machine runs out of.
87+
#[inline]
88+
pub fn live() -> usize {
89+
LIVE.load(Ordering::Relaxed)
90+
}
91+
92+
/// The high-water mark of [`live`].
93+
#[inline]
94+
pub fn peak() -> usize {
95+
PEAK.load(Ordering::Relaxed)
96+
}
97+
98+
/// Every byte ever asked for — churn, not memory. See the module documentation for why the difference matters.
4099
#[inline]
41-
pub fn bytes() -> usize {
42-
BYTES.load(Ordering::Relaxed)
100+
pub fn total() -> usize {
101+
TOTAL.load(Ordering::Relaxed)
43102
}
44103

45104
/// Whether a run asked for the counter — what a stage's `eprintln!` is gated on.

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,8 @@ impl<'a, F: FileProvider> FileIndexer<'a, F> {
411411
// `Session::render_the_unit` is a different rendering (measured: correct for the 28-line reproducer in
412412
// `docs/buglist.md` §31, while the summary this function builds is wrong). So the one text that matters gets a
413413
// way out, off unless asked for, like this crate's other traces.
414-
if let Some(directory) = std::env::var_os(crate::probes::DUMP_RENDERING_DIR) {
415-
let directory = std::path::PathBuf::from(directory);
416-
let _ = std::fs::create_dir_all(&directory);
414+
if let Some(directory) = crate::probes::dump_rendering_dir() {
415+
let _ = std::fs::create_dir_all(directory);
417416
let name = path
418417
.file_name()
419418
.map(|name| name.to_string_lossy().into_owned())
@@ -638,9 +637,8 @@ impl<'a, F: FileProvider> FileIndexer<'a, F> {
638637
// **The whole stream, its file boundaries and its file list**, when a dump directory is named: the only way
639638
// to see which file's text a wrong scope came from — the rendering has no file markers in it, and the
640639
// boundaries are what say where one file stops.
641-
if let Some(directory) = std::env::var_os(crate::probes::DUMP_RENDERING_DIR) {
642-
let directory = std::path::PathBuf::from(directory);
643-
let _ = std::fs::create_dir_all(&directory);
640+
if let Some(directory) = crate::probes::dump_rendering_dir() {
641+
let _ = std::fs::create_dir_all(directory);
644642
let _ = std::fs::write(directory.join("unit.stream"), &stream.text);
645643
let boundaries: Vec<String> = stream
646644
.file_boundaries()
@@ -804,9 +802,8 @@ impl<'a, F: FileProvider> FileIndexer<'a, F> {
804802
// The text this summary is built from, when `CPPLS_DUMP_RENDERING_DIR` names a directory — the companion of the
805803
// same hook in [`FileIndexer::index_rendering`], and the only way to see which reading a wrong answer came
806804
// from when the file's own text parses correctly.
807-
if let Some(directory) = std::env::var_os(crate::probes::DUMP_RENDERING_DIR) {
808-
let directory = std::path::PathBuf::from(directory);
809-
let _ = std::fs::create_dir_all(&directory);
805+
if let Some(directory) = crate::probes::dump_rendering_dir() {
806+
let _ = std::fs::create_dir_all(directory);
810807
let name = path
811808
.file_name()
812809
.map(|name| name.to_string_lossy().into_owned())

‎crates/cpp_code_analysis/src/index/project/macro_walk.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,8 @@ impl ProjectIndex {
696696
.clear();
697697

698698
// The name index holds this file's *other* reading of the same id, if any, and then this one.
699-
let tracing = std::env::var("CPPLS_TRACE_NAMES").is_ok()
700-
&& (path.ends_with("stl_map.h") || path.ends_with("iostream"));
699+
let tracing =
700+
crate::probes::names() && (path.ends_with("stl_map.h") || path.ends_with("iostream"));
701701
if tracing {
702702
eprintln!(
703703
"[names] insert_a_reading {}: sequence {:?} id {id} declarations {} diagnostics {:?}",
@@ -726,7 +726,7 @@ impl ProjectIndex {
726726
}
727727

728728
// **Filed, in place of whatever that program (or the file itself) read before.**
729-
if std::env::var_os("CPPLS_TRACE_ALLOCS").is_some() {
729+
if crate::probes::allocs() {
730730
eprintln!(
731731
"[cook] inserted {path}, cooked map now {}",
732732
self.cooked.len()
@@ -1767,7 +1767,7 @@ impl ProjectIndex {
17671767
}
17681768
};
17691769

1770-
if std::env::var("CPPLS_TRACE_NAMES").is_ok()
1770+
if crate::probes::names()
17711771
&& let Narrow::Named(names) = narrow
17721772
&& names.iter().any(|name| *name == "cout" || *name == "map")
17731773
{
@@ -3414,7 +3414,7 @@ impl ProjectIndex {
34143414
const ALIAS_DEPTH: usize = 4;
34153415

34163416
let mut rewritten = name.to_string();
3417-
let tracing = std::env::var("CPPLS_TRACE_ALIAS").is_ok();
3417+
let tracing = crate::probes::alias();
34183418

34193419
for _ in 0..ALIAS_DEPTH {
34203420
let segments: Vec<&str> = rewritten.split("::").collect();

‎crates/cpp_code_analysis/src/index/project/members.rs‎

Lines changed: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ pub(crate) fn the_arguments_a_braced_initialisation_deduces(
472472
// query the deduction walks anyway, and every member of a class template carries the class's parameters
473473
// (`DeclFact::parameters` is what pairs them with a use's arguments), so the fallback is not a second guess: it is the
474474
// same field, read from a fact that is in hand.
475+
//
476+
// **Two lookups, and no walk, before the standard's own guides are consulted** — see below for why the order is the
477+
// whole of this change.
475478
let declared = index
476479
.definitions(name, path)
477480
.value()
@@ -486,6 +489,25 @@ pub(crate) fn the_arguments_a_braced_initialisation_deduces(
486489
Some(declaration) => declaration.fact.qualified_name(),
487490
None => name.clone(),
488491
};
492+
493+
// **The standard's own guides, before the walk that reads a class's members.** The library's constructors are
494+
// constrained templates (`tuple(_UElements&&...) requires …`), which no pairing of the class's parameters can read;
495+
// the guide is a fact about the standard rather than about the text, so it needs neither the declaration's
496+
// parameters nor the member list.
497+
//
498+
// **It used to sit below `members_of`, and that is the defect this fixes.** `members_of` is the one call here that
499+
// costs more than a query: measured on `std::tuple tuple{1, 2.5, std::string{"three"}}` in a nine-line file, listing
500+
// the members of a class template whose base is written in the pack (`tuple` inherits `_Tuple_impl<0, _Elements...>`,
501+
// which inherits `_Tuple_impl<1, …>`) took the server **16–20 seconds and 1–2 GB** for one inlay hint, and the
502+
// request never came back inside the client's deadline. The answer the table holds was available before the walk
503+
// started: a table that exists so that a walk is unnecessary cannot be read after it.
504+
if let Some(deduced) = the_deduction_the_standard_fixes(name, &class_as_written, &element_types)
505+
{
506+
return Some(crate::sema::types::Type::Named {
507+
name: class_as_written,
508+
arguments: deduced,
509+
});
510+
}
489511
let constructor_name = class_as_written
490512
.rsplit("::")
491513
.next()
@@ -502,16 +524,6 @@ pub(crate) fn the_arguments_a_braced_initialisation_deduces(
502524
.find(|member| !member.fact.parameters.is_empty())
503525
.map(|member| member.fact.parameters.clone())?,
504526
};
505-
// **The standard's own guides, before the constructors are walked.** The library's constructors are constrained
506-
// templates (`tuple(_UElements&&...) requires …`), which no pairing of the class's parameters can read; the guide is a
507-
// fact about the standard rather than about the text. See [`the_deduction_the_standard_fixes`].
508-
if let Some(deduced) = the_deduction_the_standard_fixes(name, &class_as_written, &element_types)
509-
{
510-
return Some(crate::sema::types::Type::Named {
511-
name: class_as_written,
512-
arguments: deduced,
513-
});
514-
}
515527

516528
if parameters.is_empty() {
517529
return None;
@@ -1397,12 +1409,23 @@ pub(crate) fn members_of_inner(
13971409
// kept `A`, so the trait was asked about the parameter instead of `allocator<int>`, matched no
13981410
// specialization, and took the primary `false_type` branch — `size_type` came from
13991411
// `_Normal_allocator_traits` where the right answer was `_Default_allocator_traits`'s.
1400-
.map(|written| {
1401-
(
1402-
substitute_a_base_clause(index, path, &with_arguments, &written),
1403-
class.to_string(),
1404-
)
1405-
})
1412+
.map(|written| substitute_a_base_clause(index, path, &with_arguments, &written))
1413+
// **A clause that is not a type is a gap, not a level** — the same rule the walk below applies, and
1414+
// it has to be applied here too: `std::tuple`'s own base is `_Tuple_impl<0, _Elements...>`, so this is
1415+
// the clause that started the unbounded walk. See [`why_a_clause_cannot_be_walked`].
1416+
.filter(
1417+
|substituted| match why_a_clause_cannot_be_walked(substituted) {
1418+
Some(reason) => {
1419+
list.unlisted.push(UnlistedBase {
1420+
spelling: substituted.clone(),
1421+
reason,
1422+
});
1423+
false
1424+
}
1425+
None => true,
1426+
},
1427+
)
1428+
.map(|substituted| (substituted, class.to_string()))
14061429
.collect(),
14071430
Known::Unknown(reason) => {
14081431
list.unlisted.push(UnlistedBase {
@@ -1432,6 +1455,13 @@ pub(crate) fn members_of_inner(
14321455
if !visited.insert(shown.clone()) {
14331456
continue;
14341457
}
1458+
// **Every level, when the walk is being read.** A member list that never returns is a walk whose *spellings*
1459+
// never repeat, and the spellings are the thing to look at: `_Tuple_impl<0, _Elements...>` is a base that
1460+
// becomes `_Tuple_impl<0 + 1, _Elements...>` one level down, which is a new type every time and therefore an
1461+
// unbounded number of levels. `CPPLS_TRACE_MEMBERS=1`.
1462+
if crate::probes::members() {
1463+
eprintln!("[members] {class} level {depth}: {shown}");
1464+
}
14351465

14361466
// **A base clause whose head is an *alias template* is not a class.** `conditional_t<…>` is a Type fact
14371467
// with a `type_of` target and no members, so `direct_members("conditional_t")` answered an **empty class**
@@ -1519,7 +1549,18 @@ pub(crate) fn members_of_inner(
15191549
.filter(|clause| {
15201550
!names_a_template_parameter(base_type_name(clause), &parameters)
15211551
})
1522-
.map(|clause| (clause, base.clone())),
1552+
// **The same rule as the first level** — see [`why_a_clause_cannot_be_walked`], which is
1553+
// where the measurement that makes this necessary is written down.
1554+
.filter_map(|clause| match why_a_clause_cannot_be_walked(&clause) {
1555+
Some(reason) => {
1556+
list.unlisted.push(UnlistedBase {
1557+
spelling: clause,
1558+
reason,
1559+
});
1560+
None
1561+
}
1562+
None => Some((clause, base.clone())),
1563+
}),
15231564
),
15241565
Known::Unknown(reason) => list.unlisted.push(UnlistedBase {
15251566
spelling: base,
@@ -1611,6 +1652,45 @@ pub(crate) fn the_base_clauses_of(
16111652
bases_of(index, scopes, root, path, class)
16121653
}
16131654

1655+
/// **Why a substituted base clause cannot be walked into** — the two shapes substitution produces that are not types.
1656+
///
1657+
/// The base walk terminates on a **repeated spelling**: a level is skipped when `visited` already holds it. That is a
1658+
/// proof of termination only if the spellings a level can produce are finite, and substitution can produce an *unbounded
1659+
/// family* of them:
1660+
///
1661+
/// ```text
1662+
/// level 1 std::_Tuple_impl<0, _Elements...>
1663+
/// level 2 std::_Tuple_impl<0 + 1, _Tail...>
1664+
/// level 3 std::_Tuple_impl<0 + 1 + 1, _Tail...>
1665+
/// level 4 std::_Tuple_impl<0 + 1 + 1 + 1, _Tail...> …
1666+
/// ```
1667+
///
1668+
/// — the log of `members_of("std::tuple")` on `test_cpp/src/containers.cpp`, one `_Head_base<0 + 1 + 1, _Head>` beside
1669+
/// each level. The two reasons the walk cannot stop are also the two reasons it should never have started:
1670+
///
1671+
/// * **`_Elements...` is a pack with nothing to expand it.** The clause names a type per argument, and a class asked
1672+
/// about **without** its arguments has none: no level of this walk will ever supply them, so the clause is unwritable
1673+
/// at every depth.
1674+
/// * **`0 + 1` is an expression where a type belongs.** `_Idx + 1` was substituted as *text*, because this layer does
1675+
/// not constant-fold, and each level then writes one more `+ 1` into the spelling. (Substitution joins with spaces, so
1676+
/// the operator is spelled ` + `; no *type* argument has one.)
1677+
///
1678+
/// Both are recorded as a gap — [`UnlistedBase`] is the field that exists to say where an answer stops — instead of
1679+
/// being walked. This is not a bound on the work: it is the observation that the clause names nothing, so there is no
1680+
/// level to enter. Measured before it: `inlayHint` on a nine-line file containing `std::tuple tuple{1, 2.5, s}` never
1681+
/// returned (120 s, the client's deadline) and the server reached **2 GB**; `members_of("std::tuple")` was still walking
1682+
/// **ten minutes** in with **843 MB** held and no answer.
1683+
fn why_a_clause_cannot_be_walked(clause: &str) -> Option<UnknownReason> {
1684+
if clause.contains("...") {
1685+
return Some(UnknownReason::UnknownType(Box::from(clause)));
1686+
}
1687+
const OPERATORS: [&str; 6] = [" + ", " - ", " * ", " / ", " % ", " << "];
1688+
if OPERATORS.iter().any(|operator| clause.contains(operator)) {
1689+
return Some(UnknownReason::UnknownType(Box::from(clause)));
1690+
}
1691+
None
1692+
}
1693+
16141694
/// **A base clause with the current level's arguments substituted into it** — `_Vector_base<_Tp, _Alloc>` asked of
16151695
/// `vector<int>` is `_Vector_base<int, allocator<int>>`.
16161696
///

‎crates/cpp_code_analysis/src/inlay.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,22 @@ where
363363
// **Where a hint's milliseconds go**, when asked. One line per call separates them, and a
364364
// repeated callee spelling is visible in it.
365365
let trace = crate::probes::inlay();
366+
// **Printed before the resolution, not only after it.** A call that never finishes never reaches the report
367+
// below — measured: `inlayHint` on `containers.cpp` sat in one call for sixteen seconds and a gigabyte, and the
368+
// last line the trace had printed was the call *before* it. This line is what names it.
369+
if trace {
370+
eprintln!(
371+
"[inlay] resolving {}",
372+
call.text()
373+
.to_string()
374+
.lines()
375+
.next()
376+
.unwrap_or_default()
377+
.chars()
378+
.take(60)
379+
.collect::<String>()
380+
);
381+
}
366382
let started = std::time::Instant::now();
367383
// **The parameters of the call's declaration** — a function's own, or, when the callee names a **class**, the
368384
// constructor's. `Widget(3, 4)` is a temporary of `Widget`: the class's scope holds fields, but the class also

0 commit comments

Comments
 (0)