]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
perf: Use util::Bytes
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 7 Sep 2022 09:22:36 +0000 (11:22 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 21 Sep 2022 15:06:27 +0000 (17:06 +0200)
src/core/Result.cpp
src/core/ResultExtractor.cpp
src/storage/Storage.cpp
src/storage/Storage.hpp
src/storage/secondary/FileStorage.cpp
src/storage/secondary/HttpStorage.cpp
src/storage/secondary/RedisStorage.cpp
src/storage/secondary/SecondaryStorage.hpp

index 43526f83dd9eedcc7b018788fdd508ad4821d437..d50e2ef30054f0569a5fa986250e6d05dbb5fa47 100644 (file)
@@ -33,6 +33,7 @@
 #include <core/exceptions.hpp>
 #include <core/wincompat.hpp>
 #include <fmtmacros.hpp>
+#include <util/Bytes.hpp>
 #include <util/file.hpp>
 #include <util/path.hpp>
 #include <util/string.hpp>
@@ -298,7 +299,7 @@ Serializer::serialize(std::vector<uint8_t>& output)
                                          std::get<std::string>(entry.data));
     } else if (is_file_entry) {
       const auto& path = std::get<std::string>(entry.data);
-      const auto data = util::read_file<std::vector<uint8_t>>(path);
+      const auto data = util::read_file<util::Bytes>(path);
       if (!data) {
         throw Error(FMT("Failed to read {}: {}", path, data.error()));
       }
index d88146f0b380beddf5a109e07e8e1db36751f7e9..4afcdc131ed4d8f41da6076569c20b9907e59588 100644 (file)
@@ -24,6 +24,7 @@
 #include <core/exceptions.hpp>
 #include <core/wincompat.hpp>
 #include <fmtmacros.hpp>
+#include <util/Bytes.hpp>
 #include <util/file.hpp>
 
 #include <fcntl.h>
@@ -80,8 +81,7 @@ ResultExtractor::on_raw_file(uint8_t file_number,
                     file_size));
   }
 
-  const auto data =
-    util::read_file<std::vector<uint8_t>>(raw_file_path, file_size);
+  const auto data = util::read_file<util::Bytes>(raw_file_path, file_size);
   if (!data) {
     throw Error(FMT("Failed to read {}: {}", raw_file_path, data.error()));
   }
index 8d26a34aa271c50115bad977f397fd17384b4ae9..90172876feb368a69b6e6a8a151cc0fdd4cb9efd 100644 (file)
@@ -32,6 +32,7 @@
 #ifdef HAVE_REDIS_STORAGE_BACKEND
 #  include <storage/secondary/RedisStorage.hpp>
 #endif
+#include <util/Bytes.hpp>
 #include <util/Timer.hpp>
 #include <util/Tokenizer.hpp>
 #include <util/XXH3_64.hpp>
