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 761f670 commit ae2fb1d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
8 changes: 4 additions & 4 deletions silkworm/db/snapshots/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

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

Expand Down
24 changes: 13 additions & 11 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 @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit ae2fb1d

Please sign in to comment.