StringSink pre allocate
When used with `readFile`, we have a pretty good heuristic of the file size, so `reserve` this in the `string`. This will save some allocation / copy when the string is growing.
This commit is contained in:
parent
7afcb5af98
commit
2e5be2a749
|
@ -312,7 +312,11 @@ unsigned char getFileType(const Path & path)
|
||||||
|
|
||||||
string readFile(int fd)
|
string readFile(int fd)
|
||||||
{
|
{
|
||||||
return drainFD(fd, true);
|
struct stat st;
|
||||||
|
if (fstat(fd, &st) == -1)
|
||||||
|
throw SysError("statting file");
|
||||||
|
|
||||||
|
return drainFD(fd, true, st.st_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -658,9 +662,9 @@ void writeFull(int fd, const string & s, bool allowInterrupts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
string drainFD(int fd, bool block)
|
string drainFD(int fd, bool block, const size_t reserveSize)
|
||||||
{
|
{
|
||||||
StringSink sink;
|
StringSink sink(reserveSize);
|
||||||
drainFD(fd, sink, block);
|
drainFD(fd, sink, block);
|
||||||
return std::move(*sink.s);
|
return std::move(*sink.s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,7 +162,7 @@ MakeError(EndOfFile, Error);
|
||||||
|
|
||||||
|
|
||||||
/* Read a file descriptor until EOF occurs. */
|
/* Read a file descriptor until EOF occurs. */
|
||||||
string drainFD(int fd, bool block = true);
|
string drainFD(int fd, bool block = true, const size_t reserveSize=0);
|
||||||
|
|
||||||
void drainFD(int fd, Sink & sink, bool block = true);
|
void drainFD(int fd, Sink & sink, bool block = true);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue