Skip to content

Commit

Permalink
BUG: Lucene.Net.Search.TopScoreDocCollector: Compare using Lucene.Net…
Browse files Browse the repository at this point in the history
….Util.NumericUtils.SingleToSortableInt32() to prevent test failures on x86 .NET Framework. This fixes Lucene.Net.Search.TestTopDocsMerge::TestSort_1(), Lucene.Net.Search.TestTopDocsMerge::TestSort_2(), and Lucene.Net.Search.TestSearchAfter.TestQueries(). See #269.
  • Loading branch information
NightOwl888 committed Oct 23, 2021
1 parent 8e5ecaf commit 266afb3
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/Lucene.Net/Search/TopScoreDocCollector.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using Lucene.Net.Util;
using System;
using System.Runtime.CompilerServices;

namespace Lucene.Net.Search
{
Expand Down Expand Up @@ -47,9 +47,6 @@ internal InOrderTopScoreDocCollector(int numHits)
{
}

#if NETFRAMEWORK
[MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing float equality fails in x86 on .NET Framework with optimizations enabled
#endif
public override void Collect(int doc)
{
float score = scorer.GetScore();
Expand All @@ -62,7 +59,8 @@ public override void Collect(int doc)
}

m_totalHits++;
if (score <= pqTop.Score)
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
if (NumericUtils.SingleToSortableInt32(score) <= NumericUtils.SingleToSortableInt32(pqTop.Score))
{
// Since docs are returned in-order (i.e., increasing doc Id), a document
// with equal score to pqTop.score cannot compete since HitQueue favors
Expand Down Expand Up @@ -93,9 +91,6 @@ internal InOrderPagingScoreDocCollector(ScoreDoc after, int numHits)
this.after = after;
}

#if NETFRAMEWORK
[MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing float equality fails in x86 on .NET Framework with optimizations enabled
#endif
public override void Collect(int doc)
{
float score = scorer.GetScore();
Expand All @@ -109,13 +104,17 @@ public override void Collect(int doc)

m_totalHits++;

if (score > after.Score || (score == after.Score && doc <= afterDoc))
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
int scoreBits = NumericUtils.SingleToSortableInt32(score);
int afterBits = NumericUtils.SingleToSortableInt32(after.Score);

if (scoreBits > afterBits || (scoreBits == afterBits && doc <= afterDoc))
{
// hit was collected on a previous page
return;
}

if (score <= pqTop.Score)
if (scoreBits <= NumericUtils.SingleToSortableInt32(pqTop.Score))
{
// Since docs are returned in-order (i.e., increasing doc Id), a document
// with equal score to pqTop.score cannot compete since HitQueue favors
Expand Down Expand Up @@ -153,9 +152,6 @@ internal OutOfOrderTopScoreDocCollector(int numHits)
{
}

#if NETFRAMEWORK
[MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing float equality fails in x86 on .NET Framework with optimizations enabled
#endif
public override void Collect(int doc)
{
float score = scorer.GetScore();
Expand All @@ -164,13 +160,17 @@ public override void Collect(int doc)
if (Debugging.AssertsEnabled) Debugging.Assert(!float.IsNaN(score));

m_totalHits++;
if (score < pqTop.Score)

// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
int scoreBits = NumericUtils.SingleToSortableInt32(score);
int pqTopBits = NumericUtils.SingleToSortableInt32(pqTop.Score);
if (scoreBits < pqTopBits)
{
// Doesn't compete w/ bottom entry in queue
return;
}
doc += docBase;
if (score == pqTop.Score && doc > pqTop.Doc)
if (scoreBits == pqTopBits && doc > pqTop.Doc)
{
// Break tie in score by doc ID:
return;
Expand Down Expand Up @@ -199,9 +199,6 @@ internal OutOfOrderPagingScoreDocCollector(ScoreDoc after, int numHits)
this.after = after;
}

#if NETFRAMEWORK
[MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing float equality fails in x86 on .NET Framework with optimizations enabled
#endif
public override void Collect(int doc)
{
float score = scorer.GetScore();
Expand All @@ -210,18 +207,22 @@ public override void Collect(int doc)
if (Debugging.AssertsEnabled) Debugging.Assert(!float.IsNaN(score));

m_totalHits++;
if (score > after.Score || (score == after.Score && doc <= afterDoc))
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
int scoreBits = NumericUtils.SingleToSortableInt32(score);
int afterBits = NumericUtils.SingleToSortableInt32(after.Score);
if (scoreBits > afterBits || (scoreBits == afterBits && doc <= afterDoc))
{
// hit was collected on a previous page
return;
}
if (score < pqTop.Score)
int pqTopBits = NumericUtils.SingleToSortableInt32(pqTop.Score);
if (scoreBits < pqTopBits)
{
// Doesn't compete w/ bottom entry in queue
return;
}
doc += docBase;
if (score == pqTop.Score && doc > pqTop.Doc)
if (scoreBits == pqTopBits && doc > pqTop.Doc)
{
// Break tie in score by doc ID:
return;
Expand Down

0 comments on commit 266afb3

Please sign in to comment.