1use std::collections::VecDeque;
2use std::collections::hash_map::Entry;
34use super::HalfJoinState;
5use crate::util::clear::Clear;
67type HashMap<K, V> = rustc_hash::FxHashMap<K, V>;
89use smallvec::{SmallVec, smallvec};
1011#[derive(Debug)]
12pub struct HalfSetJoinState<Key, ValBuild, ValProbe> {
13// Here a smallvec with inline storage of 1 is chosen.
14 // The rationale for this decision is that, I speculate, that joins possibly have a bimodal distribution with regards to how much key contention they have.
15 // That is, I think that there are many joins that have 1 value per key on LHS/RHS, and there are also a large category of joins that have multiple values per key.
16 // For the category of joins that have multiple values per key, it's not clear why they would only have 2, 3, 4, or N specific number of values per key. So there's no good number to set the smallvec storage to.
17 // Instead we can just focus on the first group of joins that have 1 value per key and get benefit there without hurting the other group too much with excessive memory usage.
18/// Table to probe, vec val contains all matches.
19table: HashMap<Key, SmallVec<[ValBuild; 1]>>,
20/// Not-yet emitted matches.
21current_matches: VecDeque<(Key, ValProbe, ValBuild)>,
22 len: usize,
23}
24impl<Key, ValBuild, ValProbe> Default for HalfSetJoinState<Key, ValBuild, ValProbe> {
25fn default() -> Self {
26Self {
27 table: HashMap::default(),
28 current_matches: VecDeque::default(),
29 len: 0,
30 }
31 }
32}
33impl<Key, ValBuild, ValProbe> Clear for HalfSetJoinState<Key, ValBuild, ValProbe> {
34fn clear(&mut self) {
35self.table.clear();
36self.current_matches.clear();
37self.len = 0;
38 }
39}
40impl<Key, ValBuild, ValProbe> HalfJoinState<Key, ValBuild, ValProbe>
41for HalfSetJoinState<Key, ValBuild, ValProbe>
42where
43Key: Clone + Eq + std::hash::Hash,
44 ValBuild: Clone + Eq,
45 ValProbe: Clone,
46{
47fn build(&mut self, k: Key, v: &ValBuild) -> bool {
48let entry = self.table.entry(k);
4950match entry {
51 Entry::Occupied(mut e) => {
52let vec = e.get_mut();
5354if !vec.contains(v) {
55 vec.push(v.clone());
56self.len += 1;
57return true;
58 }
59 }
60 Entry::Vacant(e) => {
61 e.insert(smallvec![v.clone()]);
62self.len += 1;
63return true;
64 }
65 };
6667false
68}
6970fn probe(&mut self, k: &Key, v: &ValProbe) -> Option<(Key, ValProbe, ValBuild)> {
71// TODO: We currently don't free/shrink the self.current_matches vecdeque to save time.
72 // This mean it will grow to eventually become the largest number of matches in a single probe call.
73 // Maybe we should clear this memory at the beginning of every tick/periodically?
74let mut iter = self
75.table
76 .get(k)?
77.iter()
78 .map(|valbuild| (k.clone(), v.clone(), valbuild.clone()));
7980let first = iter.next();
8182self.current_matches.extend(iter);
8384 first
85 }
8687fn full_probe(&self, k: &Key) -> std::slice::Iter<'_, ValBuild> {
88let Some(sv) = self.table.get(k) else {
89return [].iter();
90 };
9192 sv.iter()
93 }
9495fn pop_match(&mut self) -> Option<(Key, ValProbe, ValBuild)> {
96self.current_matches.pop_front()
97 }
9899fn len(&self) -> usize {
100self.len
101 }
102103fn iter(&self) -> std::collections::hash_map::Iter<'_, Key, SmallVec<[ValBuild; 1]>> {
104self.table.iter()
105 }
106}