Skip to content

Commit 1c7f68d

Browse files
committed
index: a name nothing defines is not a macro, and finding that out is two lookups
The hover's second, found by timing its stages: `macro_at_a_file_offset` took 1 016 ms of a 1 027 ms hover. It asks `macro_environment(name, file)`, which walks every file the cursor's file can reach -- 1 063 of them here -- and it is asked about *every* name a reader points at: a local, a member, a class, a std:: type. Almost none of them is a macro. The index already files every `#define`/`#undef` by name, and the compilation's own table holds the `-D`s and the compiler's predefines -- the only macros no file writes. A name neither of them mentions is not a macro in any file, and the walk can only return nothing: two lookups answer it, without the closure. Measured, E:\EmmyLuaCodeStyle, LSPHandle.cpp, index settled: before after hover, cold 1 027 ms 10.2 ms hover, after edit 1 057 ms 10.3 ms hover, warm 20 ms 11 ms The one answer this can be wrong about is a macro written in a file the index has not read yet -- it says "not a macro here" until that file is read, which is the answer the walk would give for an unread file too, and the name questions after it still answer. 168/170 suites green; clippy clean.
1 parent f5a7d30 commit 1c7f68d

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

‎crates/cpp_code_analysis/src/include/graph.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,15 @@ impl Marked {
11691169
self.mentioned.contains(name)
11701170
}
11711171