@@ -246,7 +247,7 @@ Storage::get(const Digest& key,
           m_secondary_storages.end(),
           [](const auto& entry) { return !entry->config.read_only; });
         if (should_put_in_secondary_storage) {
-          const auto value = util::read_file<std::vector<uint8_t>>(*path);
+          const auto value = util::read_file<util::Bytes>(*path);
           if (!value) {
             LOG("Failed to read {}: {}", *path, value.error());
             return path; // Don't indicate failure since primary storage was OK.
@@ -309,7 +310,7 @@ Storage::put(const Digest& key,
                 m_secondary_storages.end(),
                 [](const auto& entry) { return !entry->config.read_only; });
   if (should_put_in_secondary_storage) {
-    const auto value = util::read_file<std::vector<uint8_t>>(*path);
+    const auto value = util::read_file<util::Bytes>(*path);
     if (!value) {
       LOG("Failed to read {}: {}", *path, value.error());
       return true; // Don't indicate failure since primary storage was OK.
@@ -469,7 +470,7 @@ Storage::get_backend(SecondaryStorageEntry& entry,
   }
 }
 
-std::optional<std::vector<uint8_t>>
+std::optional<util::Bytes>
 Storage::get_from_secondary_storage(const Digest& key)
 {
   MTR_SCOPE("secondary_storage", "get");
@@ -510,7 +511,7 @@ Storage::get_from_secondary_storage(const Digest& key)
 
 void
 Storage::put_in_secondary_storage(const Digest& key,
-                                  const std::vector<uint8_t>& value,
+                                  nonstd::span<const uint8_t> value,
                                   bool only_if_missing)
 {
   MTR_SCOPE("secondary_storage", "put");
index 4eac498fc015093c5e721b2361d7c7cba76a96b6..5dee65a0d1543967403c30979a4e1b0f2400718d 100644 (file)
@@ -23,6 +23,8 @@
 #include <storage/secondary/SecondaryStorage.hpp>
 #include <storage/types.hpp>
 
+#include <third_party/nonstd/span.hpp>
+
 #include <functional>
 #include <memory>
 #include <optional>
@@ -81,11 +83,10 @@ private:
               std::string_view operation_description,
               const bool for_writing);
 
-  std::optional<std::vector<uint8_t>>
-  get_from_secondary_storage(const Digest& key);
+  std::optional<util::Bytes> get_from_secondary_storage(const Digest& key);
 
   void put_in_secondary_storage(const Digest& key,
-                                const std::vector<uint8_t>& value,
+                                nonstd::span<const uint8_t> value,
                                 bool only_if_missing);
 
   void remove_from_secondary_storage(const Digest& key);
index 38c0f8b1917530a0dc8d7e090b0e5cc45758aa33..91f301a1c30f443e5967ffebcba8c4ddbf1dda56 100644 (file)
@@ -26,6 +26,7 @@
 #include <assertions.hpp>
 #include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
+#include <util/Bytes.hpp>
 #include <util/expected.hpp>
 #include <util/file.hpp>
 #include <util/string.hpp>
@@ -43,11 +44,11 @@ class FileStorageBackend : public SecondaryStorage::Backend
 public:
   FileStorageBackend(const Params& params);
 
-  nonstd::expected<std::optional<std::vector<uint8_t>>, Failure>
+  nonstd::expected<std::optional<util::Bytes>, Failure>
   get(const Digest& key) override;
 
   nonstd::expected<bool, Failure> put(const Digest& key,
-                                      const std::vector<uint8_t>& value,
+                                      nonstd::span<const uint8_t> value,
                                       bool only_if_missing) override;
 
   nonstd::expected<bool, Failure> remove(const Digest& key) override;
@@ -103,8 +104,7 @@ FileStorageBackend::FileStorageBackend(const Params& params)
   }
 }
 
-nonstd::expected<std::optional<std::vector<uint8_t>>,
-                 SecondaryStorage::Backend::Failure>
+nonstd::expected<std::optional<util::Bytes>, SecondaryStorage::Backend::Failure>
 FileStorageBackend::get(const Digest& key)
 {
   const auto path = get_entry_path(key);
@@ -121,7 +121,7 @@ FileStorageBackend::get(const Digest& key)
     util::set_timestamps(path);
   }
 
-  auto value = util::read_file<std::vector<uint8_t>>(path);
+  auto value = util::read_file<util::Bytes>(path);
   if (!value) {
     LOG("Failed to read {}: {}", path, value.error());
     return nonstd::make_unexpected(Failure::error);
@@ -131,7 +131,7 @@ FileStorageBackend::get(const Digest& key)
 
 nonstd::expected<bool, SecondaryStorage::Backend::Failure>
 FileStorageBackend::put(const Digest& key,
-                        const std::vector<uint8_t>& value,
+                        const nonstd::span<const uint8_t> value,
                         const bool only_if_missing)
 {
   const auto path = get_entry_path(key);
index ba99485d897f38df794bcf8ac7fbbf5c17d2ae94..98748c9a85adf2043c156527b1d6fddcb7f92633 100644 (file)
@@ -41,11 +41,11 @@ class HttpStorageBackend : public SecondaryStorage::Backend
 public:
   HttpStorageBackend(const Params& params);
 
-  nonstd::expected<std::optional<std::vector<uint8_t>>, Failure>
+  nonstd::expected<std::optional<util::Bytes>, Failure>
   get(const Digest& key) override;
 
   nonstd::expected<bool, Failure> put(const Digest& key,
-                                      const std::vector<uint8_t>& value,
+                                      nonstd::span<const uint8_t> value,
                                       bool only_if_missing) override;
 
   nonstd::expected<bool, Failure> remove(const Digest& key) override;
@@ -144,8 +144,7 @@ HttpStorageBackend::HttpStorageBackend(const Params& params)
   m_http_client.set_write_timeout(operation_timeout);
 }
 
-nonstd::expected<std::optional<std::vector<uint8_t>>,
-                 SecondaryStorage::Backend::Failure>
+nonstd::expected<std::optional<util::Bytes>, SecondaryStorage::Backend::Failure>
 HttpStorageBackend::get(const Digest& key)
 {
   const auto url_path = get_entry_path(key);
@@ -164,12 +163,12 @@ HttpStorageBackend::get(const Digest& key)
     return std::nullopt;
   }
 
-  return std::vector<uint8_t>(result->body.begin(), result->body.end());
+  return util::Bytes(result->body.data(), result->body.size());
 }
 
 nonstd::expected<bool, SecondaryStorage::Backend::Failure>
 HttpStorageBackend::put(const Digest& key,
-                        const std::vector<uint8_t>& value,
+                        const nonstd::span<const uint8_t> value,
                         const bool only_if_missing)
 {
   const auto url_path = get_entry_path(key);
index 5f03f1e60d348def91318e55d2b57c69df202c4f..43928386fbd01b54c7b5e0062e9e2632868f6ed5 100644 (file)
@@ -60,11 +60,11 @@ class RedisStorageBackend : public SecondaryStorage::Backend
 public:
   RedisStorageBackend(const SecondaryStorage::Backend::Params& params);
 
-  nonstd::expected<std::optional<std::vector<uint8_t>>, Failure>
+  nonstd::expected<std::optional<util::Bytes>, Failure>
   get(const Digest& key) override;
 
   nonstd::expected<bool, Failure> put(const Digest& key,
-                                      const std::vector<uint8_t>& value,
+                                      nonstd::span<const uint8_t> value,
                                       bool only_if_missing) override;
 
   nonstd::expected<bool, Failure> remove(const Digest& key) override;
@@ -157,8 +157,7 @@ is_timeout(int err)
 #endif
 }
 
-nonstd::expected<std::optional<std::vector<uint8_t>>,
-                 SecondaryStorage::Backend::Failure>
+nonstd::expected<std::optional<util::Bytes>, SecondaryStorage::Backend::Failure>
 RedisStorageBackend::get(const Digest& key)
 {
   const auto key_string = get_key_string(key);
@@ -167,7 +166,7 @@ RedisStorageBackend::get(const Digest& key)
   if (!reply) {
     return nonstd::make_unexpected(reply.error());
   } else if ((*reply)->type == REDIS_REPLY_STRING) {
-    return std::vector<uint8_t>((*reply)->str, (*reply)->str + (*reply)->len);
+    return util::Bytes((*reply)->str, (*reply)->len);
   } else if ((*reply)->type == REDIS_REPLY_NIL) {
     return std::nullopt;
   } else {
@@ -178,7 +177,7 @@ RedisStorageBackend::get(const Digest& key)
 
 nonstd::expected<bool, SecondaryStorage::Backend::Failure>
 RedisStorageBackend::put(const Digest& key,
-                         const std::vector<uint8_t>& value,
+                         nonstd::span<const uint8_t> value,
                          bool only_if_missing)
 {
   const auto key_string = get_key_string(key);
index 08110517e8aa1e6837f3a16359fa7c1c2dd91d62..ffa3f972220271708981742a0f12fa20cae06327 100644 (file)
 #pragma once
 
 #include <storage/types.hpp>
+#include <util/Bytes.hpp>
 
 #include <third_party/nonstd/expected.hpp>
+#include <third_party/nonstd/span.hpp>
 #include <third_party/url.hpp>
 
 #include <chrono>
@@ -78,7 +80,7 @@ public:
 
     // Get the value associated with `key`. Returns the value on success or
     // std::nullopt if the entry is not present.
-    virtual nonstd::expected<std::optional<std::vector<uint8_t>>, Failure>
+    virtual nonstd::expected<std::optional<util::Bytes>, Failure>
     get(const Digest& key) = 0;
 
     // Put `value` associated to `key` in the storage. A true `only_if_missing`
@@ -86,7 +88,7 @@ public:
     // Returns true if the entry was stored, otherwise false.
     virtual nonstd::expected<bool, Failure>
     put(const Digest& key,
-        const std::vector<uint8_t>& value,
+        nonstd::span<const uint8_t> value,
         bool only_if_missing = false) = 0;
 
     // Remove `key` and its associated value. Returns true if the entry was