Skip to content

Commit 4e6e925

Browse files
committed
store state roots and build accounts in parallel
1 parent cd5ffe3 commit 4e6e925

File tree

6 files changed

+245
-162
lines changed

6 files changed

+245
-162
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

collator/src/collator/do_collate/finalize.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ impl Phase<FinalizeState> {
11721172
let mut new_config_params = None;
11731173
let (updated_accounts, shard_accounts) = executor.into_accounts_cache_raw();
11741174

1175-
let mut shard_accounts = shard_accounts
1175+
let shard_accounts = shard_accounts
11761176
.into_iter()
11771177
.map(|(k, v)| (k, RelaxedAugDict::from_full(&v)))
11781178
.collect::<BTreeMap<_, _>>();
@@ -1233,21 +1233,37 @@ impl Phase<FinalizeState> {
12331233

12341234
account_updates.par_sort_by(|(a, ..), (b, ..)| a.cmp(b));
12351235

1236-
let account_updates_by_shards = account_updates
1237-
.into_iter()
1238-
.group_by(|(account, _)| {
1239-
u64::from_be_bytes(*account.first_chunk()) & (0b1111u64 << 60) | (0b1u64 << 59)
1240-
})
1241-
.into_iter()
1242-
.map(|(prefix, group)| (prefix, group.collect::<Vec<_>>()))
1243-
.collect::<FastHashMap<_, _>>();
1244-
1245-
for (prefix, account_updates) in account_updates_by_shards {
1246-
let shard_accounts = shard_accounts.get_mut(&prefix).unwrap();
1247-
shard_accounts
1248-
.modify_with_sorted_iter(account_updates)
1249-
.context("failed to modify accounts dict")?;
1250-
}
1236+
let account_updates_by_shards = Arc::new(Mutex::new(
1237+
account_updates
1238+
.into_iter()
1239+
.group_by(|(account, _)| {
1240+
u64::from_be_bytes(*account.first_chunk()) & (0b1111u64 << 60) | (0b1u64 << 59)
1241+
})
1242+
.into_iter()
1243+
.map(|(prefix, group)| (prefix, group.collect::<Vec<_>>()))
1244+
.collect::<FastHashMap<_, _>>(),
1245+
));
1246+
1247+
let updated_shard_accounts = Arc::new(Mutex::new(BTreeMap::new()));
1248+
1249+
rayon::scope(|s| {
1250+
for (prefix, mut shard_accounts) in shard_accounts {
1251+
let account_updates_by_shards = Arc::clone(&account_updates_by_shards);
1252+
let updated_shard_accounts = Arc::clone(&updated_shard_accounts);
1253+
s.spawn(move |_| {
1254+
let account_updates = account_updates_by_shards.lock().remove(&prefix);
1255+
if let Some(account_updates) = account_updates {
1256+
shard_accounts
1257+
.modify_with_sorted_iter(account_updates)
1258+
.context("failed to modify accounts dict")
1259+
.unwrap();
1260+
}
1261+
1262+
let dict = shard_accounts.build().unwrap();
1263+
updated_shard_accounts.lock().insert(prefix, dict);
1264+
});
1265+
}
1266+
});
12511267

12521268
let accounts_len = account_blocks.len();
12531269

@@ -1257,10 +1273,9 @@ impl Phase<FinalizeState> {
12571273
.map(|(k, v)| (k, v.transactions.root_extra().clone(), v)),
12581274
)?;
12591275

1260-
let shard_accounts = shard_accounts
1261-
.into_iter()
1262-
.map(|(k, dict)| (k, dict.build().unwrap()))
1263-
.collect::<BTreeMap<_, _>>();
1276+
let shard_accounts = Arc::try_unwrap(updated_shard_accounts)
1277+
.map_err(|_e| anyhow::anyhow!("Failed to unwrap Arc"))?
1278+
.into_inner();
12641279

12651280
Ok(ProcessedAccounts {
12661281
account_blocks: account_blocks.build()?,

storage/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ metrics = { workspace = true }
2727
moka = { workspace = true }
2828
parking_lot = { workspace = true }
2929
parking_lot_core = { workspace = true }
30+
rayon = { workspace = true }
3031
quick_cache = { workspace = true }
3132
rand = { workspace = true }
3233
rlimit = { workspace = true }

storage/src/db/kv_db/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,6 @@ weedb::tables! {
131131
pub block_connections: tables::BlockConnections,
132132

133133
// tables are empty, but they cannot be deleted because they are in a storage config
134-
_cells: tables::Cells,
135-
_temp_cells: tables::TempCells,
136-
_shard_states: tables::ShardStates,
137134
_shard_internal_messages: tables::ShardInternalMessagesOld,
138135
_int_msg_stats_uncommited: tables::InternalMessageStatsUncommitedOld,
139136
_shard_int_msgs_uncommited: tables::ShardInternalMessagesUncommitedOld,

0 commit comments

Comments
 (0)