Skip to content

Commit 4357908

Browse files
committed
feat(collator): add by slots check for messages buffer
1 parent 91bd0a7 commit 4357908

1 file changed

Lines changed: 23 additions & 33 deletions

File tree

collator/src/collator/messages_buffer.rs

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{btree_map, BTreeMap, VecDeque};
1+
use std::collections::{BTreeMap, VecDeque};
22

33
use everscale_types::cell::HashBytes;
44
use everscale_types::models::{ExtInMsgInfo, IntMsgInfo, MsgInfo};
@@ -30,7 +30,6 @@ pub struct MessagesBuffer {
3030
msgs: FastIndexMap<HashBytes, VecDeque<Box<ParsedMessage>>>,
3131
int_count: usize,
3232
ext_count: usize,
33-
sorted_index: BTreeMap<usize, FastHashSet<HashBytes>>,
3433
}
3534

3635
impl MessagesBuffer {
@@ -71,34 +70,7 @@ impl MessagesBuffer {
7170
let account_id = dst.as_std().map(|a| a.address).unwrap_or_default();
7271

7372
// insert message to buffer
74-
let prev_msgs_count = match self.msgs.entry(account_id) {
75-
indexmap::map::Entry::Vacant(vacant) => {
76-
vacant.insert([msg].into());
77-
0
78-
}
79-
indexmap::map::Entry::Occupied(mut occupied) => {
80-
let entry = occupied.get_mut();
81-
let prev_count = entry.len();
82-
entry.push_back(msg);
83-
prev_count
84-
}
85-
};
86-
87-
// update sorted by count index
88-
// remove account_id from previous count bracket
89-
if prev_msgs_count > 0 {
90-
let accounts = self.sorted_index.get_mut(&prev_msgs_count).unwrap();
91-
accounts.remove(&account_id);
92-
}
93-
// add account_id to the next count bracket
94-
match self.sorted_index.entry(prev_msgs_count + 1) {
95-
btree_map::Entry::Vacant(vacant) => {
96-
vacant.insert([account_id].into_iter().collect());
97-
}
98-
btree_map::Entry::Occupied(mut occupied) => {
99-
occupied.get_mut().insert(account_id);
100-
}
101-
}
73+
self.msgs.entry(account_id).or_default().push_back(msg);
10274
}
10375

10476
pub fn remove_messages_by_accounts(&mut self, addresses_to_remove: &FastHashSet<HashBytes>) {
@@ -334,7 +306,15 @@ impl MessagesBuffer {
334306

335307
// remove empty accounts from buffer
336308
ops_count.saturating_add_assign(self.msgs.len() as u64);
337-
self.msgs.retain(|_, account_msgs| !account_msgs.is_empty());
309+
let mut addresses_to_remove: FastHashSet<HashBytes> = Default::default();
310+
self.msgs.retain(|account_id, account_msgs| {
311+
if account_msgs.is_empty() {
312+
addresses_to_remove.insert(*account_id);
313+
false
314+
} else {
315+
true
316+
}
317+
});
338318

339319
// 3. update slots index
340320
let mut remove_slot_ids = BTreeMap::<usize, FastHashSet<u16>>::new();
@@ -636,8 +616,18 @@ impl MessagesBuffer {
636616
BufferFillStateByCount::NotFull
637617
};
638618

639-
// TODO: msgs-v3: check if we can already fill required slots
640-
let by_slots = BufferFillStateBySlots::CanNotFill;
619+
let mut by_slots = BufferFillStateBySlots::CanNotFill;
620+
621+
if self.msgs.len() >= limits.slots_count {
622+
let full_slots = self
623+
.msgs
624+
.iter()
625+
.filter(|(_, msgs)| msgs.len() >= limits.slot_vert_size)
626+
.count();
627+
if full_slots >= limits.slots_count {
628+
by_slots = BufferFillStateBySlots::CanFill;
629+
}
630+
}
641631

642632
(by_count, by_slots)
643633
}

0 commit comments

Comments
 (0)