Skip to content

Commit

Permalink
Lucene.Net.Util.Automaton: Fixed State class to initialize and trim m…
Browse files Browse the repository at this point in the history
…ore efficiently (#295, #261). Fixes the performance of Lucene.Net.Util.Automaton.TestBasicOperations::TestEmptyLanguageConcatenate().
  • Loading branch information
NightOwl888 committed Jun 30, 2020
1 parent b66279b commit 867834b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
8 changes: 7 additions & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
-->
<Project>
<!-- Features in .NET Standard and .NET Core only (no .NET Framework support) -->
<PropertyGroup Condition=" $(TargetFramework.StartsWith('netstandard')) Or $(TargetFramework.StartsWith('netcoreapp')) ">
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_ARRAYEMPTY</DefineConstants>
<DebugType>portable</DebugType>
</PropertyGroup>

Expand Down Expand Up @@ -66,7 +68,11 @@

</PropertyGroup>

<!-- Features in .NET Framework 4.5+ only -->
<!-- Features in .NET Framework 4.6+ only -->
<PropertyGroup Condition="$(TargetFramework.StartsWith('net46')) Or $(TargetFramework.StartsWith('net47')) Or $(TargetFramework.StartsWith('net48'))">
<DefineConstants>$(DefineConstants);FEATURE_ARRAYEMPTY</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework.StartsWith('net4'))">

<DefineConstants>$(DefineConstants);FEATURE_SERIALIZABLE_EXCEPTIONS</DefineConstants>
Expand Down
20 changes: 13 additions & 7 deletions src/Lucene.Net/Util/Automaton/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ public class State : IComparable<State>, IEquatable<State> // LUCENENET specific
[SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")]
public Transition[] TransitionsArray
{
get { return transitionsArray; }
get => transitionsArray;
// LUCENENET NOTE: Setter removed because it is apparently not in use outside of this class
}
private Transition[] transitionsArray;
internal int numTransitions; // LUCENENET NOTE: Made internal because we already have a public property for access
#if FEATURE_ARRAYEMPTY
private Transition[] transitionsArray = Array.Empty<Transition>();
#else
private Transition[] transitionsArray = new Transition[0];
#endif
internal int numTransitions = 0;// LUCENENET NOTE: Made internal because we already have a public property for access

internal int number;

Expand All @@ -64,7 +68,7 @@ public Transition[] TransitionsArray
/// </summary>
public State()
{
ResetTransitions();
//ResetTransitions(); // LUCENENET: Let class initializer set these
id = next_id++;
}

Expand All @@ -73,7 +77,11 @@ public State()
/// </summary>
internal void ResetTransitions()
{
#if FEATURE_ARRAYEMPTY
transitionsArray = Array.Empty<Transition>();
#else
transitionsArray = new Transition[0];
#endif
numTransitions = 0;
}

Expand Down Expand Up @@ -242,9 +250,7 @@ public virtual void TrimTransitionsArray()
{
if (numTransitions < transitionsArray.Length)
{
Transition[] newArray = new Transition[numTransitions];
Array.Copy(transitionsArray, 0, newArray, 0, numTransitions);
transitionsArray = newArray;
Array.Resize(ref transitionsArray, numTransitions); // LUCENENET: Resize rather than copy
}
}

Expand Down

0 comments on commit 867834b

Please sign in to comment.