[ty] Optimize collection of "all symbols"

Instead of locking and unlocking the mutex for every symbol,
we gather what we can and only lock the mutex after processing
a module.

Before:

```
$ ./target/profiling/ty_completion_bench ~/astral/relatedclones/scratch-home-assistant/homeassistant/scratch.py 1 -q --iters 30
total elapsed for initial completions request: 579.057075ms
total elapsed: 5.627666455s, time per completion request: 187.588881ms
```

After:

```
$ ./target/profiling/ty_completion_bench ~/astral/relatedclones/scratch-home-assistant/homeassistant/scratch.py 1 -q --iters 30
total elapsed for initial completions request: 565.911487ms
total elapsed: 5.254306446s, time per completion request: 175.143548ms
```

This is a very naive/simplistic "improvement." We can probably do
better.
This commit is contained in:
Andrew Gallant
2026-01-16 13:22:15 -05:00
committed by Andrew Gallant
parent 79741c59c9
commit 1f6a87f770

View File

@@ -72,22 +72,19 @@ pub fn all_symbols<'db>(
);
let _entered = symbols_for_file_span.entered();
let mut symbols = vec![];
if query.is_match_symbol_name(module.name(&*db)) {
let symbol = AllSymbolInfo::from_module(&*db, module, file);
results.lock().unwrap().push(symbol);
symbols.push(AllSymbolInfo::from_module(&*db, module, file));
}
for (_, symbol) in symbols_for_file_global_only(&*db, file).search(query) {
let symbol = AllSymbolInfo::from_non_module_symbol(
symbols.push(AllSymbolInfo::from_non_module_symbol(
&*db,
symbol.to_owned(),
module,
file,
);
// It seems like we could do better here than
// locking `results` for every single symbol,
// but this works pretty well as it is.
results.lock().unwrap().push(symbol);
));
}
results.lock().unwrap().extend(symbols);
});
}
});