From: Raihaan Shouhell Date: Sun, 14 Apr 2024 07:48:44 +0000 (+0800) Subject: perf: Use copyfile(3) on apple systems (#1430) X-Git-Tag: v4.10~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=69d511f8aeced5aed9a438256668e604006f8aa0;p=thirdparty%2Fccache.git perf: Use copyfile(3) on apple systems (#1430) Use copyfile for more efficient copying on OSX. --- diff --git a/src/ccache/util/file.cpp b/src/ccache/util/file.cpp index 15d1905c..fd49aee9 100644 --- a/src/ccache/util/file.cpp +++ b/src/ccache/util/file.cpp @@ -70,6 +70,10 @@ # include #endif +#ifdef __APPLE__ +# include +#endif + namespace fs = util::filesystem; using pstr = util::PathString; @@ -105,7 +109,15 @@ copy_file(const fs::path& src, const fs::path& dest, ViaTmpFile via_tmp_file) } } -#if defined(HAVE_SYS_SENDFILE_H) +#if defined(__APPLE__) + copyfile_state_t state = copyfile_state_alloc(); + int n = fcopyfile(*src_fd, *dest_fd, state, COPYFILE_DATA); + copyfile_state_free(state); + if (n < 0) { + return tl::unexpected(FMT("Failed to copy: {} to {}", src, dest)); + } +#else +# if defined(HAVE_SYS_SENDFILE_H) DirEntry dir_entry(src, *src_fd); if (!dir_entry) { return tl::unexpected(FMT("Failed to stat {}: {}", src, strerror(errno))); @@ -125,12 +137,13 @@ copy_file(const fs::path& src, const fs::path& dest, ViaTmpFile via_tmp_file) } if (fallback_to_rw) { -#endif +# endif TRY(read_fd(*src_fd, [&](nonstd::span data) { write_fd(*dest_fd, data.data(), data.size()); })); -#if defined(HAVE_SYS_SENDFILE_H) +# if defined(HAVE_SYS_SENDFILE_H) } +# endif #endif dest_fd.close();