|
1 | | -use std::collections::{btree_map, BTreeMap, VecDeque}; |
| 1 | +use std::collections::{BTreeMap, VecDeque}; |
2 | 2 |
|
3 | 3 | use everscale_types::cell::HashBytes; |
4 | 4 | use everscale_types::models::{ExtInMsgInfo, IntMsgInfo, MsgInfo}; |
@@ -30,7 +30,6 @@ pub struct MessagesBuffer { |
30 | 30 | msgs: FastIndexMap<HashBytes, VecDeque<Box<ParsedMessage>>>, |
31 | 31 | int_count: usize, |
32 | 32 | ext_count: usize, |
33 | | - sorted_index: BTreeMap<usize, FastHashSet<HashBytes>>, |
34 | 33 | } |
35 | 34 |
|
36 | 35 | impl MessagesBuffer { |
@@ -71,34 +70,7 @@ impl MessagesBuffer { |
71 | 70 | let account_id = dst.as_std().map(|a| a.address).unwrap_or_default(); |
72 | 71 |
|
73 | 72 | // 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); |
102 | 74 | } |
103 | 75 |
|
104 | 76 | pub fn remove_messages_by_accounts(&mut self, addresses_to_remove: &FastHashSet<HashBytes>) { |
@@ -334,7 +306,15 @@ impl MessagesBuffer { |
334 | 306 |
|
335 | 307 | // remove empty accounts from buffer |
336 | 308 | 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 | + }); |
338 | 318 |
|
339 | 319 | // 3. update slots index |
340 | 320 | let mut remove_slot_ids = BTreeMap::<usize, FastHashSet<u16>>::new(); |
@@ -636,8 +616,18 @@ impl MessagesBuffer { |
636 | 616 | BufferFillStateByCount::NotFull |
637 | 617 | }; |
638 | 618 |
|
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 | + } |
641 | 631 |
|
642 | 632 | (by_count, by_slots) |
643 | 633 | } |
|
0 commit comments