From 822b554d43ece90119dcd6b291fd03a956861c6d Mon Sep 17 00:00:00 2001 From: "Giuseppe.Esposito" Date: Thu, 12 Nov 2026 13:37:48 +0100 Subject: [PATCH] Build the container index once per configurator being confirmed Building the container index confirms the configurator files, and a lookup made while confirming one asks for the index again. Nothing built meanwhile is kept, so every such lookup built the whole index again, nested as deep as the files being confirmed. Keep each index a thread builds while confirming files, keyed by those files, and reuse it for the next lookup made while the same files are being confirmed: the answers stay the same, and the index is built once per set of files being confirmed instead of once per lookup. Assisted-by: claude-code/claude-opus-5-5 --- docs/CHANGELOG.md | 1 + src/blade/typed_receiver.rs | 12 ++- src/symfony/container.rs | 142 +++++++++++++++++++++++++++++++++++- 3 files changed, 153 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 1063c0a81..b2c8a1b63 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -238,6 +238,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **Less memory for indexed classes.** The methods, properties and constants of every indexed class now take only the memory they need. On a large Laravel project this cuts memory after startup by about 5%. Contributed by @vishnub1626. - **Faster startup on Symfony and Drupal projects.** Working out which class a service ID or the value of a container parameter refers to no longer reads through every service definition in the project each time. Starting up on a Drupal site takes about 12% less CPU time. - **Faster go-to-implementation on library classes.** Finding the implementations of a class or interface from a Composer package now reads only the files that could hold one, instead of loading every class the project depends on. +- **Faster service lookups after an edit.** In a workspace with Symfony PHP configuration, the first service or parameter lookup after an edit that changes a method signature is faster. In a workspace holding a Symfony app and a Drupal site, going to the definition of a plugin ID after such an edit takes about a second instead of three. Contributed by @giuseppe-esposito-87. ### Fixed diff --git a/src/blade/typed_receiver.rs b/src/blade/typed_receiver.rs index 81b5532e4..f58323d9e 100644 --- a/src/blade/typed_receiver.rs +++ b/src/blade/typed_receiver.rs @@ -514,10 +514,19 @@ thread_local! { static PARTIAL_ANSWERS: std::cell::Cell = const { std::cell::Cell::new(0) }; } -fn note_partial_answer() { +pub(crate) fn note_partial_answer() { PARTIAL_ANSWERS.with(|count| count.set(count.get() + 1)); } +/// The files whose sites are being confirmed on this thread, sorted: the +/// ones [`Backend::typed_receiver_spans_for`] answers for with no spans. +pub(crate) fn confirming_files() -> Vec { + let mut files: Vec = + CONFIRMING.with(|confirming| confirming.borrow().iter().cloned().collect()); + files.sort_unstable(); + files +} + /// A count that moves whenever [`Backend::typed_receiver_spans_for`] /// answers on this thread with no spans for a file it could not confirm /// (one already being confirmed further up, or whose text no longer @@ -547,6 +556,7 @@ impl ConfirmingGuard { impl Drop for ConfirmingGuard { fn drop(&mut self) { CONFIRMING.with(|confirming| confirming.borrow_mut().remove(&self.uri)); + crate::symfony::container::forget_builds_confirming(&self.uri); } } diff --git a/src/symfony/container.rs b/src/symfony/container.rs index 452417392..35602020a 100644 --- a/src/symfony/container.rs +++ b/src/symfony/container.rs @@ -344,11 +344,38 @@ impl Backend { { return Arc::clone(index); } + // A lookup made while confirming a file's sites asks for the index + // again, for every lookup the file makes, and so does each file the + // build confirms in turn. The files being confirmed read as + // declaring nothing, and every other file's spans, once confirmed, + // are cached and stay so while those files are being confirmed: + // build it once for each set of files, and keep it until one of + // them is confirmed (see `forget_builds_confirming`). + let confirming = crate::blade::typed_receiver::confirming_files(); + if !confirming.is_empty() + && let Some((index, partial)) = THREAD_BUILDS.with(|builds| { + builds + .borrow() + .iter() + .find(|build| { + build.index.generation == generation + && build.index.is_symfony == is_symfony + && build.confirming == confirming + }) + .map(|build| (Arc::clone(&build.index), build.partial)) + }) + { + if partial { + crate::blade::typed_receiver::note_partial_answer(); + } + return index; + } let partial_before = crate::blade::typed_receiver::partial_answers(); let index = Arc::new(self.build_container_index(generation, is_symfony)); // A configurator file being confirmed further up this thread reads // as declaring nothing, so an index built meanwhile is incomplete. - if crate::blade::typed_receiver::partial_answers() == partial_before { + let partial = crate::blade::typed_receiver::partial_answers() != partial_before; + if !partial { let mut built = state.built.write(); if built .as_ref() @@ -357,10 +384,21 @@ impl Backend { *built = Some(Arc::clone(&index)); } } + if !confirming.is_empty() { + THREAD_BUILDS.with(|builds| { + builds.borrow_mut().push(ThreadBuild { + confirming, + index: Arc::clone(&index), + partial, + }); + }); + } index } fn build_container_index(&self, generation: u64, is_symfony: bool) -> ContainerIndex { + #[cfg(test)] + BUILDS.set(BUILDS.get() + 1); let mut index = ContainerIndex { generation, is_symfony, @@ -595,6 +633,39 @@ fn push_unique(out: &mut Vec, class: Atom) { } } +/// A container index this thread built while it was confirming +/// `confirming`. +struct ThreadBuild { + confirming: Vec, + index: Arc, + /// Whether the files being confirmed left the index incomplete. + partial: bool, +} + +thread_local! { + /// The container indexes this thread built for the sets of files it is + /// still confirming (see `Backend::container_index`). + static THREAD_BUILDS: std::cell::RefCell> = + const { std::cell::RefCell::new(Vec::new()) }; +} + +/// Forget the container indexes this thread built while confirming `uri`, +/// once it is confirmed: that set of files does not come back. +pub(crate) fn forget_builds_confirming(uri: &str) { + THREAD_BUILDS.with(|builds| { + let mut builds = builds.borrow_mut(); + if !builds.is_empty() { + builds.retain(|build| !build.confirming.iter().any(|file| file == uri)); + } + }); +} + +#[cfg(test)] +thread_local! { + /// How many times this thread has built the container index. + static BUILDS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + #[cfg(test)] mod tests { use super::*; @@ -659,4 +730,73 @@ mod tests { "none" ); } + + const PSR: &str = " Backend { + let backend = Backend::new_headless(); + backend.set_is_symfony(true); + open(&backend, "file:///stubs/ContainerInterface.php", PSR); + open(&backend, "file:///stubs/ContainerBuilder.php", BUILDER); + backend + } + + /// Configurator files that each register a service on the one the + /// previous file registers, named so that each sorts before the file + /// it depends on. + fn chained_configurators(backend: &Backend, n: usize) { + for k in 0..n { + let receiver = if k == 0 { + "$container".to_string() + } else { + format!("$container->get('app.s{}')", k - 1) + }; + let source = format!( + "register('app.s{k}', ContainerBuilder::class);\n}}\n" + ); + open( + backend, + &format!("file:///config/c{:02}.php", n - 1 - k), + &source, + ); + } + } + + #[test] + fn building_the_index_builds_it_once_for_each_configurator_being_confirmed() { + let backend = symfony_backend(); + chained_configurators(&backend, 8); + BUILDS.set(0); + let index = backend.container_index(); + assert_eq!(index.services.len(), 8); + assert!(BUILDS.get() <= 8, "{} builds", BUILDS.get()); + } + + #[test] + fn confirming_a_configurator_builds_the_index_once_for_all_its_lookups() { + let backend = symfony_backend(); + chained_configurators(&backend, 1); + let uri = "file:///config/lookups.php"; + let source = "get('app.s0')->register('app.a', ContainerBuilder::class);\n $container->get('app.s0')->register('app.b', ContainerBuilder::class);\n $container->get('app.s0')->register('app.c', ContainerBuilder::class);\n}\n"; + open(&backend, uri, source); + let map = backend.symbol_map_for(uri).unwrap(); + BUILDS.set(0); + let spans = backend.typed_receiver_spans_for(uri, &map); + let declared = spans + .iter() + .filter(|span| matches!(span.kind, SymbolKind::StringKey { is_write: true, .. })) + .count(); + assert_eq!(declared, 3); + assert_eq!(BUILDS.get(), 1); + } }