From: Joel Rosdahl Date: Wed, 7 Sep 2022 09:22:36 +0000 (+0200) Subject: perf: Use util::Bytes X-Git-Tag: v4.7~67 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1db166ecd8d19f7f3b6c26e249e21768b2a7edc2;p=thirdparty%2Fccache.git perf: Use util::Bytes --- diff --git a/src/core/Result.cpp b/src/core/Result.cpp index 43526f83d..d50e2ef30 100644 --- a/src/core/Result.cpp +++ b/src/core/Result.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -298,7 +299,7 @@ Serializer::serialize(std::vector& output) std::get(entry.data)); } else if (is_file_entry) { const auto& path = std::get(entry.data); - const auto data = util::read_file>(path); + const auto data = util::read_file(path); if (!data) { throw Error(FMT("Failed to read {}: {}", path, data.error())); } diff --git a/src/core/ResultExtractor.cpp b/src/core/ResultExtractor.cpp index d88146f0b..4afcdc131 100644 --- a/src/core/ResultExtractor.cpp +++ b/src/core/ResultExtractor.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -80,8 +81,7 @@ ResultExtractor::on_raw_file(uint8_t file_number, file_size)); } - const auto data = - util::read_file>(raw_file_path, file_size); + const auto data = util::read_file(raw_file_path, file_size); if (!data) { throw Error(FMT("Failed to read {}: {}", raw_file_path, data.error())); } diff --git a/src/storage/Storage.cpp b/src/storage/Storage.cpp index 8d26a34aa..90172876f 100644 --- a/src/storage/Storage.cpp +++ b/src/storage/Storage.cpp @@ -32,6 +32,7 @@ #ifdef HAVE_REDIS_STORAGE_BACKEND # include #endif +#include #include #include #include @@ -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>(*path); + const auto value = util::read_file(*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>(*path); + const auto value = util::read_file(*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::optional 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& value, + nonstd::span value, bool only_if_missing) { MTR_SCOPE("secondary_storage", "put"); diff --git a/src/storage/Storage.hpp b/src/storage/Storage.hpp index 4eac498fc..5dee65a0d 100644 --- a/src/storage/Storage.hpp +++ b/src/storage/Storage.hpp @@ -23,6 +23,8 @@ #include #include +#include + #include #include #include @@ -81,11 +83,10 @@ private: std::string_view operation_description, const bool for_writing); - std::optional> - get_from_secondary_storage(const Digest& key); + std::optional get_from_secondary_storage(const Digest& key); void put_in_secondary_storage(const Digest& key, - const std::vector& value, + nonstd::span value, bool only_if_missing); void remove_from_secondary_storage(const Digest& key); diff --git a/src/storage/secondary/FileStorage.cpp b/src/storage/secondary/FileStorage.cpp index 38c0f8b19..91f301a1c 100644 --- a/src/storage/secondary/FileStorage.cpp +++ b/src/storage/secondary/FileStorage.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -43,11 +44,11 @@ class FileStorageBackend : public SecondaryStorage::Backend public: FileStorageBackend(const Params& params); - nonstd::expected>, Failure> + nonstd::expected, Failure> get(const Digest& key) override; nonstd::expected put(const Digest& key, - const std::vector& value, + nonstd::span value, bool only_if_missing) override; nonstd::expected remove(const Digest& key) override; @@ -103,8 +104,7 @@ FileStorageBackend::FileStorageBackend(const Params& params) } } -nonstd::expected>, - SecondaryStorage::Backend::Failure> +nonstd::expected, 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>(path); + auto value = util::read_file(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 FileStorageBackend::put(const Digest& key, - const std::vector& value, + const nonstd::span value, const bool only_if_missing) { const auto path = get_entry_path(key); diff --git a/src/storage/secondary/HttpStorage.cpp b/src/storage/secondary/HttpStorage.cpp index ba99485d8..98748c9a8 100644 --- a/src/storage/secondary/HttpStorage.cpp +++ b/src/storage/secondary/HttpStorage.cpp @@ -41,11 +41,11 @@ class HttpStorageBackend : public SecondaryStorage::Backend public: HttpStorageBackend(const Params& params); - nonstd::expected>, Failure> + nonstd::expected, Failure> get(const Digest& key) override; nonstd::expected put(const Digest& key, - const std::vector& value, + nonstd::span value, bool only_if_missing) override; nonstd::expected remove(const Digest& key) override; @@ -144,8 +144,7 @@ HttpStorageBackend::HttpStorageBackend(const Params& params) m_http_client.set_write_timeout(operation_timeout); } -nonstd::expected>, - SecondaryStorage::Backend::Failure> +nonstd::expected, 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(result->body.begin(), result->body.end()); + return util::Bytes(result->body.data(), result->body.size()); } nonstd::expected HttpStorageBackend::put(const Digest& key, - const std::vector& value, + const nonstd::span value, const bool only_if_missing) { const auto url_path = get_entry_path(key); diff --git a/src/storage/secondary/RedisStorage.cpp b/src/storage/secondary/RedisStorage.cpp index 5f03f1e60..43928386f 100644 --- a/src/storage/secondary/RedisStorage.cpp +++ b/src/storage/secondary/RedisStorage.cpp @@ -60,11 +60,11 @@ class RedisStorageBackend : public SecondaryStorage::Backend public: RedisStorageBackend(const SecondaryStorage::Backend::Params& params); - nonstd::expected>, Failure> + nonstd::expected, Failure> get(const Digest& key) override; nonstd::expected put(const Digest& key, - const std::vector& value, + nonstd::span value, bool only_if_missing) override; nonstd::expected remove(const Digest& key) override; @@ -157,8 +157,7 @@ is_timeout(int err) #endif } -nonstd::expected>, - SecondaryStorage::Backend::Failure> +nonstd::expected, 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((*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 RedisStorageBackend::put(const Digest& key, - const std::vector& value, + nonstd::span value, bool only_if_missing) { const auto key_string = get_key_string(key); diff --git a/src/storage/secondary/SecondaryStorage.hpp b/src/storage/secondary/SecondaryStorage.hpp index 08110517e..ffa3f9722 100644 --- a/src/storage/secondary/SecondaryStorage.hpp +++ b/src/storage/secondary/SecondaryStorage.hpp @@ -19,8 +19,10 @@ #pragma once #include +#include #include +#include #include #include @@ -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>, Failure> + virtual nonstd::expected, 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 put(const Digest& key, - const std::vector& value, + nonstd::span value, bool only_if_missing = false) = 0; // Remove `key` and its associated value. Returns true if the entry was