From 94c0dad4bc80b38c45ee9f263dbaf35ffb180b9e Mon Sep 17 00:00:00 2001 From: nothingTVatYT Date: Mon, 18 Oct 2021 04:11:28 +0200 Subject: [PATCH 1/2] optimize file copy to sendfile on Linux --- .../Engine/Platform/Linux/LinuxFileSystem.cpp | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/Source/Engine/Platform/Linux/LinuxFileSystem.cpp b/Source/Engine/Platform/Linux/LinuxFileSystem.cpp index d0d58f69c..14412a10b 100644 --- a/Source/Engine/Platform/Linux/LinuxFileSystem.cpp +++ b/Source/Engine/Platform/Linux/LinuxFileSystem.cpp @@ -12,6 +12,7 @@ #include "Engine/Utilities/StringConverter.h" #include #include +#include #include #include #include @@ -342,6 +343,7 @@ bool LinuxFileSystem::CopyFile(const StringView& dst, const StringView& src) char buffer[4096]; ssize_t readSize; int cachedError; + off_t offset = 0; srcFile = open(srcANSI.Get(), O_RDONLY); if (srcFile < 0) @@ -350,24 +352,37 @@ bool LinuxFileSystem::CopyFile(const StringView& dst, const StringView& src) if (dstFile < 0) goto out_error; - while (readSize = read(srcFile, buffer, sizeof(buffer)), readSize > 0) + // first try the kernel method + struct stat statBuf; + fstat(srcFile, &statBuf); + readSize = 1; + while (readSize > 0) { - char* ptr = buffer; - ssize_t writeSize; - - do + readSize = sendfile(dstFile, srcFile, 0, statBuf.st_size); + } + // sendfile could fail for example if the input file is not nmap'able + // in this case we fall back to the read/write loop + if (readSize < 0) + { + while (readSize = read(srcFile, buffer, sizeof(buffer)), readSize > 0) { - writeSize = write(dstFile, ptr, readSize); - if (writeSize >= 0) + char* ptr = buffer; + ssize_t writeSize; + + do { - readSize -= writeSize; - ptr += writeSize; - } - else if (errno != EINTR) - { - goto out_error; - } - } while (readSize > 0); + writeSize = write(dstFile, ptr, readSize); + if (writeSize >= 0) + { + readSize -= writeSize; + ptr += writeSize; + } + else if (errno != EINTR) + { + goto out_error; + } + } while (readSize > 0); + } } if (readSize == 0) From ae24641e07d5ea6711dba434255137557addacf1 Mon Sep 17 00:00:00 2001 From: nothingTVatYT Date: Mon, 18 Oct 2021 04:15:07 +0200 Subject: [PATCH 2/2] offset is not used --- Source/Engine/Platform/Linux/LinuxFileSystem.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Engine/Platform/Linux/LinuxFileSystem.cpp b/Source/Engine/Platform/Linux/LinuxFileSystem.cpp index 14412a10b..79522495c 100644 --- a/Source/Engine/Platform/Linux/LinuxFileSystem.cpp +++ b/Source/Engine/Platform/Linux/LinuxFileSystem.cpp @@ -343,7 +343,6 @@ bool LinuxFileSystem::CopyFile(const StringView& dst, const StringView& src) char buffer[4096]; ssize_t readSize; int cachedError; - off_t offset = 0; srcFile = open(srcANSI.Get(), O_RDONLY); if (srcFile < 0)