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

Move arrays to heap, to reduce stack usage #12250

Open
wants to merge 1 commit into
base: master
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
8 changes: 4 additions & 4 deletions src/libutil/archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ static void parseContents(CreateRegularFileSink & sink, Source & source)
sink.preallocateContents(size);

uint64_t left = size;
std::array<char, 65536> buf;
auto buf = std::make_unique<std::array<char, 65536>>();

while (left) {
checkInterrupt();
auto n = buf.size();
auto n = buf->size();
if ((uint64_t)n > left) n = left;
source(buf.data(), n);
sink({buf.data(), n});
source(buf->data(), n);
sink({buf->data(), n});
left -= n;
}

Expand Down
6 changes: 3 additions & 3 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,13 @@ void writeFile(const Path & path, Source & source, mode_t mode, bool sync)
if (!fd)
throw SysError("opening file '%1%'", path);

std::array<char, 64 * 1024> buf;
auto buf = std::make_unique<std::array<char, 64 * 1024>>();

try {
while (true) {
try {
auto n = source.read(buf.data(), buf.size());
writeFull(fd.get(), {buf.data(), n});
auto n = source.read(buf->data(), buf->size());
writeFull(fd.get(), {buf->data(), n});
} catch (EndOfFile &) { break; }
}
} catch (Error & e) {
Expand Down
6 changes: 3 additions & 3 deletions src/libutil/posix-source-accessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ void PosixSourceAccessor::readFile(

off_t left = st.st_size;

std::array<unsigned char, 64 * 1024> buf;
auto buf = std::make_unique<std::array<unsigned char, 64 * 1024>>();
while (left) {
checkInterrupt();
ssize_t rd = read(fromDescriptorReadOnly(fd.get()), buf.data(), (size_t) std::min(left, (off_t) buf.size()));
ssize_t rd = read(fromDescriptorReadOnly(fd.get()), buf->data(), (size_t) std::min(left, (off_t) buf->size()));
if (rd == -1) {
if (errno != EINTR)
throw SysError("reading from file '%s'", showPath(path));
Expand All @@ -76,7 +76,7 @@ void PosixSourceAccessor::readFile(
throw SysError("unexpected end-of-file reading '%s'", showPath(path));
else {
assert(rd <= left);
sink({(char *) buf.data(), (size_t) rd});
sink({(char *) buf->data(), (size_t) rd});
left -= rd;
}
}
Expand Down
Loading