From ae2fb1d5717f896168aeafd895adacf38733f384 Mon Sep 17 00:00:00 2001 From: battlmonstr Date: Sat, 16 Mar 2024 19:04:07 +0100 Subject: [PATCH] RecSplit.add_key: use ByteView --- silkworm/db/snapshots/index.cpp | 8 +++---- silkworm/db/snapshots/rec_split/rec_split.hpp | 24 ++++++++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/silkworm/db/snapshots/index.cpp b/silkworm/db/snapshots/index.cpp index 5b9b514fa6..30e70c6fd9 100644 --- a/silkworm/db/snapshots/index.cpp +++ b/silkworm/db/snapshots/index.cpp @@ -73,14 +73,14 @@ bool HeaderIndex::walk(RecSplit8& rec_split, uint64_t i, uint64_t offset, ByteVi const ethash::hash256 hash = keccak256(rlp_encoded_header); ensure(hash.bytes[0] == first_hash_byte, [&]() { return "HeaderIndex: invalid prefix=" + to_hex(first_hash_byte) + " hash=" + to_hex(hash.bytes); }); - rec_split.add_key(hash.bytes, kHashLength, offset); + rec_split.add_key(ByteView{hash.bytes}, offset); return true; } bool BodyIndex::walk(RecSplit8& rec_split, uint64_t i, uint64_t offset, ByteView /*word*/) { Bytes uint64_buffer; seg::varint::encode(uint64_buffer, i); - rec_split.add_key(uint64_buffer.data(), uint64_buffer.size(), offset); + rec_split.add_key(uint64_buffer, offset); return true; } @@ -249,7 +249,7 @@ void TransactionIndex::build() { throw std::runtime_error{error.str()}; } - tx_hash_to_block_rs.add_key(tx_hash.bytes, kHashLength, block_number); + tx_hash_to_block_rs.add_key(tx_hash, block_number); i++; } @@ -278,7 +278,7 @@ bool TransactionIndex::walk(RecSplit8& rec_split, uint64_t i, uint64_t offset, B throw std::runtime_error{error.str()}; } - rec_split.add_key(tx_hash.bytes, kHashLength, offset); + rec_split.add_key(tx_hash, offset); return true; } diff --git a/silkworm/db/snapshots/rec_split/rec_split.hpp b/silkworm/db/snapshots/rec_split/rec_split.hpp index 61c88475fe..be89854af6 100644 --- a/silkworm/db/snapshots/rec_split/rec_split.hpp +++ b/silkworm/db/snapshots/rec_split/rec_split.hpp @@ -65,6 +65,8 @@ #include #include +#include +#include #include #include #include @@ -389,17 +391,17 @@ class RecSplit { building_strategy_->add_key(bucket_id, bucket_key, offset); } - void add_key(const void* key_data, const size_t key_length, uint64_t offset) { + void add_key(ByteView key, uint64_t offset) { if (built_) { throw std::logic_error{"cannot add key after perfect hash function has been built"}; } - const auto key_hash = murmur_hash_3(key_data, key_length); + const auto key_hash = murmur_hash_3(key); add_key(key_hash, offset); } void add_key(const std::string& key, uint64_t offset) { - add_key(key.c_str(), key.size(), offset); + add_key(string_view_to_byte_view(key), offset); } //! Build the MPHF using the RecSplit algorithm and save the resulting index file @@ -608,17 +610,17 @@ class RecSplit { } //! Return the value associated with the given key within the MPHF mapping - std::size_t operator()(const std::string& key) const { return operator()(murmur_hash_3(key.c_str(), key.size())); } + std::size_t operator()(ByteView key) const { return operator()(murmur_hash_3(key)); } - //! Return the value associated with the given key within the index - [[nodiscard]] std::size_t lookup(ByteView key) const { return lookup(key.data(), key.size()); } + //! Return the value associated with the given key within the MPHF mapping + std::size_t operator()(const std::string& key) const { return operator()(string_view_to_byte_view(key)); } //! Return the value associated with the given key within the index - [[nodiscard]] std::size_t lookup(const std::string& key) const { return lookup(key.data(), key.size()); } + [[nodiscard]] std::size_t lookup(const std::string& key) const { return lookup(string_view_to_byte_view(key)); } //! Return the value associated with the given key within the index - std::size_t lookup(const void* key, const size_t length) const { - const auto record = operator()(murmur_hash_3(key, length)); + std::size_t lookup(ByteView key) const { + const auto record = operator()(key); const auto position = 1 + 8 + bytes_per_record_ * (record + 1); const auto region = encoded_file_->region(); @@ -842,9 +844,9 @@ class RecSplit { } } - hash128_t inline murmur_hash_3(const void* data, const size_t length) const { + hash128_t inline murmur_hash_3(ByteView data) const { hash128_t h{}; - hasher_->hash_x64_128(data, length, &h); + hasher_->hash_x64_128(data.data(), data.size(), &h); return h; }