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

[API Proposal]: Add File.ReadAllLinesAsync methods that return IAsyncEnumerable<string> #60719

Closed
eiriktsarpalis opened this issue Oct 21, 2021 · 4 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.IO untriaged New issue has not been triaged by the area owner

Comments

@eiriktsarpalis
Copy link
Member

Background and motivation

The existing File.ReadAllLinesAsync method returns a Task<string[]> meaning that the entire contents will have to be loaded in-memory before the task completes. This might be unsuitable in cases where we need to lazily process very large files on a line-by-line basis. It seems like IAsyncEnumerable<string> might be the ideal abstraction to achieve this.

API Proposal

namespace System.IO
{
    public partial static class File
    {
        public static IAsyncEnumerable<string> ReadLinesAsAsyncEnumerable(string path, CancellationToken cancellationToken = default);
        public static IAsyncEnumerable<string> ReadLinesAsAsyncEnumerable(string path, Encoding encoding, CancellationToken cancellationToken = default);
    }
}

API Usage

await foreach(string line in File.ReadLinesAsAsyncEnumerable("myHugeFile.txt"))
{
     yield return JsonSerializer.Deserialize<MyPoco>(line);
}

Alternative Designs

Should we consider accepting a custom separator parameter? (instead of Environment.NewLine)

Risks

No response

@eiriktsarpalis eiriktsarpalis added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Oct 21, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Oct 21, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost
Copy link

ghost commented Oct 21, 2021

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

The existing File.ReadAllLinesAsync method returns a Task<string[]> meaning that the entire contents will have to be loaded in-memory before the task completes. This might be unsuitable in cases where we need to lazily process very large files on a line-by-line basis. It seems like IAsyncEnumerable<string> might be the ideal abstraction to achieve this.

API Proposal

namespace System.IO
{
    public partial static class File
    {
        public static IAsyncEnumerable<string> ReadLinesAsAsyncEnumerable(string path, CancellationToken cancellationToken = default);
        public static IAsyncEnumerable<string> ReadLinesAsAsyncEnumerable(string path, Encoding encoding, CancellationToken cancellationToken = default);
    }
}

API Usage

await foreach(string line in File.ReadLinesAsAsyncEnumerable("myHugeFile.txt"))
{
     yield return JsonSerializer.Deserialize<MyPoco>(line);
}

Alternative Designs

Should we consider accepting a custom separator parameter? (instead of Environment.NewLine)

Risks

No response

Author: eiriktsarpalis
Assignees: -
Labels:

api-suggestion, area-System.IO, untriaged

Milestone: -

@jozkee
Copy link
Member

jozkee commented Oct 21, 2021

I think this is duplicate of #2214.

@eiriktsarpalis
Copy link
Member Author

Oh, cool! For some reason it didn't turn up in my issue search.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.IO untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants