Skip to content

Commit

Permalink
SWEEP: Lucene.Net: Use NumericUtils.SingleToSortableInt32() to compar…
Browse files Browse the repository at this point in the history
…e floating point numbers (except in cases where we are testing for whole numbers).
  • Loading branch information
NightOwl888 committed Oct 23, 2021
1 parent c83dbd2 commit 677e8eb
Show file tree
Hide file tree
Showing 24 changed files with 103 additions and 46 deletions.
4 changes: 3 additions & 1 deletion src/Lucene.Net/Search/BooleanQuery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using J2N;
using J2N.Collections.Generic.Extensions;
using Lucene.Net.Util;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -681,7 +682,8 @@ public override bool Equals(object o)
return false;
}
BooleanQuery other = (BooleanQuery)o;
return this.Boost == other.Boost
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return NumericUtils.SingleToSortableInt32(this.Boost) == NumericUtils.SingleToSortableInt32(other.Boost)
&& this.clauses.Equals(other.clauses)
&& this.MinimumNumberShouldMatch == other.MinimumNumberShouldMatch
&& this.disableCoord == other.disableCoord;
Expand Down
5 changes: 3 additions & 2 deletions src/Lucene.Net/Search/DisjunctionMaxQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,9 @@ public override bool Equals(object o)
return false;
}
DisjunctionMaxQuery other = (DisjunctionMaxQuery)o;
return this.Boost == other.Boost
&& this.tieBreakerMultiplier == other.tieBreakerMultiplier
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return NumericUtils.SingleToSortableInt32(this.Boost) == NumericUtils.SingleToSortableInt32(other.Boost)
&& NumericUtils.SingleToSortableInt32(this.tieBreakerMultiplier) == NumericUtils.SingleToSortableInt32(other.tieBreakerMultiplier)
&& this.disjuncts.Equals(other.disjuncts);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Lucene.Net/Search/FieldCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,8 @@ public override bool Equals(object obj)
{
if (obj is AcceptableOverheadRatio other)
{
return Value.Equals(other.Value);
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return NumericUtils.SingleToSortableInt32(Value) == NumericUtils.SingleToSortableInt32(other.Value);
}
return false;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Lucene.Net/Search/HitQueue.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Lucene.Net.Search
namespace Lucene.Net.Search
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
Expand Down Expand Up @@ -76,13 +76,14 @@ protected override ScoreDoc GetSentinelObject()

protected internal override sealed bool LessThan(ScoreDoc hitA, ScoreDoc hitB)
{
if (hitA.Score == hitB.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(hitA.Score) == NumericUtils.SingleToSortableInt32(hitB.Score))
{
return hitA.Doc > hitB.Doc;
}
else
{
return hitA.Score < hitB.Score;
return NumericUtils.SingleToSortableInt32(hitA.Score) < NumericUtils.SingleToSortableInt32(hitB.Score);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Lucene.Net/Search/IndexSearcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using Lucene.Net.Support.Threading;
using Lucene.Net.Util;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -818,7 +819,8 @@ public TopFieldDocs Call()
}

// Carry over maxScore from sub:
if (doMaxScore && docs.MaxScore > hq.maxScore)
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
if (doMaxScore && NumericUtils.SingleToSortableInt32(docs.MaxScore) > NumericUtils.SingleToSortableInt32(hq.maxScore))
{
hq.maxScore = docs.MaxScore;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Lucene.Net/Search/MatchAllDocsQuery.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Lucene.Net.Util;
using System.Collections.Generic;
using System.Text;

Expand Down Expand Up @@ -155,7 +156,8 @@ public override bool Equals(object o)
return false;
}
MatchAllDocsQuery other = (MatchAllDocsQuery)o;
return this.Boost == other.Boost;
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return NumericUtils.SingleToSortableInt32(this.Boost) == NumericUtils.SingleToSortableInt32(other.Boost);
}

public override int GetHashCode()
Expand Down
8 changes: 5 additions & 3 deletions src/Lucene.Net/Search/MultiPhraseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace Lucene.Net.Search
*/

using J2N.Collections.Generic.Extensions;
using Lucene.Net.Util;
using System.Collections;
using ArrayUtil = Lucene.Net.Util.ArrayUtil;
using AtomicReader = Lucene.Net.Index.AtomicReader;
Expand Down Expand Up @@ -444,9 +445,10 @@ public override bool Equals(object o)
return false;
}
MultiPhraseQuery other = (MultiPhraseQuery)o;
return this.Boost == other.Boost
&& this.slop == other.slop
&& TermArraysEquals(this.termArrays, other.termArrays)
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return NumericUtils.SingleToSortableInt32(this.Boost) == NumericUtils.SingleToSortableInt32(other.Boost)
&& this.slop == other.slop
&& TermArraysEquals(this.termArrays, other.termArrays)
&& this.positions.Equals(other.positions);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Lucene.Net/Search/PhraseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Index;
using Lucene.Net.Support;
using Lucene.Net.Util;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -504,7 +505,8 @@ public override bool Equals(object o)
return false;
}
PhraseQuery other = (PhraseQuery)o;
return (this.Boost == other.Boost)
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return (NumericUtils.SingleToSortableInt32(this.Boost) == NumericUtils.SingleToSortableInt32(other.Boost))
&& (this.slop == other.slop)
&& this.terms.Equals(other.terms)
&& this.positions.Equals(other.positions);
Expand Down
8 changes: 5 additions & 3 deletions src/Lucene.Net/Search/QueryRescorer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -114,11 +115,12 @@ public override TopDocs Rescore(IndexSearcher searcher, TopDocs firstPassTopDocs
Array.Sort(hits, Comparer<ScoreDoc>.Create((a, b) =>
{
// Sort by score descending, then docID ascending:
if (a.Score > b.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(a.Score) > NumericUtils.SingleToSortableInt32(b.Score))
{
return -1;
}
else if (a.Score < b.Score)
else if (NumericUtils.SingleToSortableInt32(a.Score) < NumericUtils.SingleToSortableInt32(b.Score))
{
return 1;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Lucene.Net/Search/ScoringRewrite.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using System;

namespace Lucene.Net.Search
Expand Down Expand Up @@ -174,7 +175,8 @@ public override bool Collect(BytesRef bytes)
// duplicate term: update docFreq
int pos = (-e) - 1;
array.termState[pos].Register(state, m_readerContext.Ord, termsEnum.DocFreq, termsEnum.TotalTermFreq);
if (Debugging.AssertsEnabled) Debugging.Assert(array.boost[pos] == boostAtt.Boost, "boost should be equal in all segment TermsEnums");
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
if (Debugging.AssertsEnabled) Debugging.Assert(NumericUtils.SingleToSortableInt32(array.boost[pos]) == NumericUtils.SingleToSortableInt32(boostAtt.Boost), "boost should be equal in all segment TermsEnums");
}
else
{
Expand Down
6 changes: 5 additions & 1 deletion src/Lucene.Net/Search/Spans/FieldMaskingSpanQuery.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -144,7 +145,10 @@ public override bool Equals(object o)
return false;
}
FieldMaskingSpanQuery other = (FieldMaskingSpanQuery)o;
return (this.Field.Equals(other.Field, StringComparison.Ordinal) && (this.Boost == other.Boost) && this.MaskedQuery.Equals(other.MaskedQuery));
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return (this.Field.Equals(other.Field, StringComparison.Ordinal)
&& (NumericUtils.SingleToSortableInt32(this.Boost) == NumericUtils.SingleToSortableInt32(other.Boost))
&& this.MaskedQuery.Equals(other.MaskedQuery));
}

public override int GetHashCode()
Expand Down
6 changes: 5 additions & 1 deletion src/Lucene.Net/Search/Spans/SpanFirstQuery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using J2N.Numerics;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using System.Text;

namespace Lucene.Net.Search.Spans
Expand Down Expand Up @@ -88,7 +89,10 @@ public override bool Equals(object o)
}

SpanFirstQuery other = (SpanFirstQuery)o;
return this.m_end == other.m_end && this.m_match.Equals(other.m_match) && this.Boost == other.Boost;
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return this.m_end == other.m_end
&& this.m_match.Equals(other.m_match)
&& NumericUtils.SingleToSortableInt32(this.Boost) == NumericUtils.SingleToSortableInt32(other.Boost);
}

public override int GetHashCode()
Expand Down
8 changes: 5 additions & 3 deletions src/Lucene.Net/Search/Spans/SpanNearPayloadCheckQuery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using J2N.Numerics;
using Lucene.Net.Support;
using Lucene.Net.Util;
using System.Collections;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -130,9 +131,10 @@ public override bool Equals(object o)
}

