Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/write extension refactor #88

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 93 additions & 26 deletions RecordParser/Extensions/FileWriter/WriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace RecordParser.Extensions
{

/// <summary>
/// Delegate representing object to text convert method.
/// </summary>
Expand Down Expand Up @@ -47,7 +49,14 @@ public static void WriteRecords<T>(this TextWriter textWriter, IEnumerable<T> it
{
if (options.Enabled)
{
WriteParallel(textWriter, items, tryFormat, options);
if (options.EnsureOriginalOrdering)
{
WriteParallelOrdered(textWriter, items, tryFormat, options);
}
else
{
WriteParallelUnordered(textWriter, items, tryFormat, options);
}
}
else
{
Expand All @@ -62,40 +71,98 @@ private class BufferContext
public object lockObj;
}

private static void WriteParallel<T>(TextWriter textWriter, IEnumerable<T> items, TryFormat<T> tryFormat, ParallelismOptions options)
private static void WriteParallelUnordered<T>(TextWriter textWriter, IEnumerable<T> items, TryFormat<T> tryFormat, ParallelismOptions options)
{
var parallelism = 20; // TODO remove hardcoded

var buffers = Enumerable
.Range(0, parallelism)
.Select(_ => new BufferContext
{
pow = 10,
buffer = ArrayPool<char>.Shared.Rent((int)Math.Pow(2, 10)),
lockObj = new object()
})
.ToArray();

var textWriterLock = new object();
var opts = new ParallelOptions();
if (options.MaxDegreeOfParallelism is { } max)
opts.MaxDegreeOfParallelism = max;

try
{
Parallel.ForEach(items, opts, (item, _, i) =>
{
var x = buffers[i % parallelism];

lock (x.lockObj)
{
retry:

if (tryFormat(item, x.buffer, out var charsWritten))
{
lock (textWriterLock)
{
textWriter.WriteLine(x.buffer, 0, charsWritten);
}
}
else
{
ArrayPool<char>.Shared.Return(x.buffer);
x.pow++;
x.buffer = ArrayPool<char>.Shared.Rent((int)Math.Pow(2, x.pow));
goto retry;
}
}
});
}
finally
{
foreach (var x in buffers)
ArrayPool<char>.Shared.Return(x.buffer);
}
}

private static void WriteParallelOrdered<T>(TextWriter textWriter, IEnumerable<T> items, TryFormat<T> tryFormat, ParallelismOptions options)
{
var initialPool = 20;
var pool = new Stack<char[]>(initialPool);

for (var index = 0; index < initialPool; index++)
pool.Push(ArrayPool<char>.Shared.Rent((int)Math.Pow(2, initialPow)));

var xs = items.AsParallel(options).Select((item, i) =>
try
{
var buffer = Pop() ?? ArrayPool<char>.Shared.Rent((int)Math.Pow(2, initialPow));
retry:
for (var index = 0; index < initialPool; index++)
pool.Push(ArrayPool<char>.Shared.Rent((int)Math.Pow(2, initialPow)));

if (tryFormat(item, buffer, out var charsWritten))
var xs = items.AsParallel(options).Select((item, i) =>
{
return (buffer, charsWritten);
}
else
var buffer = Pop();
retry:

if (tryFormat(item, buffer, out var charsWritten))
{
return (buffer, charsWritten);
}
else
{
ArrayPool<char>.Shared.Return(buffer);
buffer = ArrayPool<char>.Shared.Rent(buffer.Length * 2);
goto retry;
}
});

foreach (var x in xs)
{
ArrayPool<char>.Shared.Return(buffer);
buffer = ArrayPool<char>.Shared.Rent(buffer.Length * 2);
goto retry;
textWriter.WriteLine(x.buffer, 0, x.charsWritten);
Push(x.buffer);
}
});

foreach (var x in xs)
{
textWriter.WriteLine(x.buffer, 0, x.charsWritten);
Push(x.buffer);
}

foreach (var x in pool)
finally
{
ArrayPool<char>.Shared.Return(x);
foreach (var x in pool)
{
ArrayPool<char>.Shared.Return(x);
}
}

pool.Clear();
Expand All @@ -105,7 +172,7 @@ char[] Pop()
char[] x;
lock (pool)
pool.TryPop(out x);
return x;
return x ?? ArrayPool<char>.Shared.Rent((int)Math.Pow(2, initialPow));
}

void Push(char[] item)
Expand Down Expand Up @@ -146,4 +213,4 @@ private static void WriteSequential<T>(TextWriter textWriter, IEnumerable<T> ite
}
}
}
}
}
Loading