From f94eb708e9d068ce5af251ca7df95643f3e8ff4c Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Fri, 14 Jul 2023 15:52:03 +0300 Subject: [PATCH] cql3: statement_restrictions: avoid expression's default constructor when classifying restrictions We have some gnarly code that classifies restrictions by the column they restrict. This uses std::unordered_map::operatorp[], which uses the value's default constructor. This happens to be "expression", and as we're about to remove the default constructor, this won't do. Fix by using try_emplace(), which makes the code nicer and more efficient. It could be further improved, but it's better to demolish it instead. --- cql3/restrictions/statement_restrictions.cc | 32 ++++++++------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/cql3/restrictions/statement_restrictions.cc b/cql3/restrictions/statement_restrictions.cc index 69b0eaed45..0d8f34a404 100644 --- a/cql3/restrictions/statement_restrictions.cc +++ b/cql3/restrictions/statement_restrictions.cc @@ -120,11 +120,9 @@ static std::vector extract_partition_range( auto s = &cv; with_current_binary_operator(*this, [&] (const binary_operator& b) { if (s->col->is_partition_key() && (b.op == oper_t::EQ || b.op == oper_t::IN)) { - const auto found = single_column.find(s->col); - if (found == single_column.end()) { - single_column[s->col] = b; - } else { - found->second = make_conjunction(std::move(found->second), b); + const auto [it, inserted] = single_column.try_emplace(s->col, b); + if (!inserted) { + it->second = make_conjunction(std::move(it->second), b); } } }); @@ -139,11 +137,9 @@ static std::vector extract_partition_range( with_current_binary_operator(*this, [&] (const binary_operator& b) { if (cval.col->is_partition_key() && (b.op == oper_t::EQ || b.op == oper_t::IN)) { - const auto found = single_column.find(cval.col); - if (found == single_column.end()) { - single_column[cval.col] = b; - } else { - found->second = make_conjunction(std::move(found->second), b); + const auto [it, inserted] = single_column.try_emplace(cval.col, b); + if (!inserted) { + it->second = make_conjunction(std::move(it->second), b); } } }); @@ -247,11 +243,9 @@ static std::vector extract_clustering_prefix_restrictions( auto s = &cv; with_current_binary_operator(*this, [&] (const binary_operator& b) { if (s->col->is_clustering_key()) { - const auto found = single.find(s->col); - if (found == single.end()) { - single[s->col] = b; - } else { - found->second = make_conjunction(std::move(found->second), b); + const auto [it, inserted] = single.try_emplace(s->col, b); + if (!inserted) { + it->second = make_conjunction(std::move(it->second), b); } } }); @@ -262,11 +256,9 @@ static std::vector extract_clustering_prefix_restrictions( with_current_binary_operator(*this, [&] (const binary_operator& b) { if (cval.col->is_clustering_key()) { - const auto found = single.find(cval.col); - if (found == single.end()) { - single[cval.col] = b; - } else { - found->second = make_conjunction(std::move(found->second), b); + const auto [it, inserted] = single.try_emplace(cval.col, b); + if (!inserted) { + it->second = make_conjunction(std::move(it->second), b); } } });