Skip to content

Commit

Permalink
SWEEP: Use IndexOptionsComparer class to avoid the boxing associated …
Browse files Browse the repository at this point in the history
…with System.Enum.CompareTo() on IndexOptions enum (closes #375, closes #376)
  • Loading branch information
NightOwl888 committed Nov 3, 2020
1 parent 54aabdf commit 3c01253
Show file tree
Hide file tree
Showing 31 changed files with 195 additions and 124 deletions.
10 changes: 6 additions & 4 deletions src/Lucene.Net.Codecs/BlockTerms/BlockTermsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,12 @@ public override TermsEnum GetEnumerator()
return new SegmentTermsEnum(this);
}

public override bool HasFreqs => fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS) >= 0;
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
public override bool HasFreqs => IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS) >= 0;

public override bool HasOffsets => fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
public override bool HasOffsets => IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;

public override bool HasPositions => fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
public override bool HasPositions => IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;

public override bool HasPayloads => fieldInfo.HasPayloads;

Expand Down Expand Up @@ -786,7 +787,8 @@ public override DocsEnum Docs(IBits liveDocs, DocsEnum reuse, DocsFlags flags)
public override DocsAndPositionsEnum DocsAndPositions(IBits liveDocs, DocsAndPositionsEnum reuse,
DocsAndPositionsFlags flags)
{
if (outerInstance.fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0)
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
if (IndexOptionsComparer.Default.Compare(outerInstance.fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0)
{
// Positions were not indexed:
return null;
Expand Down
7 changes: 4 additions & 3 deletions src/Lucene.Net.Codecs/Memory/DirectPostingsFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,10 @@ public DirectField(SegmentReadState state, string field, Terms termsIn, int minS

this.minSkipCount = minSkipCount;

hasFreq = fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_ONLY) > 0;
hasPos = fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS) > 0;
hasOffsets = fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) > 0;
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
hasFreq = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_ONLY) > 0;
hasPos = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS) > 0;
hasOffsets = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) > 0;
hasPayloads = fieldInfo.HasPayloads;

BytesRef term;
Expand Down
7 changes: 4 additions & 3 deletions src/Lucene.Net.Codecs/Memory/FSTOrdTermsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,12 @@ internal TermsReader(FSTOrdTermsReader outerInstance, FieldInfo fieldInfo, Index

public override IComparer<BytesRef> Comparer => BytesRef.UTF8SortedAsUnicodeComparer;

public override bool HasFreqs => fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS) >= 0;
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
public override bool HasFreqs => IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS) >= 0;

public override bool HasOffsets => fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
public override bool HasOffsets => IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;

public override bool HasPositions => fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
public override bool HasPositions => IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;

public override bool HasPayloads => fieldInfo.HasPayloads;

Expand Down
7 changes: 4 additions & 3 deletions src/Lucene.Net.Codecs/Memory/FSTTermsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,12 @@ internal TermsReader(FSTTermsReader outerInstance, FieldInfo fieldInfo, IndexInp

public override IComparer<BytesRef> Comparer => BytesRef.UTF8SortedAsUnicodeComparer;

public override bool HasFreqs => fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS) >= 0;
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
public override bool HasFreqs => IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS) >= 0;

public override bool HasOffsets => fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
public override bool HasOffsets => IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;

public override bool HasPositions => fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
public override bool HasPositions => IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;

public override bool HasPayloads => fieldInfo.HasPayloads;

Expand Down
13 changes: 7 additions & 6 deletions src/Lucene.Net.Codecs/Memory/MemoryPostingsFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ public override void AddPosition(int pos, BytesRef payload, int startOffset, int
buffer.WriteVInt32(delta);
}

