Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace string_view with string in RaxTreeMap::FindIterator #3982

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/core/search/rax_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cassert>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>

Expand Down Expand Up @@ -72,7 +73,7 @@ template <typename V> struct RaxTreeMap {
};

// Result of find() call. Inherits from pair to mimic iterator interface, not incrementable.
struct FindIterator : public std::optional<std::pair<std::string_view, V&>> {
struct FindIterator : public std::optional<std::pair<std::string, V&>> {
bool operator==(const SeekIterator& rhs) const {
if (this->has_value() != !bool(rhs.it_.flags & RAX_ITER_EOF))
return false;
Expand All @@ -92,6 +93,11 @@ template <typename V> struct RaxTreeMap {
}

~RaxTreeMap() {
for (auto it = begin(); it != end(); ++it) {
V* ptr = &(*it).second;
std::allocator_traits<decltype(alloc_)>::destroy(alloc_, ptr);
alloc_.deallocate(ptr, 1);
}
Comment on lines +96 to +100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really forgot this 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻 🤦🏻

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raxFree(tree_);
}

Expand All @@ -113,7 +119,8 @@ template <typename V> struct RaxTreeMap {

FindIterator find(std::string_view key) const {
if (void* ptr = raxFind(tree_, to_key_ptr(key), key.size()); ptr != raxNotFound)
return FindIterator{std::pair<std::string_view, V&>(key, *reinterpret_cast<V*>(ptr))};
return FindIterator{std::pair<std::string, V&>(std::string(key), *reinterpret_cast<V*>(ptr))};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's dangerous, but it also means every lookup is a copy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we already leaked in the tests 😄 I will figure out something and push it. I will merge this and follow up


return FindIterator{std::nullopt};
}

Expand Down Expand Up @@ -155,8 +162,8 @@ std::pair<typename RaxTreeMap<V>::FindIterator, bool> RaxTreeMap<V>::try_emplace
raxInsert(tree_, to_key_ptr(key), key.size(), ptr, reinterpret_cast<void**>(&old));
assert(old == nullptr);

auto it = std::make_optional(std::pair<std::string_view, V&>(key, *ptr));
return std::make_pair(FindIterator{it}, true);
auto it = std::make_optional(std::pair<std::string, V&>(std::string(key), *ptr));
return std::make_pair(std::move(FindIterator{it}), true);
}

} // namespace dfly::search
Loading