Skip to content

Query validation materializes results multiple times #1954

Description

@Widthdom

Summary

DbSymbolReader.SearchSymbols() / CountSearchSymbols() (src/CodeIndex/Database/DbSymbolReader.cs:122-141, 378) normalize the queries input array by chaining .Select().Where().Distinct().ToList(). At lines 124 and 139 the .ToList() forces a materialization, but when validQueries.Count > 1 line 141 immediately recurses into SearchSymbols(validQueries, ...), which re-runs the same normalization on already-normalized input. Repeated normalization adds GC pressure for queries with many distinct terms.

Where

  • src/CodeIndex/Database/DbSymbolReader.cs:122-141 (AnySearchSymbols / CountSearchSymbols)
  • src/CodeIndex/Database/DbSymbolReader.cs:378 (ExcludeUsedSymbols — same pattern)
  • src/CodeIndex/Database/DbSymbolReader.cs:235, 330 (AnalyzeSymbol — .Where().Distinct().ToList())

Suggested approach

  1. Extract query normalization into one method returning IReadOnlyList<string>; cache the result on first call per input.
  2. Change recursion call sites to pass the already-normalized list and have the callee detect it.
  3. Use HashSet<string> where order is unimportant; reserve Distinct().ToList() for cases that need ordered output.
  4. Make the public method accept IReadOnlyList<string> and document that callers should pre-normalize when possible.
  5. Add a unit test that asserts normalization runs once per distinct input set.
  6. Run a benchmark with a 50-term query and verify allocations drop materially.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions