Skip to content

Commit

Permalink
PERFORMANCE: Eliminated several calls to FirstOrDefault(), LastOrDefa…
Browse files Browse the repository at this point in the history
…ult(), Skip(), First(), and Last() (see #261)
  • Loading branch information
NightOwl888 committed Jul 29, 2020
1 parent 17c12be commit 61bd539
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/Lucene.Net.Analysis.Kuromoji/JapaneseTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using Console = Lucene.Net.Util.SystemConsole;

Expand Down Expand Up @@ -379,7 +378,7 @@ public override bool IncrementToken()
Parse();
}

Token token = pending.LastOrDefault();
Token token = pending[pending.Count - 1]; // LUCENENET: The above loop ensures we don't get here unless we have at least 1 item
if (token != null)
{
pending.Remove(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using JCG = J2N.Collections.Generic;


namespace Lucene.Net.Analysis.Phonetic.Language.Bm
{
/*
Expand Down Expand Up @@ -483,7 +481,7 @@ public virtual string Encode(string input, LanguageSet languageSet)
{
// not a multi-word name
//input = words.iterator().next();
input = words.FirstOrDefault();
input = words[0];
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions src/Lucene.Net.Benchmark/Quality/Utils/DocNameExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Lucene.Net.Benchmarks.Quality.Utils
{
Expand Down Expand Up @@ -52,7 +51,7 @@ public virtual string DocName(IndexSearcher searcher, int docid)
IList<string> name = new List<string>();
searcher.IndexReader.Document(docid, new StoredFieldVisitorAnonymousHelper(this, name));

return name.FirstOrDefault();
return name.Count > 0 ? name[0] : null;
}

private class StoredFieldVisitorAnonymousHelper : StoredFieldVisitor
Expand Down
5 changes: 2 additions & 3 deletions src/Lucene.Net.Grouping/SearchGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using JCG = J2N.Collections.Generic;

namespace Lucene.Net.Search.Grouping
Expand Down Expand Up @@ -390,7 +389,7 @@ private void UpdateNextGroup(int topN, ShardIter<T> shard)
// Prune un-competitive groups:
while (queue.Count > topN)
{
MergedGroup<T> group = queue.Last();
MergedGroup<T> group = queue.Max;
queue.Remove(group);
//System.out.println("PRUNE: " + group);
group.IsInQueue = false;
Expand Down Expand Up @@ -421,7 +420,7 @@ public virtual ICollection<SearchGroup<T>> Merge(IList<IEnumerable<ISearchGroup<

while (queue.Count != 0)
{
MergedGroup<T> group = queue.First();
MergedGroup<T> group = queue.Min;
queue.Remove(group);
group.IsProcessed = true;
//System.out.println(" pop: shards=" + group.shards + " group=" + (group.groupValue == null ? "null" : (((BytesRef) group.groupValue).utf8ToString())) + " sortValues=" + Arrays.toString(group.topValues));
Expand Down
18 changes: 12 additions & 6 deletions src/Lucene.Net.QueryParser/Surround/Query/NotQuery.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Lucene.Net.Search;
using System.Collections.Generic;
using System.Linq;

namespace Lucene.Net.QueryParsers.Surround.Query
{
Expand Down Expand Up @@ -34,12 +33,19 @@ public NotQuery(IList<SrndQuery> queries, string opName)
public override Search.Query MakeLuceneQueryFieldNoBoost(string fieldName, BasicQueryFactory qf)
{
var luceneSubQueries = MakeLuceneSubQueriesField(fieldName, qf);
BooleanQuery bq = new BooleanQuery();
bq.Add(luceneSubQueries.FirstOrDefault(), Occur.MUST);
BooleanQuery bq = new BooleanQuery
{
{ luceneSubQueries.Count > 0 ? luceneSubQueries[0] : null, Occur.MUST }
};

// LUCENENET: SubList() is slow, so we do an array copy operation instead
var luceneSubQueriesArray = new Search.Query[luceneSubQueries.Count - 1];
for (int i = 1, j = 0; i < luceneSubQueries.Count; i++, j++)
luceneSubQueriesArray[j] = luceneSubQueries[i];

SrndBooleanQuery.AddQueriesToBoolean(bq,
// FIXME: do not allow weights on prohibited subqueries.
//luceneSubQueries.subList(1, luceneSubQueries.size()),
luceneSubQueries.Skip(1).ToList(),
// FIXME: do not allow weights on prohibited subqueries.
luceneSubQueriesArray,
// later subqueries: not required, prohibited
Occur.MUST_NOT);
return bq;
Expand Down
3 changes: 1 addition & 2 deletions src/Lucene.Net/Store/LockStressTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
Expand Down Expand Up @@ -78,7 +77,7 @@ public static void Main(string[] args)
int count = Convert.ToInt32(args[arg++], CultureInfo.InvariantCulture);

IPAddress[] addresses = Dns.GetHostAddressesAsync(verifierHost).Result;
IPAddress addr = addresses.FirstOrDefault();
IPAddress addr = addresses.Length > 0 ? addresses[0] : null;

Type c;
try
Expand Down

0 comments on commit 61bd539

Please sign in to comment.