From 87bd24cedb071a01c999fbb017f2df53845a7de7 Mon Sep 17 00:00:00 2001
From: Denis Smirnov <sd@picodata.io>
Date: Thu, 11 Aug 2022 21:54:32 +0700
Subject: [PATCH] perf: remove redundant clone and add ahash in the router

---
 src/ir.rs                                     | 2 +-
 src/ir/expression.rs                          | 4 ++--
 src/ir/transformation/redistribution.rs       | 7 ++++---
 src/ir/transformation/redistribution/tests.rs | 4 +++-
 4 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/ir.rs b/src/ir.rs
index bbee034de2..67326f6925 100644
--- a/src/ir.rs
+++ b/src/ir.rs
@@ -438,7 +438,7 @@ impl Plan {
             }
 
             if let Some(list_of_column_nodes) = ref_node.children() {
-                let child_ids = targets.clone().ok_or_else(|| {
+                let child_ids = targets.as_ref().ok_or_else(|| {
                     QueryPlannerError::CustomError("Node refs to scan node, not alias".into())
                 })?;
                 let column_index_in_list = child_ids.get(0).ok_or_else(|| {
diff --git a/src/ir/expression.rs b/src/ir/expression.rs
index 6785269479..8082197196 100644
--- a/src/ir/expression.rs
+++ b/src/ir/expression.rs
@@ -729,8 +729,8 @@ impl Plan {
     pub fn get_relational_from_row_nodes(
         &self,
         row_id: usize,
-    ) -> Result<HashSet<usize>, QueryPlannerError> {
-        let mut rel_nodes: HashSet<usize> = HashSet::new();
+    ) -> Result<HashSet<usize, RandomState>, QueryPlannerError> {
+        let mut rel_nodes: HashSet<usize, RandomState> = HashSet::with_hasher(RandomState::new());
 
         let row = self.get_expression_node(row_id)?;
         if let Expression::Row { .. } = row {
diff --git a/src/ir/transformation/redistribution.rs b/src/ir/transformation/redistribution.rs
index f4d2bf08d6..18367a2f25 100644
--- a/src/ir/transformation/redistribution.rs
+++ b/src/ir/transformation/redistribution.rs
@@ -1,5 +1,6 @@
 //! Resolve distribution conflicts and insert motion nodes to IR.
 
+use ahash::RandomState;
 use std::cmp::Ordering;
 use std::collections::{hash_map::Entry, HashMap, HashSet};
 
@@ -197,9 +198,9 @@ impl Plan {
     /// - There are more than one sub-query
     pub fn get_sub_query_among_rel_nodes(
         &self,
-        rel_nodes: &HashSet<usize>,
+        rel_nodes: &HashSet<usize, RandomState>,
     ) -> Result<Option<usize>, QueryPlannerError> {
-        let mut sq_set: HashSet<usize> = HashSet::new();
+        let mut sq_set: HashSet<usize, RandomState> = HashSet::with_hasher(RandomState::new());
         for rel_id in rel_nodes {
             if let Node::Relational(Relational::ScanSubQuery { .. }) = self.get_node(*rel_id)? {
                 sq_set.insert(*rel_id);
@@ -240,7 +241,7 @@ impl Plan {
     /// - There are more than one motion node
     pub fn get_motion_among_rel_nodes(
         &self,
-        rel_nodes: &HashSet<usize>,
+        rel_nodes: &HashSet<usize, RandomState>,
     ) -> Result<Option<usize>, QueryPlannerError> {
         let mut motion_set: HashSet<usize> = HashSet::new();
 
diff --git a/src/ir/transformation/redistribution/tests.rs b/src/ir/transformation/redistribution/tests.rs
index fb311fece8..827c1ede6f 100644
--- a/src/ir/transformation/redistribution/tests.rs
+++ b/src/ir/transformation/redistribution/tests.rs
@@ -6,6 +6,7 @@ use crate::ir::operator::Relational;
 use crate::ir::relation::*;
 use crate::ir::transformation::helpers::sql_to_ir;
 use crate::ir::Plan;
+use ahash::RandomState;
 use pretty_assertions::assert_eq;
 use std::fs;
 use std::path::Path;
@@ -52,7 +53,8 @@ fn segment_motion_for_sub_query() {
     let select_id = plan.add_select(&children[..], eq_id).unwrap();
     plan.set_top(select_id).unwrap();
 
-    let mut expected_rel_set: HashSet<usize> = HashSet::new();
+    let mut expected_rel_set: HashSet<usize, RandomState> =
+        HashSet::with_hasher(RandomState::new());
     expected_rel_set.insert(sq_id);
     assert_eq!(
         expected_rel_set,
-- 
GitLab