From 7b4b1ba8ed65249c6591876947e73cd2efd0467d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A1nos=20Benjamin=20Antal?= <benjamin.antal@memgraph.io>
Date: Tue, 1 Nov 2022 11:51:35 +0100
Subject: [PATCH 1/5] Speed up `Parameters::AtTokenPosition`

---
 src/query/v2/parameters.hpp      | 20 ++++----------------
 src/storage/v3/bindings/eval.hpp | 18 +++---------------
 2 files changed, 7 insertions(+), 31 deletions(-)

diff --git a/src/query/v2/parameters.hpp b/src/query/v2/parameters.hpp
index 1e2f0744f..7f4ac0b31 100644
--- a/src/query/v2/parameters.hpp
+++ b/src/query/v2/parameters.hpp
@@ -12,8 +12,8 @@
 #pragma once
 
 #include <algorithm>
+#include <unordered_map>
 #include <utility>
-#include <vector>
 
 #include "storage/v3/property_value.hpp"
 #include "utils/logging.hpp"
@@ -32,7 +32,7 @@ struct Parameters {
    * @param position Token position in query of value.
    * @param value
    */
-  void Add(int position, const storage::v3::PropertyValue &value) { storage_.emplace_back(position, value); }
+  void Add(int position, const storage::v3::PropertyValue &value) { storage_.emplace(position, value); }
 
   /**
    *  Returns the value found for the given token position.
@@ -41,23 +41,11 @@ struct Parameters {
    *  @return Value for the given token position.
    */
   const storage::v3::PropertyValue &AtTokenPosition(int position) const {
-    auto found = std::find_if(storage_.begin(), storage_.end(), [&](const auto &a) { return a.first == position; });
+    auto found = storage_.find(position);
     MG_ASSERT(found != storage_.end(), "Token position must be present in container");
     return found->second;
   }
 
-  /**
-   * Returns the position-th stripped value. Asserts that this
-   * container has at least (position + 1) elements.
-   *
-   * @param position Which stripped param is sought.
-   * @return Token position and value for sought param.
-   */
-  const std::pair<int, storage::v3::PropertyValue> &At(int position) const {
-    MG_ASSERT(position < static_cast<int>(storage_.size()), "Invalid position");
-    return storage_[position];
-  }
-
   /** Returns the number of arguments in this container */
   auto size() const { return storage_.size(); }
 
@@ -65,7 +53,7 @@ struct Parameters {
   auto end() const { return storage_.end(); }
 
  private:
-  std::vector<std::pair<int, storage::v3::PropertyValue>> storage_;
+  std::unordered_map<int, storage::v3::PropertyValue> storage_;
 };
 
 }  // namespace memgraph::query::v2
