Skip to content

Commit

Permalink
Style, optimize link copy elision.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jun 11, 2024
1 parent 6fb504d commit df05f40
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
33 changes: 17 additions & 16 deletions include/bitcoin/database/impl/primitives/hashmap.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ TEMPLATE
bool CLASS::create() NOEXCEPT
{
Link count{};
return head_.create() &&
head_.get_body_count(count) && manager_.truncate(count);
return head_.create() && head_.get_body_count(count) &&
manager_.truncate(count);
}

TEMPLATE
Expand All @@ -58,16 +58,16 @@ TEMPLATE
bool CLASS::restore() NOEXCEPT
{
Link count{};
return head_.verify() &&
head_.get_body_count(count) && manager_.truncate(count);
return head_.verify() && head_.get_body_count(count) &&
manager_.truncate(count);
}

TEMPLATE
bool CLASS::verify() const NOEXCEPT
{
Link count{};
return head_.verify() &&
head_.get_body_count(count) && count == manager_.count();
return head_.verify() && head_.get_body_count(count) &&
(count == manager_.count());
}

// sizing
Expand Down Expand Up @@ -130,7 +130,10 @@ code CLASS::reload() NOEXCEPT
TEMPLATE
Link CLASS::top(const Link& link) const NOEXCEPT
{
return link < head_.buckets() ? head_.top(link) : Link{};
if (link >= head_.buckets())
return {};

return head_.top(link);
}

TEMPLATE
Expand Down Expand Up @@ -165,12 +168,10 @@ TEMPLATE
Key CLASS::get_key(const Link& link) NOEXCEPT
{
const auto ptr = manager_.get(link);

// As with link, search key is presumed valid (otherwise null array).
if (!ptr || system::is_lesser(ptr->size(), Link::size + key_size))
if (!ptr || system::is_lesser(ptr->size(), index_size))
return {};

return system::unsafe_array_cast<uint8_t, key_size>(std::next(
return system::unsafe_array_cast<uint8_t, array_count<Key>>(std::next(
ptr->begin(), Link::size));
}

Expand All @@ -185,7 +186,7 @@ bool CLASS::get(const Link& link, Element& element) const NOEXCEPT

iostream stream{ *ptr };
reader source{ stream };
source.skip_bytes(Link::size + key_size);
source.skip_bytes(index_size);

if constexpr (!is_slab) { source.set_limit(Size); }
return element.from_data(source);
Expand All @@ -203,7 +204,7 @@ bool CLASS::get(const iterator& it, Element& element) const NOEXCEPT
const auto buffer = ptr->offset(iterator::link_to_position(it.self()));
iostream stream{ buffer, buffer_size };
reader source{ stream };
source.skip_bytes(Link::size + key_size);
source.skip_bytes(index_size);

if constexpr (!is_slab) { source.set_limit(Size); }
return element.from_data(source);
Expand All @@ -220,7 +221,7 @@ bool CLASS::set(const Link& link, const Element& element) NOEXCEPT

iostream stream{ *ptr };
finalizer sink{ stream };
sink.skip_bytes(Link::size + key_size);
sink.skip_bytes(index_size);

if constexpr (!is_slab) { sink.set_limit(Size); }
return element.to_data(sink);
Expand Down Expand Up @@ -322,8 +323,8 @@ bool CLASS::commit(const Link& link, const Key& key) NOEXCEPT
return false;

// Set element search key.
system::unsafe_array_cast<uint8_t, key_size>(std::next(ptr->begin(),
Link::size)) = key;
system::unsafe_array_cast<uint8_t, array_count<Key>>(
std::next(ptr->begin(), Link::size)) = key;

// Commit element to search index.
auto& next = system::unsafe_array_cast<uint8_t, Link::size>(ptr->begin());
Expand Down
5 changes: 2 additions & 3 deletions include/bitcoin/database/primitives/hashmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,10 @@ class hashmap
Link commit_link(const Link& link, const Key& key) NOEXCEPT;

private:
static constexpr auto key_size = array_count<Key>;
static constexpr auto is_slab = (Size == max_size_t);
static constexpr auto index_size = Link::size + array_count<Key>;
static constexpr auto buffer_size = is_slab ? system::maximum<ptrdiff_t> :
system::possible_narrow_and_sign_cast<ptrdiff_t>(
Size + Link::size + key_size);
system::possible_narrow_and_sign_cast<ptrdiff_t>(Size + index_size);

using head = database::head<Link, Key, Hash>;
using manager = database::manager<Link, Key, Size>;
Expand Down

0 comments on commit df05f40

Please sign in to comment.