// LUCENENET NOTE: Need to use the structural equality comparer to compare equality of all contained values
return payloadEqualityComparer.Equals(this.m_payloadToMatch, other.m_payloadToMatch)
&& this.m_match.Equals(other.m_match)
&& this.Boost == other.Boost;
return payloadEqualityComparer.Equals(this.m_payloadToMatch, other.m_payloadToMatch)
&& this.m_match.Equals(other.m_match)
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
&& NumericUtils.SingleToSortableInt32(this.Boost) == NumericUtils.SingleToSortableInt32(other.Boost);
}

public override int GetHashCode()
Expand Down
4 changes: 3 additions & 1 deletion src/Lucene.Net/Search/Spans/SpanNearQuery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using J2N.Collections.Generic.Extensions;
using J2N.Numerics;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -209,7 +210,8 @@ public override bool Equals(object o)
return false;
}

return Boost == spanNearQuery.Boost;
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return NumericUtils.SingleToSortableInt32(Boost) == NumericUtils.SingleToSortableInt32(spanNearQuery.Boost);
}

public override int GetHashCode()
Expand Down
4 changes: 3 additions & 1 deletion src/Lucene.Net/Search/Spans/SpanOrQuery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using J2N.Collections.Generic.Extensions;
using J2N.Numerics;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -163,7 +164,8 @@ public override bool Equals(object o)
return false;
}

