Skip to content

Commit

Permalink
RecSplit.add_key: use ByteView
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr committed Mar 17, 2024
1 parent 7151cb5 commit 08c4dc9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
8 changes: 4 additions & 4 deletions silkworm/db/snapshots/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,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;
}

Expand Down Expand Up @@ -251,7 +251,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++;
}

Expand Down Expand Up @@ -280,7 +280,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;
}

Expand Down
26 changes: 14 additions & 12 deletions silkworm/db/snapshots/rec_split/rec_split.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
#include <gsl/util>

#include <silkworm/core/common/assert.hpp>
#include <silkworm/core/common/bytes.hpp>
#include <silkworm/core/common/bytes_to_string.hpp>
#include <silkworm/core/common/endian.hpp>
#include <silkworm/core/common/math.hpp>
#include <silkworm/core/common/util.hpp>
Expand Down Expand Up @@ -418,17 +420,17 @@ class RecSplit {
}
}

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
Expand Down Expand Up @@ -649,20 +651,20 @@ 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 MPHF mapping
std::size_t operator()(const std::string& key) const { return operator()(string_view_to_byte_view(key)); }

//! Search result: value position and flag indicating if found or not
using LookupResult = std::pair<std::size_t, bool>;

//! Return the value associated with the given key within the index
[[nodiscard]] LookupResult lookup(ByteView key) const { return lookup(key.data(), key.size()); }

//! Return the value associated with the given key within the index
[[nodiscard]] LookupResult lookup(const std::string& key) const { return lookup(key.data(), key.size()); }
[[nodiscard]] LookupResult 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
LookupResult lookup(const void* key, const size_t length) const {
const hash128_t& hashed_key{murmur_hash_3(key, length)};
LookupResult lookup(ByteView key) const {
const hash128_t& hashed_key{murmur_hash_3(key)};
const auto record = operator()(hashed_key);
const auto position = 1 + 8 + bytes_per_record_ * (record + 1);

Expand Down Expand Up @@ -891,9 +893,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;
}

Expand Down

0 comments on commit 08c4dc9

Please sign in to comment.