Skip to content

Commit 053166b

Browse files
author
HaoranYi
committed
optimize sort and remove dups for shrinking
1 parent 8098948 commit 053166b

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

accounts-db/src/accounts_db.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3908,19 +3908,22 @@ impl AccountsDb {
39083908
// stable sort because we want the most recent only
39093909
accounts.sort_by(|a, b| a.pubkey().cmp(b.pubkey()));
39103910
if accounts.len() > 1 {
3911-
let mut i = 0;
3912-
// iterate 0..1 less than end
3913-
while i < accounts.len() - 1 {
3914-
let current = accounts[i];
3915-
let next = accounts[i + 1];
3916-
if current.pubkey() == next.pubkey() {
3917-
// remove the first duplicate
3918-
accounts.remove(i);
3919-
// do not advance i, we just removed an element at i
3920-
continue;
3911+
let mut last = 0;
3912+
let mut curr = 1;
3913+
3914+
loop {
3915+
if accounts[curr].pubkey() == accounts[last].pubkey() {
3916+
accounts[last] = accounts[curr];
3917+
} else {
3918+
last += 1;
3919+
accounts[last] = accounts[curr];
3920+
}
3921+
curr += 1;
3922+
if curr == accounts.len() {
3923+
break;
39213924
}
3922-
i += 1;
39233925
}
3926+
accounts.truncate(last + 1);
39243927
}
39253928
}
39263929

0 commit comments

Comments
 (0)