Skip to content

Commit 1f9b79c

Browse files
dj3500dj3500
andauthored
optimize detect_common_filters (#646)
Co-authored-by: dj3500 <jatarnaw@microsoft.com>
1 parent fc3c6e2 commit 1f9b79c

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/index.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -756,36 +756,51 @@ template <typename T, typename TagT, typename LabelT> std::vector<uint32_t> Inde
756756
}
757757

758758
// Find common filter between a node's labels and a given set of labels, while
759-
// taking into account universal label
759+
// taking into account universal label.
760+
// Note: incoming_labels should be sorted.
760761
template <typename T, typename TagT, typename LabelT>
761762
bool Index<T, TagT, LabelT>::detect_common_filters(uint32_t point_id, bool search_invocation,
762763
const std::vector<LabelT> &incoming_labels)
763764
{
764765
auto &curr_node_labels = _location_to_labels[point_id];
765-
std::vector<LabelT> common_filters;
766-
std::set_intersection(incoming_labels.begin(), incoming_labels.end(), curr_node_labels.begin(),
767-
curr_node_labels.end(), std::back_inserter(common_filters));
768-
if (common_filters.size() > 0)
766+
// Check for intersection between incoming_labels and curr_node_labels
767+
// using two-pointer approach (both vectors are sorted)
768+
auto it_inc = incoming_labels.begin();
769+
auto it_curr = curr_node_labels.begin();
770+
771+
while (it_inc != incoming_labels.end() && it_curr != curr_node_labels.end())
769772
{
770-
// This is to reduce the repetitive calls. If common_filters size is > 0 ,
771-
// we dont need to check further for universal label
772-
return true;
773+
if (*it_inc < *it_curr)
774+
{
775+
++it_inc;
776+
}
777+
else if (*it_curr < *it_inc)
778+
{
779+
++it_curr;
780+
}
781+
else
782+
{
783+
// common label found
784+
return true;
785+
}
773786
}
787+
// intersection empty; proceed to check the universal label logic
788+
774789
if (_use_universal_label)
775790
{
776791
if (!search_invocation)
777792
{
778793
if (std::find(incoming_labels.begin(), incoming_labels.end(), _universal_label) != incoming_labels.end() ||
779794
std::find(curr_node_labels.begin(), curr_node_labels.end(), _universal_label) != curr_node_labels.end())
780-
common_filters.push_back(_universal_label);
795+
return true;
781796
}
782797
else
783798
{
784799
if (std::find(curr_node_labels.begin(), curr_node_labels.end(), _universal_label) != curr_node_labels.end())
785-
common_filters.push_back(_universal_label);
800+
return true;
786801
}
787802
}
788-
return (common_filters.size() > 0);
803+
return false;
789804
}
790805

791806
template <typename T, typename TagT, typename LabelT>

0 commit comments

Comments
 (0)