Refactor and optimize selection logic in ResultExtractor#30
Open
ycherkes wants to merge 2 commits into
Open
Conversation
Replaced legacy .Max()/.MaxN() selection with efficient core helpers using min-heaps and new internal types for candidate tracking and tie-breaking. Improved performance and determinism for both sequential and parallel variants. Added comprehensive benchmarks and unit tests to validate correctness, tie-breaking, and enumeration guarantees. Standardized API usage and ensured thread safety in parallel methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements #29
PR Summary — optimize-extract-methods
Branch:
optimize-extract-methodsHead commit:
20a65f733843640636139da5eb8c5dc5286b7bd5Commit message: "Refactor and optimize selection logic in ResultExtractor"
Overview
Refactors the selection logic used by the
ResultExtractorto improve performance and clarity when selecting best / top-N matches. Core extractor public APIs are preserved; internal selection was rewritten and extracted into smaller helpers and types.Key changes
ScoredCandidate<T>(record-like readonly struct) andScoredCandidateComparer<T>to represent scored items and provide simple score comparisons.FuzzySharp/FuzzySharp/Extractor/ScoredCandidate.csBestCandidate<T>helper to encapsulate "best so far" selection logic with a deterministic tie-breaker (prefer lower index on equal scores).FuzzySharp/FuzzySharp/Extractor/ScoredCandidate.csResultExtractorinternals:ExtractOneCore<T>: simplified linear scan for best match using local tracking variables.ExtractTopCore<T>: implemented top-N extraction using aMinHeap<ScoredCandidate<T>>to achieve O(n log k) behavior and lower overhead compared to full sorting.AddTopCandidate,CreateTopResultsto encapsulate heap logic and result materialization.FuzzySharp/FuzzySharp/Extractor/ResultExtractor.csBehavior / semantics
ExtractOne,ExtractTop,ExtractSorted,ExtractWithoutOrder) keep the same signatures and behavior from the consumer perspective.Rationale / benefits
limit << n.Tests / verification
ExtractTopfor correct top-N results and ordering with duplicates and ties.ExtractOneselects earliest index on equal scores.choicesand smalllimit.Files changed (approximate)
FuzzySharp/FuzzySharp/Extractor/ScoredCandidate.cs(new helpers / types)FuzzySharp/FuzzySharp/Extractor/ResultExtractor.cs(refactored selection logic)Notes for reviewers
MinHeapusage andScoredCandidateComparer<T>for expected ordering semantics.ExtractTop, very large cutoffs).