From 7b4b1ba8ed65249c6591876947e73cd2efd0467d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Benjamin=20Antal?= Date: Tue, 1 Nov 2022 11:51:35 +0100 Subject: [PATCH] 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 +#include #include -#include #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 &At(int position) const { - MG_ASSERT(position < static_cast(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> storage_; + std::unordered_map 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 &At(int position) const { - MG_ASSERT(position < static_cast(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> storage_; + std::unordered_map storage_; }; struct EvaluationContext {