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
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion src/blade/typed_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,19 @@ thread_local! {
static PARTIAL_ANSWERS: std::cell::Cell<u64> = 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<String> {
let mut files: Vec<String> =
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
Expand Down Expand Up @@ -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);
}
}

Expand Down
142 changes: 141 additions & 1 deletion src/symfony/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -595,6 +633,39 @@ fn push_unique(out: &mut Vec<Atom>, class: Atom) {
}
}

/// A container index this thread built while it was confirming
/// `confirming`.
struct ThreadBuild {
confirming: Vec<String>,
index: Arc<ContainerIndex>,
/// 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<Vec<ThreadBuild>> =
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<u32> = const { std::cell::Cell::new(0) };
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -659,4 +730,73 @@ mod tests {
"none"
);
}

const PSR: &str = "<?php\nnamespace Psr\\Container;\ninterface ContainerInterface {\n public function get(string $id): mixed;\n}\n";
const BUILDER: &str = "<?php\nnamespace Symfony\\Component\\DependencyInjection;\nclass ContainerBuilder implements \\Psr\\Container\\ContainerInterface {\n public function get(string $id): mixed {}\n public function register(string $id, ?string $class = null): Definition {}\n}\nclass Definition {}\n";

fn open(backend: &Backend, uri: &str, source: &str) {
backend
.open_files
.write()
.insert(uri.to_string(), Arc::new(source.to_string()));
backend.update_ast(uri, source);
}

/// A Symfony backend that knows the container's classes.
fn symfony_backend() -> 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!(
"<?php\nnamespace App;\nuse Symfony\\Component\\DependencyInjection\\ContainerBuilder;\nfunction configure{k}(ContainerBuilder $container): void {{\n {receiver}->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 = "<?php\nnamespace App;\nuse Symfony\\Component\\DependencyInjection\\ContainerBuilder;\nfunction lookups(ContainerBuilder $container): void {\n $container->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);
}
}
Loading