Skip to content

[efficiency-improver] perf: eliminate LINQ Select+Where+ToList+ToArray allocation in TaxonomySearchContext.SearchableFields - #590

Draft
github-actions[bot] wants to merge 1 commit into
devfrom
efficiency/taxonomysearchcontext-linq-loop-92a6de6d10e2a5ec
Draft

[efficiency-improver] perf: eliminate LINQ Select+Where+ToList+ToArray allocation in TaxonomySearchContext.SearchableFields#590
github-actions[bot] wants to merge 1 commit into
devfrom
efficiency/taxonomysearchcontext-linq-loop-92a6de6d10e2a5ec

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🤖 This PR was created by Efficiency Improver, an automated AI assistant.

Focus Area

Code-Level Efficiency

Goal

TaxonomySearchContext.SearchableFields built its result with MultiFields.GetMergedFieldInfos(reader).Select(x => x.Name).ToList() followed by a second .Where(...).ToArray() chain. This allocates two LINQ iterator/state-machine objects (Select + Where) plus an intermediate List<string> buffer, on every rebuild of the searchable-fields cache. The sibling class SearchContext.SearchableFields already had this exact pattern eliminated in PR #545, but TaxonomySearchContext (used for the facets/taxonomy search path) was missed.

Approach

Replaced the two-stage LINQ chain with a single pre-sized List<string> (capacity = FieldInfos.Count) populated via one foreach loop that inlines the filter condition, then materialized once with .ToArray(). Removed the now-unused using System.Linq; and added using System.Collections.Generic;. Behavior is unchanged — same field names, same order, same filtering of special/facet fields, same caching semantics (only non-empty results are cached).

Energy Efficiency Evidence

Proxy metric: memory allocation (reduced iterator/state-machine + intermediate list allocations), which maps to energy via reduced GC/DRAM churn — the same methodology used in the previously-merged PR #545 for the equivalent SearchContext.SearchableFields fix.

This method rebuilds at most once per TaxonomySearchContext lifetime (or once per empty-index probe), so per-call savings are small, but the pattern eliminates 2 iterator allocations + 1 intermediate List<string> per rebuild with zero behavioral change.

Green Software Foundation Context

  • Hardware Efficiency: Fewer short-lived allocations reduce Gen0 GC pressure, making better use of CPU cache/memory bandwidth per functional unit of work (SCI: Energy component reduced, functional unit unchanged).

Trade-offs

None identified — public API/behavior unchanged. The explicit loop is marginally more verbose than the LINQ chain but stays simple and matches the already-established pattern in SearchContext.SearchableFields.

Test Status

  • dotnet build src/Examine.sln --configuration Release — succeeded, 0 errors, 0 warnings.
  • dotnet test src/Examine.Test/Examine.Test.csproj -f net8.0 --configuration Release316 passed, 0 failed, 2 skipped.

Reproducibility

dotnet build src/Examine.sln --configuration Release
dotnet test src/Examine.Test/Examine.Test.csproj -f net8.0 --configuration Release

Generated by Efficiency Improver · auto · 67 AIC · ⌖ 12.4 AIC · ⊞ 17.6K ·

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/efficiency-improver.md@42c2ab5b4e4c9273534c39259b2e0df7f20f07e9

…archContext.SearchableFields

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants