Optimise edge expansion

This commit is contained in:
Gareth Lloyd 2024-03-11 13:22:35 +00:00
parent 78fcb92738
commit 51301a8125
2 changed files with 47 additions and 16 deletions

View File

@ -69,6 +69,7 @@
#include "utils/pmr/vector.hpp" #include "utils/pmr/vector.hpp"
#include "utils/readable_size.hpp" #include "utils/readable_size.hpp"
#include "utils/string.hpp" #include "utils/string.hpp"
#include "utils/tag.hpp"
#include "utils/temporal.hpp" #include "utils/temporal.hpp"
#include "utils/typeinfo.hpp" #include "utils/typeinfo.hpp"
@ -864,17 +865,15 @@ bool Expand::ExpandCursor::Pull(Frame &frame, ExecutionContext &context) {
SCOPED_PROFILE_OP_BY_REF(self_); SCOPED_PROFILE_OP_BY_REF(self_);
// A helper function for expanding a node from an edge. // A helper function for expanding a node from an edge.
auto pull_node = [this, &frame](const EdgeAccessor &new_edge, EdgeAtom::Direction direction) { auto pull_node = [this, &frame]<EdgeAtom::Direction direction>(const EdgeAccessor &new_edge,
utils::tag_value<direction>) {
if (self_.common_.existing_node) return; if (self_.common_.existing_node) return;
switch (direction) { if constexpr (direction == EdgeAtom::Direction::IN) {
case EdgeAtom::Direction::IN: frame[self_.common_.node_symbol] = new_edge.From();
frame[self_.common_.node_symbol] = new_edge.From(); } else if constexpr (direction == EdgeAtom::Direction::OUT) {
break; frame[self_.common_.node_symbol] = new_edge.To();
case EdgeAtom::Direction::OUT: } else {
frame[self_.common_.node_symbol] = new_edge.To(); LOG_FATAL("Must indicate exact expansion direction here");
break;
case EdgeAtom::Direction::BOTH:
LOG_FATAL("Must indicate exact expansion direction here");
} }
}; };
@ -893,7 +892,7 @@ bool Expand::ExpandCursor::Pull(Frame &frame, ExecutionContext &context) {
#endif #endif
frame[self_.common_.edge_symbol] = edge; frame[self_.common_.edge_symbol] = edge;
pull_node(edge, EdgeAtom::Direction::IN); pull_node(edge, utils::tag_v<EdgeAtom::Direction::IN>);
return true; return true;
} }
@ -913,7 +912,7 @@ bool Expand::ExpandCursor::Pull(Frame &frame, ExecutionContext &context) {
} }
#endif #endif
frame[self_.common_.edge_symbol] = edge; frame[self_.common_.edge_symbol] = edge;
pull_node(edge, EdgeAtom::Direction::OUT); pull_node(edge, utils::tag_v<EdgeAtom::Direction::OUT>);
return true; return true;
} }
@ -1007,12 +1006,12 @@ bool Expand::ExpandCursor::InitEdges(Frame &frame, ExecutionContext &context) {
auto existing_node = *expansion_info_.existing_node; auto existing_node = *expansion_info_.existing_node;
auto edges_result = UnwrapEdgesResult(vertex.InEdges(self_.view_, self_.common_.edge_types, existing_node)); auto edges_result = UnwrapEdgesResult(vertex.InEdges(self_.view_, self_.common_.edge_types, existing_node));
in_edges_.emplace(edges_result.edges); in_edges_.emplace(std::move(edges_result.edges));
num_expanded_first = edges_result.expanded_count; num_expanded_first = edges_result.expanded_count;
} }
} else { } else {
auto edges_result = UnwrapEdgesResult(vertex.InEdges(self_.view_, self_.common_.edge_types)); auto edges_result = UnwrapEdgesResult(vertex.InEdges(self_.view_, self_.common_.edge_types));
in_edges_.emplace(edges_result.edges); in_edges_.emplace(std::move(edges_result.edges));
num_expanded_first = edges_result.expanded_count; num_expanded_first = edges_result.expanded_count;
} }
if (in_edges_) { if (in_edges_) {
@ -1026,12 +1025,12 @@ bool Expand::ExpandCursor::InitEdges(Frame &frame, ExecutionContext &context) {
if (expansion_info_.existing_node) { if (expansion_info_.existing_node) {
auto existing_node = *expansion_info_.existing_node; auto existing_node = *expansion_info_.existing_node;
auto edges_result = UnwrapEdgesResult(vertex.OutEdges(self_.view_, self_.common_.edge_types, existing_node)); auto edges_result = UnwrapEdgesResult(vertex.OutEdges(self_.view_, self_.common_.edge_types, existing_node));
out_edges_.emplace(edges_result.edges); out_edges_.emplace(std::move(edges_result.edges));
num_expanded_second = edges_result.expanded_count; num_expanded_second = edges_result.expanded_count;
} }
} else { } else {
auto edges_result = UnwrapEdgesResult(vertex.OutEdges(self_.view_, self_.common_.edge_types)); auto edges_result = UnwrapEdgesResult(vertex.OutEdges(self_.view_, self_.common_.edge_types));
out_edges_.emplace(edges_result.edges); out_edges_.emplace(std::move(edges_result.edges));
num_expanded_second = edges_result.expanded_count; num_expanded_second = edges_result.expanded_count;
} }
if (out_edges_) { if (out_edges_) {

32
src/utils/tag.hpp Normal file
View File

@ -0,0 +1,32 @@
// Copyright 2024 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
// License, and you may not use this file except in compliance with the Business Source License.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
#pragma once
namespace memgraph::utils {
template <typename T>
struct tag_type {
using type = T;
};
template <auto V>
struct tag_value {
static constexpr auto value = V;
};
template <typename T>
auto tag_t = tag_type<T>{};
template <auto V>
auto tag_v = tag_value<V>{};
} // namespace memgraph::utils