1172+
/// **Is this name one the compilation itself says something about** — a `-D`, a `-U`, or a compiler predefine?
1173+
///
1174+
/// The compiler's own table is the only place a macro exists that no file writes, so this is what a caller needs
1175+
/// to decide that a name *no file defines* cannot be a macro at all: the two questions together are two index
1176+
/// lookups, where asking the include closure is a walk of every file the cursor's file can reach.
1177+
pub fn names_the_compilation(&self, name: &str) -> bool {
1178+
self.mentions(name)
1179+
}
1180+
11721181
/// Is the name one this state passed without being able to decide?
11731182
pub fn is_uncertain(&self, name: &str) -> bool {
11741183
self.uncertain.iter().any(|held| &**held == name)

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15667,6 +15667,23 @@ impl ProjectIndex {
1566715667
/// The cost is one traversal of the reachable files per caller, so a caller that asks about one position should
1566815668
/// use [`ProjectIndex::macro_definition`] and one that asks about many should hold this.
1566915669
pub fn macro_environment(&self, name: &str, visible_from: &Path) -> MacroEnvironment {
15670+
// **A name nothing defines cannot be a macro, and that is the answer most hovers want.** This walk is a
15671+
// traversal of every file the cursor can reach — **1.0 s** on `EmmyLuaCodeStyle` for `LSPHandle.cpp`'s
15672+
// standard-library closure — and a hover asks it about *every* name: a local, a member, a class, a `std::`
15673+
// type. The index already holds each file's `#define`/`#undef` by name, and the compilation's own table
15674+
// (the `-D`s and the compiler's predefines) holds the rest, so a name neither of them mentions is not a macro
15675+
// in any file and the walk can only return nothing. Two lookups instead of a closure walk.
15676+
//
15677+
// **A file the index has not read yet is the one case this can be wrong about**, and it degrades rather than
15678+
// lies: the answer is "no macro here", which is the same answer the walk would give for an unread file, and the
15679+
// name questions below it still answer. The macro the unread file defines comes back when the index reaches it
15680+
// — the memo is dropped for the file it arrives from.
15681+
if self.names.definers_of(name).is_empty() && !self.macros.names_the_compilation(name) {
15682+
return MacroEnvironment {
15683+
name: name.to_string(),
15684+
candidates: Vec::new(),
15685+
};
15686+
}
1567015687
// **The walk is remembered, because it is the whole cost and it is asked again.** Measured on neovim:
1567115688
// 1.19 s per walk, three identical walks in a row, and every repeated question paid it again — see
1567215689
// [`ProjectIndex::macro_environments`] for the numbers and for the invalidation rule.

‎crates/cpp_ls/src/handlers/hover/mod.rs‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,31 @@ pub fn hover_text(
263263
})
264264
}
265265

266+
/// **One call, timed, and said out loud when it is slow.** The stage names are the only way to tell "the query" from
267+
/// what the query asked: a hover's own second has been attributed to the pump, the cook and the locks in earlier
268+
/// rounds, and each time it was one of these calls — so the next attribution comes from a name, not a theory.
269+
macro_rules! timed {
270+
($name:expr, $call:expr) => {{
271+
let started = std::time::Instant::now();
272+
let value = $call;
273+
let ms = started.elapsed().as_millis();
274+
if ms >= 15 {
275+
log::warn!("hover: stage {} took {ms} ms", $name);
276+
}
277+
value
278+
}};
279+
}
280+
266281
pub fn hover(
267282
session: &Session<DiskFiles>,
268283
view: &FileView,
269284
offset: cpp_code_analysis::file::coordinates::ReadingOffset,
270285
in_the_file: cpp_code_analysis::file::coordinates::FileOffset,
271286
) -> Option<Hover> {
272-
if let Some(found) = hover_of_the_program(session, view, offset, in_the_file) {
287+
if let Some(found) = timed!(
288+
"program",
289+
hover_of_the_program(session, view, offset, in_the_file)
290+
) {
273291
return Some(found);
274292
}
275293

@@ -361,15 +379,18 @@ fn hover_of_the_program(
361379
// popup is the resolved path, which is the one thing a reader cannot see from the line they wrote — the header
362380
// is found by a search, and *which* of the four thousand files on this machine's search path it found is the
363381
// answer to the question the line asks.
364-
if let Known::Yes(header) = session.header_at(view, in_the_file) {
382+
if let Known::Yes(header) = timed!("header_at", session.header_at(view, in_the_file)) {
365383
return Some(markdown(header_markdown(&header)));
366384
}
367385

368386
// **And the format string**, which is the other thing a reader points at and the index cannot answer: `%zu` is a
369387
// `size_t` in decimal and `{:#x}` is hex with a prefix, and both are inside a literal that no declaration
370388
// describes. Asked here — after the header, before every name question — because a string literal is not a name
371389
// and the queries below would spend their time finding that out.
372-
if let Some(found) = crate::handlers::format::format_string_hover(session, view, in_the_file) {
390+
if let Some(found) = timed!(
391+
"format_string",
392+
crate::handlers::format::format_string_hover(session, view, in_the_file)
393+
) {
373394
return Some(found);
374395
}
375396

@@ -386,7 +407,10 @@ fn hover_of_the_program(
386407
// **The macro question, asked in the file's coordinates.** A macro use has no footprint in the rendering — the cook
387408
// expanded it — so the query takes the file offset the client gave us. Measured: a hover on a file's own
388409
// `#define MAX(a, b)` answered nothing, because the reading offset named a position the file does not have.
389-
if let Known::Yes(found) = session.macro_at_a_file_offset(view, in_the_file) {
410+
if let Known::Yes(found) = timed!(
411+
"macro_at_a_file_offset",
412+
session.macro_at_a_file_offset(view, in_the_file)
413+
) {
390414
return Some(markdown(macro_markdown(
391415
session,
392416
view,
@@ -400,7 +424,7 @@ fn hover_of_the_program(
400424
// `<gtest/gtest.h>` writes `#define TEST(a, b) …` behind `#if !GTEST_DONT_DEFINE_TEST`, and the file that uses
401425
// `TEST(Format, LocalStatement)` plainly has it. The popup says what the name is and that its existence depends on
402426
// the compilation, rather than saying nothing at all (which is what an editor reported: no hover on `TEST`).
403-
match session.macro_definition(view, offset) {
427+
match timed!("macro_definition", session.macro_definition(view, offset)) {
404428
Known::Yes(found) => {
405429
return Some(markdown(macro_markdown(
406430
session,

0 commit comments

Comments
 (0)