diff --git a/src/storage/v3/bindings/eval.hpp b/src/storage/v3/bindings/eval.hpp
index 38b5aae87..7f93bb79c 100644
--- a/src/storage/v3/bindings/eval.hpp
+++ b/src/storage/v3/bindings/eval.hpp
@@ -42,7 +42,7 @@ struct Parameters {
    * @param position Token position in query of value.
    * @param value
    */
-  void Add(int position, const storage::v3::PropertyValue &value) { storage_.emplace_back(position, value); }
+  void Add(int position, const storage::v3::PropertyValue &value) { storage_.emplace(position, value); }
 
   /**
    *  Returns the value found for the given token position.
@@ -51,23 +51,11 @@ struct Parameters {
    *  @return Value for the given token position.
    */
   const storage::v3::PropertyValue &AtTokenPosition(int position) const {
-    auto found = std::find_if(storage_.begin(), storage_.end(), [&](const auto &a) { return a.first == position; });
+    auto found = storage_.find(position);
     MG_ASSERT(found != storage_.end(), "Token position must be present in container");
     return found->second;
   }
 
-  /**
-   * Returns the position-th stripped value. Asserts that this
-   * container has at least (position + 1) elements.
-   *
-   * @param position Which stripped param is sought.
-   * @return Token position and value for sought param.
-   */
-  const std::pair<int, storage::v3::PropertyValue> &At(int position) const {
-    MG_ASSERT(position < static_cast<int>(storage_.size()), "Invalid position");
-    return storage_[position];
-  }
-
   /** Returns the number of arguments in this container */
   auto size() const { return storage_.size(); }
 
@@ -75,7 +63,7 @@ struct Parameters {
   auto end() const { return storage_.end(); }
 
  private:
-  std::vector<std::pair<int, storage::v3::PropertyValue>> storage_;
+  std::unordered_map<int, storage::v3::PropertyValue> storage_;
 };
 
 struct EvaluationContext {

From a96f4897566afde491e9f3b7d7d99af0fb921205 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A1nos=20Benjamin=20Antal?= <benjamin.antal@memgraph.io>
Date: Wed, 2 Nov 2022 14:02:19 +0100
Subject: [PATCH 2/5] Count the number of requests per operator

---
 src/query/v2/plan/profile.hpp        |  1 +
 src/query/v2/plan/scoped_profile.hpp | 11 ++++++++---
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/query/v2/plan/profile.hpp b/src/query/v2/plan/profile.hpp
index da25be33c..bf7687018 100644
--- a/src/query/v2/plan/profile.hpp
+++ b/src/query/v2/plan/profile.hpp
@@ -29,6 +29,7 @@ struct ProfilingStats {
   static constexpr std::string_view kNumCycles{"num_cycles"};
   static constexpr std::string_view kRelativeTime{"relative_time"};
   static constexpr std::string_view kAbsoluteTime{"absolute_time"};
+  static constexpr std::string_view kActualHits{"actual_hits"};
 
   int64_t actual_hits{0};
   uint64_t num_cycles{0};
diff --git a/src/query/v2/plan/scoped_profile.hpp b/src/query/v2/plan/scoped_profile.hpp
index 3aa4bc569..a9d4c1296 100644
--- a/src/query/v2/plan/scoped_profile.hpp
+++ b/src/query/v2/plan/scoped_profile.hpp
@@ -42,13 +42,18 @@ class ScopedCustomProfile {
         custom_data = nlohmann::json::object();
       }
       const auto elapsed = utils::ReadTSC() - start_time_;
-      auto &num_cycles_json = custom_data[ProfilingStats::kNumCycles];
-      const auto num_cycles = num_cycles_json.is_null() ? 0 : num_cycles_json.get<uint64_t>();
-      num_cycles_json = num_cycles + elapsed;
+      IncreaseCustomData(custom_data, ProfilingStats::kNumCycles, elapsed);
+      IncreaseCustomData(custom_data, ProfilingStats::kActualHits, 1);
     }
   }
 
  private:
+  static void IncreaseCustomData(nlohmann::json &custom_data, const std::string_view key, uint64_t increment) {
+    auto &json_data = custom_data[key];
+    const auto numerical_data = json_data.is_null() ? 0 : json_data.get<uint64_t>();
+    json_data = numerical_data + increment;
+  }
+
   std::string_view custom_data_name_;
   uint64_t start_time_;
   ExecutionContext *context_;

From bb3b0533757302f04c641ddf30fe563d77cc533d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A1nos=20Benjamin=20Antal?= <benjamin.antal@memgraph.io>
Date: Wed, 2 Nov 2022 14:04:10 +0100
Subject: [PATCH 3/5] Create edge in the correct direction

---
 src/query/v2/plan/operator.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/query/v2/plan/operator.cpp b/src/query/v2/plan/operator.cpp
index 0ab550ed1..0f35a4bbf 100644
--- a/src/query/v2/plan/operator.cpp
+++ b/src/query/v2/plan/operator.cpp
@@ -2458,13 +2458,13 @@ class DistributedCreateExpandCursor : public Cursor {
       std::invoke([&]() {
         switch (edge_info.direction) {
           case EdgeAtom::Direction::IN: {
-            set_vertex(v1, request.src_vertex);
-            set_vertex(v2, request.dest_vertex);
+            set_vertex(v2, request.src_vertex);
+            set_vertex(v1, request.dest_vertex);
             break;
           }
           case EdgeAtom::Direction::OUT: {
-            set_vertex(v1, request.dest_vertex);
-            set_vertex(v2, request.src_vertex);
+            set_vertex(v1, request.src_vertex);
+            set_vertex(v2, request.dest_vertex);
             break;
           }
           case EdgeAtom::Direction::BOTH:

From 57e71692036bd921a349460e4c45f2285712c85d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A1nos=20Benjamin=20Antal?= <benjamin.antal@memgraph.io>
Date: Wed, 2 Nov 2022 14:06:08 +0100
Subject: [PATCH 4/5] Eliminate dangling reference from lambda

---
 src/query/v2/plan/operator.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/query/v2/plan/operator.cpp b/src/query/v2/plan/operator.cpp
index 0f35a4bbf..068ca9192 100644
--- a/src/query/v2/plan/operator.cpp
+++ b/src/query/v2/plan/operator.cpp
@@ -2513,7 +2513,8 @@ class DistributedExpandCursor : public Cursor {
     }
     MG_ASSERT(direction != EdgeAtom::Direction::BOTH);
     const auto &edge = frame[self_.common_.edge_symbol].ValueEdge();
-    static auto get_dst_vertex = [&edge](const EdgeAtom::Direction direction) {
+    static constexpr auto get_dst_vertex = [](const EdgeAccessor &edge,
+                                              const EdgeAtom::Direction direction) -> msgs::VertexId {
       switch (direction) {
         case EdgeAtom::Direction::IN:
           return edge.From().Id();
@@ -2526,7 +2527,7 @@ class DistributedExpandCursor : public Cursor {
     msgs::ExpandOneRequest request;
     // to not fetch any properties of the edges
     request.edge_properties.emplace();
-    request.src_vertices.push_back(get_dst_vertex(direction));
+    request.src_vertices.push_back(get_dst_vertex(edge, direction));
     request.direction = (direction == EdgeAtom::Direction::IN) ? msgs::EdgeDirection::OUT : msgs::EdgeDirection::IN;
     msgs::ExecutionState<msgs::ExpandOneRequest> request_state;
     auto result_rows = context.shard_request_manager->Request(request_state, std::move(request));

From 589dd36bf2ca9816576f0aac83e0f0f8ddf10694 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A1nos=20Benjamin=20Antal?= <benjamin.antal@memgraph.io>
Date: Wed, 2 Nov 2022 14:44:54 +0100
Subject: [PATCH 5/5] Make function parameter constant

---
 src/query/v2/plan/scoped_profile.hpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/query/v2/plan/scoped_profile.hpp b/src/query/v2/plan/scoped_profile.hpp
index a9d4c1296..73484c4bc 100644
--- a/src/query/v2/plan/scoped_profile.hpp
+++ b/src/query/v2/plan/scoped_profile.hpp
@@ -48,7 +48,7 @@ class ScopedCustomProfile {
   }
 
  private:
-  static void IncreaseCustomData(nlohmann::json &custom_data, const std::string_view key, uint64_t increment) {
+  static void IncreaseCustomData(nlohmann::json &custom_data, const std::string_view key, const uint64_t increment) {
     auto &json_data = custom_data[key];
     const auto numerical_data = json_data.is_null() ? 0 : json_data.get<uint64_t>();
     json_data = numerical_data + increment;