From 62a4eb0d11efe7bf325d3e1d208fed8c26b83e74 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 26 Oct 2024 21:47:33 -0700 Subject: [PATCH] Avoid redundant file copy when the file is newly allocated 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. --- src/build.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/build.cpp b/src/build.cpp index 17034f3..42a8ae6 100644 --- a/src/build.cpp +++ b/src/build.cpp @@ -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* dataSource) { if (!context->pendingFiles.empty() && context->pendingFiles.back().name == path) { @@ -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(data, data + dataSize); + file.contents = dataSource ? std::move(*dataSource) : std::vector(data, data + dataSize); context->pendingFiles.emplace_back(file); context->pendingSize += dataSize; @@ -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"); @@ -652,7 +657,7 @@ bool buildAppendFile(BuildContext* context, const char* path, uint64_t timeStamp { std::vector 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; }