]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Optimize read_file size_hint on Windows 1418/head
authorClemens Wasser <clemens.wasser@gmail.com>
Wed, 20 Mar 2024 12:40:58 +0000 (13:40 +0100)
committerClemens Wasser <clemens.wasser@gmail.com>
Wed, 20 Mar 2024 13:24:49 +0000 (14:24 +0100)
src/ccache/util/file.cpp

index 13daa53784f3d36a9405cfd582b26f478ebb644b..5ea8cbe9c69597633a8874fa0159d66fba108290 100644 (file)
 #include <type_traits>
 #include <vector>
 
+#ifdef _WIN32
+#  include <io.h>
+#  include <windows.h>
+#endif
+
 namespace fs = util::filesystem;
 
 using pstr = util::PathString;
@@ -229,6 +234,7 @@ template<typename T>
 tl::expected<T, std::string>
 read_file(const fs::path& path, size_t size_hint)
 {
+#ifndef _WIN32
   if (size_hint == 0) {
     DirEntry de(path);
     if (!de) {
@@ -236,6 +242,7 @@ read_file(const fs::path& path, size_t size_hint)
     }
     size_hint = de.size();
   }
+#endif
 
   if (size_hint > std::numeric_limits<size_t>::max() / 4) {
     // Too large value on a 32-bit system won't work well, better bail.
@@ -255,6 +262,15 @@ read_file(const fs::path& path, size_t size_hint)
     return tl::unexpected(strerror(errno));
   }
 
+#ifdef _WIN32
+  if (size_hint == 0) {
+    LARGE_INTEGER file_size;
+    GetFileSizeEx(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())),
+                  &file_size);
+    size_hint = (size_t)file_size.QuadPart;
+  }
+#endif
+
   int64_t ret = 0;
   size_t pos = 0;
   T result;