Skip to content

Use both ends of the pipe in parallel #5

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
31 changes: 28 additions & 3 deletions src/FileIO/WithPipeLines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,33 @@ public async Task<int> ProcessFileAsync(string filePath, Employee[] employeeReco
if (!File.Exists(filePath)) return position;
await using var fileStream = File.OpenRead(filePath);
var pipeReader = PipeReader.Create(fileStream);
return await ReadFromPipe(pipeReader, employeeRecords, position);
}

public async Task<int> ProcessWithFullPipeAsync(string filePath, Employee[] employeeRecords)
{
var position = 0;
if (!File.Exists(filePath)) return position;
await using var fileStream = File.OpenRead(filePath);
Pipe p = new();
var fillPipe = FillPipe(fileStream, p.Writer);
var readPipe = ReadFromPipe(p.Reader, employeeRecords, position);
await Task.WhenAll(fillPipe, readPipe);

return await readPipe;
}

private static async Task<int> ReadFromPipe(PipeReader pipeReader, Employee[] employeeRecords, int position)
{
int pos = position;
while (true)
{
var fileData = await pipeReader.ReadAsync();

// convert to Buffer
var fileDataBuffer = fileData.Buffer;

var sequencePosition = ParseLines(employeeRecords, fileDataBuffer, ref position);
var sequencePosition = ParseLines(employeeRecords, fileDataBuffer, ref pos);

pipeReader.AdvanceTo(sequencePosition, fileDataBuffer.End);

Expand All @@ -40,8 +59,14 @@ public async Task<int> ProcessFileAsync(string filePath, Employee[] employeeReco
}
}

await pipeReader.CompleteAsync(); // marking pipereader as Completed
return position;
await pipeReader.CompleteAsync(); // marking pipe reader as Completed
return pos;
}

private async Task FillPipe(FileStream fileStream, PipeWriter writer)
{
await fileStream.CopyToAsync(writer.AsStream());
await writer.CompleteAsync();
}

private static SequencePosition ParseLines(Employee[] employeeRecords, in ReadOnlySequence<byte> buffer, ref int position)
Expand Down
17 changes: 17 additions & 0 deletions tests/FileIO.Benchmarks/FileIOTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ public async Task PipeLines()
}
}

[Benchmark]
public async Task FullPipe()
{
var pool = ArrayPool<Employee>.Shared;
var employeeRecords = pool.Rent(100000);
var pipeLinesTest = new WithPipeLines();

try
{
await pipeLinesTest.ProcessWithFullPipeAsync(_filePath, employeeRecords);
}
finally
{
pool.Return(employeeRecords, clearArray: true);
}
}

[Benchmark]
public async Task<IList<Employee>> AsyncStream()
{
Expand Down