Skip to content

Commit

Permalink
Avoid redundant file copy when the file is newly allocated
Browse files Browse the repository at this point in the history
When buildAppendFilePart is called with chunk data as the source we need
to copy it as the lifetimes are decoupled; however for changed files,
we've already allocated a vector for it so it's a waste to copy it as we
can move instead.
  • Loading branch information
zeux committed Oct 27, 2024
1 parent b522d9c commit 62a4eb0
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ BuildContext* buildStart(Output* output, const char* path, unsigned int fileCoun
return context.release();
}

void buildAppendFilePart(BuildContext* context, const char* path, unsigned int startLine, const char* data, size_t dataSize, uint64_t timeStamp, uint64_t fileSize)
static void appendFilePart(BuildContext* context, const char* path, unsigned int startLine, const char* data, size_t dataSize, uint64_t timeStamp, uint64_t fileSize, std::vector<char>* dataSource)
{
if (!context->pendingFiles.empty() && context->pendingFiles.back().name == path)
{
Expand All @@ -623,7 +623,7 @@ void buildAppendFilePart(BuildContext* context, const char* path, unsigned int s
file.startLine = startLine;
file.timeStamp = timeStamp;
file.fileSize = fileSize;
file.contents = std::vector<char>(data, data + dataSize);
file.contents = dataSource ? std::move(*dataSource) : std::vector<char>(data, data + dataSize);

context->pendingFiles.emplace_back(file);
context->pendingSize += dataSize;
Expand All @@ -639,6 +639,11 @@ void buildAppendFilePart(BuildContext* context, const char* path, unsigned int s
}
}

void buildAppendFilePart(BuildContext* context, const char* path, unsigned int startLine, const char* data, size_t dataSize, uint64_t timeStamp, uint64_t fileSize)
{
appendFilePart(context, path, startLine, data, dataSize, timeStamp, fileSize, nullptr);
}

bool buildAppendFile(BuildContext* context, const char* path, uint64_t timeStamp, uint64_t fileSize)
{
FileStream in(path, "rb");
Expand All @@ -652,7 +657,7 @@ bool buildAppendFile(BuildContext* context, const char* path, uint64_t timeStamp
{
std::vector<char> contents = convertToUTF8(readFile(in));

buildAppendFilePart(context, path, 0, contents.empty() ? 0 : &contents[0], contents.size(), timeStamp, fileSize);
appendFilePart(context, path, 0, contents.empty() ? 0 : &contents[0], contents.size(), timeStamp, fileSize, &contents);

return true;
}
Expand Down

0 comments on commit 62a4eb0

Please sign in to comment.