@@ -1172,7 +1172,7 @@ impl Phase<FinalizeState> {
1172
1172
let mut new_config_params = None ;
1173
1173
let ( updated_accounts, shard_accounts) = executor. into_accounts_cache_raw ( ) ;
1174
1174
1175
- let mut shard_accounts = shard_accounts
1175
+ let shard_accounts = shard_accounts
1176
1176
. into_iter ( )
1177
1177
. map ( |( k, v) | ( k, RelaxedAugDict :: from_full ( & v) ) )
1178
1178
. collect :: < BTreeMap < _ , _ > > ( ) ;
@@ -1233,21 +1233,37 @@ impl Phase<FinalizeState> {
1233
1233
1234
1234
account_updates. par_sort_by ( |( a, ..) , ( b, ..) | a. cmp ( b) ) ;
1235
1235
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
+ } ) ;
1251
1267
1252
1268
let accounts_len = account_blocks. len ( ) ;
1253
1269
@@ -1257,10 +1273,9 @@ impl Phase<FinalizeState> {
1257
1273
. map ( |( k, v) | ( k, v. transactions . root_extra ( ) . clone ( ) , v) ) ,
1258
1274
) ?;
1259
1275
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 ( ) ;
1264
1279
1265
1280
Ok ( ProcessedAccounts {
1266
1281
account_blocks : account_blocks. build ( ) ?,
0 commit comments