return Boost == that.Boost;
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return NumericUtils.SingleToSortableInt32(Boost) == NumericUtils.SingleToSortableInt32(that.Boost);
}

public override int GetHashCode()
Expand Down
8 changes: 5 additions & 3 deletions src/Lucene.Net/Search/Spans/SpanPayloadCheckQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Collections;
using J2N.Numerics;
using Lucene.Net.Util;

namespace Lucene.Net.Search.Spans
{
Expand Down Expand Up @@ -134,9 +135,10 @@ public override bool Equals(object o)

SpanPayloadCheckQuery other = (SpanPayloadCheckQuery)o;
// LUCENENET NOTE: Need to use the structural equality comparer to compare equality of all contained values
return payloadEqualityComparer.Equals(this.m_payloadToMatch, other.m_payloadToMatch)
&& this.m_match.Equals(other.m_match)
&& this.Boost == other.Boost;
return payloadEqualityComparer.Equals(this.m_payloadToMatch, other.m_payloadToMatch)
&& this.m_match.Equals(other.m_match)
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
&& NumericUtils.SingleToSortableInt32(this.Boost) == NumericUtils.SingleToSortableInt32(other.Boost);
}

public override int GetHashCode()
Expand Down
6 changes: 5 additions & 1 deletion src/Lucene.Net/Search/Spans/SpanPositionRangeQuery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using J2N.Numerics;
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using System.Text;

namespace Lucene.Net.Search.Spans
Expand Down Expand Up @@ -93,7 +94,10 @@ public override bool Equals(object o)
}

SpanPositionRangeQuery other = (SpanPositionRangeQuery)o;
return this.m_end == other.m_end && this.m_start == other.m_start && this.m_match.Equals(other.m_match) && this.Boost == other.Boost;
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return this.m_end == other.m_end
&& this.m_start == other.m_start
&& this.m_match.Equals(other.m_match) && NumericUtils.SingleToSortableInt32(this.Boost) == NumericUtils.SingleToSortableInt32(other.Boost);
}

public override int GetHashCode()
Expand Down
5 changes: 4 additions & 1 deletion src/Lucene.Net/Search/TermQuery.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -233,7 +234,9 @@ public override bool Equals(object o)
return false;
}
TermQuery other = (TermQuery)o;
return (this.Boost == other.Boost) && this.term.Equals(other.term);
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return (NumericUtils.SingleToSortableInt32(this.Boost) == NumericUtils.SingleToSortableInt32(other.Boost))
&& this.term.Equals(other.term);
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/Lucene.Net/Search/TopDocs.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using Lucene.Net.Util;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -111,11 +112,12 @@ protected internal override bool LessThan(ShardRef first, ShardRef second)
float firstScore = shardHits[first.ShardIndex][first.HitIndex].Score;
float secondScore = shardHits[second.ShardIndex][second.HitIndex].Score;

if (firstScore < secondScore)
// 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(firstScore) < NumericUtils.SingleToSortableInt32(secondScore))
{
return false;
}
else if (firstScore > secondScore)
else if (NumericUtils.SingleToSortableInt32(firstScore) > NumericUtils.SingleToSortableInt32(secondScore))
{
return true;
}
Expand Down
15 changes: 10 additions & 5 deletions src/Lucene.Net/Search/TopFieldCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ internal void UpdateBottom(int doc, float score)
public override void Collect(int doc)
{
float score = scorer.GetScore();
if (score > maxScore)
// 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(maxScore))
{
maxScore = score;
}
Expand Down Expand Up @@ -355,7 +356,8 @@ public OutOfOrderOneComparerScoringMaxScoreCollector(FieldValueHitQueue<Entry> q
public override void Collect(int doc)
{
float score = scorer.GetScore();
if (score > maxScore)
// 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(maxScore))
{
maxScore = score;
}
Expand Down Expand Up @@ -600,7 +602,8 @@ internal void UpdateBottom(int doc, float score)
public override void Collect(int doc)
{
float score = scorer.GetScore();
if (score > maxScore)
// 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(maxScore))
{
maxScore = score;
}
Expand Down Expand Up @@ -685,7 +688,8 @@ public OutOfOrderMultiComparerScoringMaxScoreCollector(FieldValueHitQueue<Entry>
public override void Collect(int doc)
{
float score = scorer.GetScore();
if (score > maxScore)
// 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(maxScore))
{
maxScore = score;
}
Expand Down Expand Up @@ -990,7 +994,8 @@ public override void Collect(int doc)
if (trackMaxScore)
{
score = scorer.GetScore();
if (score > maxScore)
// 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(maxScore))
{
maxScore = score;
}
Expand Down
Loading

0 comments on commit 677e8eb

Please sign in to comment.