-Refactored buffer pool

This commit is contained in:
James Newton-King
2015-12-13 15:40:37 +13:00
parent 4e2acac33a
commit 815b283e58
48 changed files with 187 additions and 186 deletions
@@ -54,7 +54,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Serializer
{
if (website == null)
{
throw new ArgumentNullException("website");
throw new ArgumentNullException(nameof(website));
}
Url = website.Url;
+22 -24
View File
@@ -1179,39 +1179,37 @@ third line", jsonTextReader.Value);
Assert.AreEqual("type", reader.Value);
}
public class FakeBufferPool : IJsonBufferPool<char>
public class FakeArrayPool : IArrayPool<char>
{
public readonly List<char[]> FreeBuffers = new List<char[]>();
public readonly List<char[]> UsedBuffers = new List<char[]>();
public readonly List<char[]> FreeArrays = new List<char[]>();
public readonly List<char[]> UsedArrays = new List<char[]>();
public char[] RentBuffer(int minSize)
public char[] Rent(int minimumLength)
{
char[] buffer = FreeBuffers.FirstOrDefault(b => b.Length >= minSize);
if (buffer != null)
char[] a = FreeArrays.FirstOrDefault(b => b.Length >= minimumLength);
if (a != null)
{
FreeBuffers.Remove(buffer);
UsedBuffers.Add(buffer);
FreeArrays.Remove(a);
UsedArrays.Add(a);
return buffer;
return a;
}
buffer = new char[minSize];
UsedBuffers.Add(buffer);
a = new char[minimumLength];
UsedArrays.Add(a);
return buffer;
return a;
}
public void ReturnBuffer(ref char[] buffer)
public void Return(char[] array)
{
if (UsedBuffers.Remove(buffer))
if (UsedArrays.Remove(array))
{
FreeBuffers.Add(buffer);
FreeArrays.Add(array);
// smallest first so the first buffer large enough is rented
FreeBuffers.Sort((b1, b2) => Comparer<int>.Default.Compare(b1.Length, b2.Length));
// smallest first so the first array large enough is rented
FreeArrays.Sort((b1, b2) => Comparer<int>.Default.Compare(b1.Length, b2.Length));
}
buffer = null;
}
}
@@ -1228,13 +1226,13 @@ third line", jsonTextReader.Value);
]
}";
FakeBufferPool bufferPool = new FakeBufferPool();
FakeArrayPool arrayPool = new FakeArrayPool();
for (int i = 0; i < 1000; i++)
{
using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
{
reader.BufferPool = bufferPool;
reader.ArrayPool = arrayPool;
while (reader.Read())
{
@@ -1243,12 +1241,12 @@ third line", jsonTextReader.Value);
if ((i + 1) % 100 == 0)
{
Console.WriteLine("Allocated buffers: " + bufferPool.FreeBuffers.Count);
Console.WriteLine("Allocated buffers: " + arrayPool.FreeArrays.Count);
}
}
Assert.AreEqual(0, bufferPool.UsedBuffers.Count);
Assert.AreEqual(6, bufferPool.FreeBuffers.Count);
Assert.AreEqual(0, arrayPool.UsedArrays.Count);
Assert.AreEqual(6, arrayPool.FreeArrays.Count);
}
[Test]
@@ -59,7 +59,7 @@ namespace Newtonsoft.Json.Tests
[Test]
public void BufferTest()
{
JsonTextReaderTest.FakeBufferPool bufferPool = new JsonTextReaderTest.FakeBufferPool();
JsonTextReaderTest.FakeArrayPool arrayPool = new JsonTextReaderTest.FakeArrayPool();
string longString = new string('A', 2000);
string longEscapedString = "Hello!" + new string('!', 50) + new string('\n', 1000) + "Good bye!";
@@ -71,7 +71,7 @@ namespace Newtonsoft.Json.Tests
using (JsonTextWriter writer = new JsonTextWriter(sw))
{
writer.BufferPool = bufferPool;
writer.ArrayPool = arrayPool;
writer.WriteStartObject();
@@ -92,12 +92,12 @@ namespace Newtonsoft.Json.Tests
if ((i + 1) % 100 == 0)
{
Console.WriteLine("Allocated buffers: " + bufferPool.FreeBuffers.Count);
Console.WriteLine("Allocated buffers: " + arrayPool.FreeArrays.Count);
}
}
Assert.AreEqual(0, bufferPool.UsedBuffers.Count);
Assert.AreEqual(3, bufferPool.FreeBuffers.Count);
Assert.AreEqual(0, arrayPool.UsedArrays.Count);
Assert.AreEqual(3, arrayPool.FreeArrays.Count);
}
[Test]
@@ -784,7 +784,7 @@ If attributes are not mentioned, default values are used in each case.
json = SerializeBinaryFormatter(value);
break;
default:
throw new ArgumentOutOfRangeException("method");
throw new ArgumentOutOfRangeException(nameof(method));
}
return json;
@@ -991,7 +991,7 @@ If attributes are not mentioned, default values are used in each case.
case SerializeMethod.DataContractJsonSerializer:
return DeserializeDataContractJson<T>((string)json);
default:
throw new ArgumentOutOfRangeException("method");
throw new ArgumentOutOfRangeException(nameof(method));
}
}
@@ -60,7 +60,7 @@ namespace Newtonsoft.Json.Tests.Schema
[TestFixture]
public class JsonSchemaSpecTests : TestFixtureBase
{
[TestCaseSourceAttribute("GetSpecTestDetails")]
[TestCaseSourceAttribute(nameof(GetSpecTestDetails))]
public void SpecTest(JsonSchemaSpecTest jsonSchemaSpecTest)
{
JsonSchema s = JsonSchema.Read(jsonSchemaSpecTest.Schema.CreateReader());
@@ -4514,7 +4514,7 @@ Path '', line 1, position 1.");
{
if (dictionary == null)
{
throw new ArgumentNullException("dictionary");
throw new ArgumentNullException(nameof(dictionary));
}
foreach (var entry in dictionary)
@@ -8534,7 +8534,7 @@ Path '', line 1, position 1.");
{
if (bar == null)
{
throw new ArgumentNullException("bar");
throw new ArgumentNullException(nameof(bar));
}
Bar = bar;