Merge pull request #269 from plaisted/builderpool

Remove locks / SB pooling during CoreTokenScanner parsing
This commit is contained in:
Eliot Jones 2021-01-20 08:28:37 -04:00 committed by GitHub
commit 660ac5f486
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 13 deletions

View File

@ -8,7 +8,7 @@
internal class NumericTokenizer : ITokenizer
{
private static readonly StringBuilderPool StringBuilderPool = new StringBuilderPool(10);
private readonly StringBuilder stringBuilder = new StringBuilder();
private const byte Zero = 48;
private const byte Nine = 57;
@ -23,7 +23,7 @@
if ((currentByte >= Zero && currentByte <= Nine) || currentByte == '-' || currentByte == '+' || currentByte == '.')
{
characters = StringBuilderPool.Borrow();
characters = stringBuilder;
characters.Append((char)currentByte);
}
else
@ -53,7 +53,7 @@
try
{
var str = characters.ToString();
StringBuilderPool.Return(characters);
characters.Clear();
switch (str)
{

View File

@ -1,11 +1,12 @@
namespace UglyToad.PdfPig.Tokenization
{
using Core;
using System.Text;
using Tokens;
internal class PlainTokenizer : ITokenizer
{
private static readonly StringBuilderPool StringBuilderPool = new StringBuilderPool(10);
private readonly StringBuilder stringBuilder = new StringBuilder();
public bool ReadsNextByte { get; } = true;
@ -18,7 +19,7 @@
return false;
}
var builder = StringBuilderPool.Borrow();
var builder = stringBuilder;
builder.Append((char)currentByte);
while (inputBytes.MoveNext())
{
@ -39,7 +40,7 @@
}
var text = builder.ToString();
StringBuilderPool.Return(builder);
builder.Clear();
switch (text)
{

View File

@ -15,9 +15,12 @@
private static readonly DictionaryTokenizer DictionaryTokenizer = new DictionaryTokenizer();
private static readonly HexTokenizer HexTokenizer = new HexTokenizer();
private static readonly NameTokenizer NameTokenizer = new NameTokenizer();
private static readonly NumericTokenizer NumericTokenizer = new NumericTokenizer();
private static readonly PlainTokenizer PlainTokenizer = new PlainTokenizer();
private static readonly StringTokenizer StringTokenizer = new StringTokenizer();
// NOTE: these are not thread safe so should not be static. Each instance includes a
// StringBuilder it re-uses.
private readonly PlainTokenizer PlainTokenizer = new PlainTokenizer();
private readonly NumericTokenizer NumericTokenizer = new NumericTokenizer();
private readonly StringTokenizer StringTokenizer = new StringTokenizer();
private readonly ScannerScope scope;
private readonly IInputBytes inputBytes;

View File

@ -6,7 +6,7 @@
internal class StringTokenizer : ITokenizer
{
private static readonly StringBuilderPool StringBuilderPool = new StringBuilderPool(16);
private readonly StringBuilder stringBuilder = new StringBuilder();
public bool ReadsNextByte { get; } = false;
public bool TryTokenize(byte currentByte, IInputBytes inputBytes, out IToken token)
@ -23,7 +23,7 @@
return false;
}
var builder = StringBuilderPool.Borrow();
var builder = stringBuilder;
var numberOfBrackets = 1;
var isEscapeActive = false;
var isLineBreaking = false;
@ -178,7 +178,7 @@
encodedWith = StringToken.Encoding.Iso88591;
}
StringBuilderPool.Return(builder);
builder.Clear();
token = new StringToken(tokenStr, encodedWith);

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net451;net452;net46;net461;net462;net47</TargetFrameworks>
<LangVersion>latest</LangVersion>