BREAKING: Introducing ValuePriorityQueue<T> #826
Merged
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.
This adds a new ref struct to the project,
Lucene.Net.Util.ValuePriorityQueue<T>
. This struct contains the most of the same API and business logic asPriorityQueue<T>
, but allows the memory used to be allocated elsewhere asSpan<T>
and passed in.The amount to allocate can be calculated by calling
PriorityQueue.GetArrayHeapSize(maxSize)
, wheremaxSize
is the external size of thePriorityQueue
(representing the same value that is passed into the constructor of the currentPriorityQueue<T>
). The actual allocation amount is 1 greater than the priority queue size because it is 1-based rather than 0-based.We get around the issue with structs not being inheritable by introducing a new
PriorityComparer<T>
abstract class with a singlebool LessThan(T a, T b)
method. This signature makes converting existing subclasses ofPriorityQueue<T>
toPriorityComparer<T>
straightforward, as there are no business logic changes to make. ThisIComparer<T>
is a required constructor argument ofValuePriorityQueue<T>
. It can be either aPriorityComparer<T>
orIComparer<T>
, although if it returns a 0 (equal), that value is considered the same as 1 (greater than) by the priority queue.This also modifies
SingleTaxonomyFacets
,Int32TaxonomyFacets
,TopDocs
merge andFST
pack to useValuePriorityQueue<T>
and converts the item types they deal with from class to struct so they can be stack allocated if the size is belowConstants.MaxStackByteLimit
, which is set using the system propertymaxStackByteLimit
.Closes #774. Thanks @eladmarg for the suggestion.
There are a few more places in the codebase where this type can be useful, but unfortunately the types they use don't convert easily into structs so although we can eliminate the heap allocation of the priority queue itself, we will need some configuration and/or logic to decide whether to allocate the array directly or use an array pool.
Breaking Changes
OrdAndValue<T>
from class to struct.OrdAndValue<T> where T : struct
.