if (outerInstance.field.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0)
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
if (IndexOptionsComparer.Default.Compare(outerInstance.field.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0)
{
// don't use startOffset - lastEndOffset, because this creates lots of negative vints for synonyms,
// and the numbers aren't that much smaller anyways.
Expand Down Expand Up @@ -825,8 +826,8 @@ public override DocsEnum Docs(IBits liveDocs, DocsEnum reuse, DocsFlags flags)

public override DocsAndPositionsEnum DocsAndPositions(IBits liveDocs, DocsAndPositionsEnum reuse, DocsAndPositionsFlags flags)
{
bool hasOffsets = field.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
if (field.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0)
bool hasOffsets = IndexOptionsComparer.Default.Compare(field.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
if (IndexOptionsComparer.Default.Compare(field.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0)
{
return null;
}
Expand Down Expand Up @@ -948,11 +949,11 @@ public override TermsEnum GetEnumerator()

public override IComparer<BytesRef> Comparer => BytesRef.UTF8SortedAsUnicodeComparer;

public override bool HasFreqs => field.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS) >= 0;
public override bool HasFreqs => IndexOptionsComparer.Default.Compare(field.IndexOptions, IndexOptions.DOCS_AND_FREQS) >= 0;

public override bool HasOffsets => field.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
public override bool HasOffsets => IndexOptionsComparer.Default.Compare(field.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;

public override bool HasPositions => field.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
public override bool HasPositions => IndexOptionsComparer.Default.Compare(field.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;

public override bool HasPayloads => field.HasPayloads;

Expand Down
12 changes: 8 additions & 4 deletions src/Lucene.Net.Codecs/Pulsing/PulsingPostingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ public override void DecodeTerm(long[] empty, DataInput input, FieldInfo fieldIn
termState2.Absolute = termState2.Absolute || absolute;
// if we have positions, its total TF, otherwise its computed based on docFreq.
// TODO Double check this is right..
long count = fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
long count = IndexOptionsComparer.Default.Compare(fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0
? termState2.TotalTermFreq
: termState2.DocFreq;

Expand Down Expand Up @@ -335,7 +336,8 @@ public PulsingDocsEnum(FieldInfo fieldInfo)
{
_indexOptions = fieldInfo.IndexOptions;
_storePayloads = fieldInfo.HasPayloads;
_storeOffsets = _indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
_storeOffsets = IndexOptionsComparer.Default.Compare(_indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
}

public virtual PulsingDocsEnum Reset(IBits liveDocs, PulsingTermState termState)
Expand Down Expand Up @@ -385,7 +387,8 @@ public override int NextDoc()
_accum += (int)((uint)code >> 1); ; // shift off low bit
_freq = (code & 1) != 0 ? 1 : _postings.ReadVInt32();

if (_indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0)
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
if (IndexOptionsComparer.Default.Compare(_indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0)
{
// Skip positions
if (_storePayloads)
Expand Down Expand Up @@ -472,7 +475,8 @@ public PulsingDocsAndPositionsEnum(FieldInfo fieldInfo)
{
_indexOptions = fieldInfo.IndexOptions;
_storePayloads = fieldInfo.HasPayloads;
_storeOffsets = _indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
_storeOffsets = IndexOptionsComparer.Default.Compare(_indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
}

internal bool CanReuse(FieldInfo fieldInfo)
Expand Down
9 changes: 6 additions & 3 deletions src/Lucene.Net.Codecs/Pulsing/PulsingPostingsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ public override void FinishTerm(BlockTermState state)
// given codec wants to store other interesting
// stuff, it could use this pulsing codec to do so

if (_indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0)
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
if (IndexOptionsComparer.Default.Compare(_indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0)
{
var lastDocID = 0;
var pendingIDX = 0;
Expand Down Expand Up @@ -341,7 +342,8 @@ public override void FinishTerm(BlockTermState state)
_buffer.WriteVInt32(posDelta);
}

if (_indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0)
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
if (IndexOptionsComparer.Default.Compare(_indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0)
{
//System.out.println("write=" + pos.startOffset + "," + pos.endOffset);
var offsetDelta = pos.startOffset - lastOffset;
Expand Down Expand Up @@ -474,7 +476,8 @@ private void Push()
_wrappedPostingsWriter.StartTerm();

// Flush all buffered docs
if (_indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0)
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
if (IndexOptionsComparer.Default.Compare(_indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0)
{
Position doc = null;

Expand Down
3 changes: 2 additions & 1 deletion src/Lucene.Net.Codecs/Sep/SepPostingsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ public override int SetField(FieldInfo fieldInfo)
{
this.fieldInfo = fieldInfo;
this.indexOptions = fieldInfo.IndexOptions;
if (indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0)
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
if (IndexOptionsComparer.Default.Compare(indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0)
{
throw new NotSupportedException("this codec cannot index offsets");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Index;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -92,7 +93,8 @@ public override void Write(Directory directory, string segmentName, string segme

if (fi.IsIndexed)
{
if (Debugging.AssertsEnabled) Debugging.Assert(fi.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0 || !fi.HasPayloads);
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
if (Debugging.AssertsEnabled) Debugging.Assert(IndexOptionsComparer.Default.Compare(fi.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0 || !fi.HasPayloads);
SimpleTextUtil.Write(output, INDEXOPTIONS);
SimpleTextUtil.Write(output,
fi.IndexOptions != IndexOptions.NONE ? fi.IndexOptions.ToString() : string.Empty,
Expand Down
17 changes: 9 additions & 8 deletions src/Lucene.Net.Codecs/SimpleText/SimpleTextFieldsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Lucene.Net.Util.Fst;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using JCG = J2N.Collections.Generic;

Expand Down Expand Up @@ -209,8 +208,8 @@ public override DocsEnum Docs(IBits liveDocs, DocsEnum reuse, DocsFlags flags)

public override DocsAndPositionsEnum DocsAndPositions(IBits liveDocs, DocsAndPositionsEnum reuse, DocsAndPositionsFlags flags)
{

if (_indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0)
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
if (IndexOptionsComparer.Default.Compare(_indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0)
{
// Positions were not indexed
return null;
Expand Down Expand Up @@ -387,8 +386,9 @@ public virtual SimpleTextDocsAndPositionsEnum Reset(long fp, IBits liveDocs, Ind
_liveDocs = liveDocs;
_nextDocStart = fp;
_docId = -1;
_readPositions = indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
_readOffsets = indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
_readPositions = IndexOptionsComparer.Default.Compare(indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
_readOffsets = IndexOptionsComparer.Default.Compare(indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;

if (!_readOffsets)
{
Expand Down Expand Up @@ -673,11 +673,12 @@ public override TermsEnum GetEnumerator()

public override int DocCount => _docCount;

public override bool HasFreqs => _fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS) >= 0;
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
public override bool HasFreqs => IndexOptionsComparer.Default.Compare(_fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS) >= 0;

public override bool HasOffsets => _fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
public override bool HasOffsets => IndexOptionsComparer.Default.Compare(_fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;

public override bool HasPositions => _fieldInfo.IndexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
public override bool HasPositions => IndexOptionsComparer.Default.Compare(_fieldInfo.IndexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;

public override bool HasPayloads => _fieldInfo.HasPayloads;
}
Expand Down
13 changes: 7 additions & 6 deletions src/Lucene.Net.Codecs/SimpleText/SimpleTextFieldsWriter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Index;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;

namespace Lucene.Net.Codecs.SimpleText
Expand All @@ -23,11 +23,11 @@ namespace Lucene.Net.Codecs.SimpleText
* limitations under the License.
*/

using IndexOptions = Index.IndexOptions;
using BytesRef = Util.BytesRef;
using FieldInfo = Index.FieldInfo;
using SegmentWriteState = Index.SegmentWriteState;
using IndexOptions = Index.IndexOptions;
using IndexOutput = Store.IndexOutput;
using BytesRef = Util.BytesRef;
using SegmentWriteState = Index.SegmentWriteState;

internal class SimpleTextFieldsWriter : FieldsConsumer
{
Expand Down Expand Up @@ -117,8 +117,9 @@ public SimpleTextPostingsWriter(SimpleTextFieldsWriter outerInstance, FieldInfo
{
_outerInstance = outerInstance;
_indexOptions = field.IndexOptions;
_writePositions = _indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
_writeOffsets = _indexOptions.CompareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
// LUCENENET specific - to avoid boxing, changed from CompareTo() to IndexOptionsComparer.Compare()
_writePositions = IndexOptionsComparer.Default.Compare(_indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0;
_writeOffsets = IndexOptionsComparer.Default.Compare(_indexOptions, IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) >= 0;
}

public override void StartDoc(int docId, int termDocFreq)
Expand Down
Loading

0 comments on commit 3c01253

Please sign in to comment.