Skip to content

Commit db69df3

Browse files
committed
Auto merge of #17478 - kilpkonn:master, r=Veykril
Simplify some term search tactics Working on the paper `@phijor` found that "Data constructor" tactic could be simplified quite a bit by running it only in the backwards direction. With n+1 rounds it has same coverage as previous implementation in n rounds, however the tactic it self is more simple and also potentially faster as there is less to do. In a nutshell the idea is to only work with types in the wish-list rather than with any types. Turns out it is quite a bit faster: Before: ``` ripgrep: Tail Expr syntactic hits: 238/1692 (14%) Tail Exprs found: 1223/1692 (72%) Term search avg time: 15ms nalgebra: Tail Expr syntactic hits: 125/3001 (4%) Tail Exprs found: 2143/3001 (71%) Term search avg time: 849ms ``` After ```` ripgrep: Tail Expr syntactic hits: 246/1692 (14%) Tail Exprs found: 1209/1692 (71%) Term search avg time: 8ms nalgebra: Tail Expr syntactic hits: 125/3001 (4%) Tail Exprs found: 2028/3001 (67%) Term search avg time: 305ms ```` _Also removed niche optimization of removing scope defs from the search space as this wasn't helping much anyway and made code a bit more complex._
2 parents 2fd803c + 51c3bd2 commit db69df3

File tree

8 files changed

+163
-456
lines changed

8 files changed

+163
-456
lines changed

crates/hir/src/term_search.rs

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ struct LookupTable {
9393
data: FxHashMap<Type, AlternativeExprs>,
9494
/// New types reached since last query by the `NewTypesKey`
9595
new_types: FxHashMap<NewTypesKey, Vec<Type>>,
96-
/// ScopeDefs that are not interesting any more
97-
exhausted_scopedefs: FxHashSet<ScopeDef>,
98-
/// ScopeDefs that were used in current round
99-
round_scopedef_hits: FxHashSet<ScopeDef>,
100-
/// Amount of rounds since scopedef was first used.
101-
rounds_since_sopedef_hit: FxHashMap<ScopeDef, u32>,
10296
/// Types queried but not present
10397
types_wishlist: FxHashSet<Type>,
10498
/// Threshold to squash trees to `Many`
@@ -212,37 +206,6 @@ impl LookupTable {
212206
}
213207
}
214208

215-
/// Mark `ScopeDef` as exhausted meaning it is not interesting for us any more
216-
fn mark_exhausted(&mut self, def: ScopeDef) {
217-
self.exhausted_scopedefs.insert(def);
218-
}
219-
220-
/// Mark `ScopeDef` as used meaning we managed to produce something useful from it
221-
fn mark_fulfilled(&mut self, def: ScopeDef) {
222-
self.round_scopedef_hits.insert(def);
223-
}
224-
225-
/// Start new round (meant to be called at the beginning of iteration in `term_search`)
226-
///
227-
/// This functions marks some `ScopeDef`s as exhausted if there have been
228-
/// `MAX_ROUNDS_AFTER_HIT` rounds after first using a `ScopeDef`.
229-
fn new_round(&mut self) {
230-
for def in &self.round_scopedef_hits {
231-
let hits =
232-
self.rounds_since_sopedef_hit.entry(*def).and_modify(|n| *n += 1).or_insert(0);
233-
const MAX_ROUNDS_AFTER_HIT: u32 = 2;
234-
if *hits > MAX_ROUNDS_AFTER_HIT {
235-
self.exhausted_scopedefs.insert(*def);
236-
}
237-
}
238-
self.round_scopedef_hits.clear();
239-
}
240-
241-
/// Get exhausted `ScopeDef`s
242-
fn exhausted_scopedefs(&self) -> &FxHashSet<ScopeDef> {
243-
&self.exhausted_scopedefs
244-
}
245-
246209
/// Types queried but not found
247210
fn types_wishlist(&mut self) -> &FxHashSet<Type> {
248211
&self.types_wishlist
@@ -275,7 +238,7 @@ pub struct TermSearchConfig {
275238

276239
impl Default for TermSearchConfig {
277240
fn default() -> Self {
278-
Self { enable_borrowcheck: true, many_alternatives_threshold: 1, fuel: 400 }
241+
Self { enable_borrowcheck: true, many_alternatives_threshold: 1, fuel: 1200 }
279242
}
280243
}
281244

@@ -328,19 +291,12 @@ pub fn term_search<DB: HirDatabase>(ctx: &TermSearchCtx<'_, DB>) -> Vec<Expr> {
328291
solutions.extend(tactics::assoc_const(ctx, &defs, &mut lookup));
329292

330293
while should_continue() {
331-
lookup.new_round();
332-
333294
solutions.extend(tactics::data_constructor(ctx, &defs, &mut lookup, should_continue));
334295
solutions.extend(tactics::free_function(ctx, &defs, &mut lookup, should_continue));
335296
solutions.extend(tactics::impl_method(ctx, &defs, &mut lookup, should_continue));
336297
solutions.extend(tactics::struct_projection(ctx, &defs, &mut lookup, should_continue));
337298
solutions.extend(tactics::impl_static_method(ctx, &defs, &mut lookup, should_continue));
338299
solutions.extend(tactics::make_tuple(ctx, &defs, &mut lookup, should_continue));
339-
340-
// Discard not interesting `ScopeDef`s for speedup
341-
for def in lookup.exhausted_scopedefs() {
342-
defs.remove(def);
343-
}
344300
}
345301

346302
solutions.into_iter().filter(|it| !it.is_many()).unique().collect()

0 commit comments

Comments
 (0)