NightOwl888 commented on issue #915: URL: https://github.com/apache/lucenenet/issues/915#issuecomment-1962263448
Maybe I am missing something, but why aren't you using the built-in string comparer (TermOrdValComparer) with reverse support? https://github.com/apache/lucenenet/blob/bed207a81acb7d100a5545dc28eeeb66d35e06ba/src/Lucene.Net.Tests/Search/TestSort.cs#L350-L384 https://github.com/apache/lucenenet/blob/bed207a81acb7d100a5545dc28eeeb66d35e06ba/src/Lucene.Net.Tests/Search/TestSort.cs#L279-L312 If the custom `FieldComparer` is required, your main issue is that you are allocating memory inside of the comparison methods. Comparers should only allocate memory in the class constructor and/or field initializers, never in the comparison methods, because this will cause a lot of garbage collection overhead. ```c# public override void Copy(int slot, int doc) { termCopy = new BytesRef(); // Expensive allocation. This method will be called a lot. sortedResults.Get(doc, termCopy); bvalues[slot] = termCopy; } public override int CompareBottom(int doc) { BytesRef termOrd = new BytesRef(); // Expensive allocation. This method will be called a lot. int ord = sortedResults.GetOrd(doc); sortedResults.LookupOrd(ord, termOrd); // Expensive lookup. The TermOrdValComparer only does lookups in the Copy() method and only when necessary. var result = DoCompare(bvalues[bottomSlot], termOrd); return result; } ``` Also, the [TermOrdValComparer](https://github.com/apache/lucenenet/blob/Lucene.Net_4_8_0_beta00016/src/Lucene.Net/Search/FieldComparator.cs#L1115) uses ords to do the comparison in most cases, falling back to term comparison only when necessary. See the [`TermOrdValComparer` docs](https://lucenenet.apache.org/docs/4.8.0-beta00016/api/core/Lucene.Net.Search.FieldComparer.TermOrdValComparer.html) and [`FieldComparer<T>` docs](https://lucenenet.apache.org/docs/4.8.0-beta00016/api/core/Lucene.Net.Search.FieldComparer-1.html). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@lucenenet.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org