Skip to content

Add buffered line reads to BufferedSource#14845

Merged
xokdvium merged 1 commit intoNixOS:masterfrom
Zaczero:zaczero/BufferedSource--readLine
Dec 27, 2025
Merged

Add buffered line reads to BufferedSource#14845
xokdvium merged 1 commit intoNixOS:masterfrom
Zaczero:zaczero/BufferedSource--readLine

Conversation

@Zaczero
Copy link
Member

@Zaczero Zaczero commented Dec 21, 2025

Provide BufferedSource::readLine for opt-in buffered line reading. Migrate applicable call sites.

Succeeds #14829

Motivation

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@xokdvium
Copy link
Contributor

Any chance we could use Pipe class in tests?

@xokdvium
Copy link
Contributor

xokdvium commented Dec 22, 2025

Also could we test this code with different buffer sizes? To catch various corner-cases. Maybe make a parametric test fixture and feed random data (mt19937 maybe?) into the source and test all against an unbuffered version via a StringSource (or does that not take a Source &? In that case we can just di a temp file). This buffer handling is pretty hard, so I want us to get it 100% right

@Zaczero Zaczero force-pushed the zaczero/BufferedSource--readLine branch 2 times, most recently from 98b425c to 185bbe0 Compare December 25, 2025 09:16
@Zaczero
Copy link
Member Author

Zaczero commented Dec 25, 2025

I added buffer oriented tests


#include <cstring>

#ifndef _WIN32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally the ifdef should be redundant, since there's a windows-specific implemntation of Pipe (not sure how well tested it is).

std::string BufferedSource::readLine(bool eofOk)
{
if (!buffer)
buffer = decltype(buffer)(new char[bufSize]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably use this, but that's just stylistic nitpick and pre-existing, so just a nice-to-have.

Suggested change
buffer = decltype(buffer)(new char[bufSize]);
buffer = std::make_unique_for_overwrite<char[]>(bufSize);

Comment on lines +150 to +152
auto * start = buffer.get() + bufPosOut;
auto * end = buffer.get() + bufPosIn;
if (auto * newline = static_cast<char *>(memchr(start, '\n', end - start))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope we could wrap this in a helper method that returns a std::span. This manual handling of bounds is quite tricky.

Comment on lines +168 to +171
bufPosOut = bufPosIn = 0;
if (eofOk)
return line;
throw EndOfFile("unexpected EOF reading a line");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and below could maybe get deduplicated with a lambda, but that's short enough to maybe not warrant that.

Provide BufferedSource::readLine for opt-in buffered line reading. Migrate applicable call sites.
@xokdvium xokdvium force-pushed the zaczero/BufferedSource--readLine branch from 185bbe0 to b813ed2 Compare December 27, 2025 20:11
@xokdvium
Copy link
Contributor

Fixed those minor nitpicks that I had.

@xokdvium xokdvium enabled auto-merge December 27, 2025 20:26
@xokdvium xokdvium added this pull request to the merge queue Dec 27, 2025
Merged via the queue into NixOS:master with commit 8093df5 Dec 27, 2025
16 checks passed
@Zaczero Zaczero deleted the zaczero/BufferedSource--readLine branch December 28, 2025 05:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants