You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2020/07/15 02:30:00 UTC
[lucenenet] 04/14: Lucene.Net.Codecs.SimpleText: Using decimal is
30% faster than using BigInteger for add and subtract
This is an automated email from the ASF dual-hosted git repository.
nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 2bd7ab608d0b7124a48823b72355c0764ca95432
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Mon Jul 13 03:45:55 2020 +0700
Lucene.Net.Codecs.SimpleText: Using decimal is 30% faster than using BigInteger for add and subtract
---
src/Lucene.Net.Codecs/Lucene.Net.Codecs.csproj | 4 ----
.../SimpleText/SimpleTextDocValuesReader.cs | 17 ++++-------------
.../SimpleText/SimpleTextDocValuesWriter.cs | 15 ++++++---------
3 files changed, 10 insertions(+), 26 deletions(-)
diff --git a/src/Lucene.Net.Codecs/Lucene.Net.Codecs.csproj b/src/Lucene.Net.Codecs/Lucene.Net.Codecs.csproj
index ff19d8d..6a5a819 100644
--- a/src/Lucene.Net.Codecs/Lucene.Net.Codecs.csproj
+++ b/src/Lucene.Net.Codecs/Lucene.Net.Codecs.csproj
@@ -38,8 +38,4 @@
<ProjectReference Include="..\Lucene.Net\Lucene.Net.csproj" />
</ItemGroup>
- <ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
- <Reference Include="System.Numerics" />
- </ItemGroup>
-
</Project>
diff --git a/src/Lucene.Net.Codecs/SimpleText/SimpleTextDocValuesReader.cs b/src/Lucene.Net.Codecs/SimpleText/SimpleTextDocValuesReader.cs
index 7bc497b..a5a1086 100644
--- a/src/Lucene.Net.Codecs/SimpleText/SimpleTextDocValuesReader.cs
+++ b/src/Lucene.Net.Codecs/SimpleText/SimpleTextDocValuesReader.cs
@@ -4,7 +4,6 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
-using System.Numerics;
using System.Text;
namespace Lucene.Net.Codecs.SimpleText
@@ -182,20 +181,12 @@ namespace Lucene.Net.Codecs.SimpleText
_input.Seek(_field.DataStartFilePointer + (1 + _field.Pattern.Length + 2) * docId);
SimpleTextUtil.ReadLine(_input, _scratch);
-
- decimal bd;
- try
- {
- // LUCNENENET: .NET doesn't have a way to specify a pattern with decimal, but all of the standard ones are built in.
- bd = decimal.Parse(_scratch.Utf8ToString(), NumberStyles.Float, CultureInfo.InvariantCulture);
- }
- catch (FormatException ex)
- {
- throw new CorruptIndexException("failed to parse long value (resource=" + _input + ")", ex);
- }
+ // LUCNENENET: .NET doesn't have a way to specify a pattern with decimal, but all of the standard ones are built in.
+ if (!decimal.TryParse(_scratch.Utf8ToString(), NumberStyles.Float, CultureInfo.InvariantCulture, out decimal bd))
+ throw new CorruptIndexException("failed to parse long value (resource=" + _input + ")");
SimpleTextUtil.ReadLine(_input, _scratch); // read the line telling us if its real or not
- return (long)BigInteger.Add(new BigInteger(_field.MinValue), new BigInteger(bd));
+ return (long)((decimal)_field.MinValue + bd); // LUCENENET specific - use decimal rather than BigInteger
}
catch (IOException ioe)
{
diff --git a/src/Lucene.Net.Codecs/SimpleText/SimpleTextDocValuesWriter.cs b/src/Lucene.Net.Codecs/SimpleText/SimpleTextDocValuesWriter.cs
index bc0c966..1c544ea 100644
--- a/src/Lucene.Net.Codecs/SimpleText/SimpleTextDocValuesWriter.cs
+++ b/src/Lucene.Net.Codecs/SimpleText/SimpleTextDocValuesWriter.cs
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
-using System.Numerics;
using System.Text;
using JCG = J2N.Collections.Generic;
@@ -97,22 +96,20 @@ namespace Lucene.Net.Codecs.SimpleText
SimpleTextUtil.WriteNewline(data);
// build up our fixed-width "simple text packed ints" format
- BigInteger maxBig = maxValue;
- BigInteger minBig = minValue;
- var diffBig = BigInteger.Subtract(maxBig, minBig);
+ var diffBig = (decimal)maxValue - (decimal)minValue; // LUCENENET specific - use decimal rather than BigInteger
var maxBytesPerValue = diffBig.ToString(CultureInfo.InvariantCulture).Length;
var sb = new StringBuilder();
for (var i = 0; i < maxBytesPerValue; i++)
sb.Append('0');
-
+
+ var patternString = sb.ToString(); // LUCENENET specific - only get the string once
+
// write our pattern to the .dat
SimpleTextUtil.Write(data, PATTERN);
- SimpleTextUtil.Write(data, sb.ToString(), scratch);
+ SimpleTextUtil.Write(data, patternString, scratch);
SimpleTextUtil.WriteNewline(data);
- var patternString = sb.ToString();
-
int numDocsWritten = 0;
// second pass to write the values
@@ -122,7 +119,7 @@ namespace Lucene.Net.Codecs.SimpleText
Debug.Assert(value >= minValue);
- var delta = BigInteger.Subtract(value, minValue);
+ var delta = (decimal)value - (decimal)minValue; // LUCENENET specific - use decimal rather than BigInteger
string s = delta.ToString(patternString, CultureInfo.InvariantCulture);
Debug.Assert(s.Length == patternString.Length);
SimpleTextUtil.Write(data, s, scratch);