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

feature/write-extensions-split #83

Closed
wants to merge 1 commit 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
116 changes: 91 additions & 25 deletions RecordParser/Extensions/FileWriter/WriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace RecordParser.Extensions.FileWriter
{
using RecordParser.Extensions.FileReader;
using System.Threading.Tasks;

/// <summary>
/// Delegate representing object to text convert method.
Expand Down Expand Up @@ -49,7 +50,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 @@ -64,40 +72,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 @@ -107,7 +173,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
Loading