Skip to content

Commit 9a91c53

Browse files
authored
Merge pull request #1577 from Phala-Network/box-ordmap-value
pruntime: Box large value in OrdMap
2 parents 39f242c + 93cd3cc commit 9a91c53

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

crates/phactory/src/contracts/support/keeper.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use crate::{
1111
im_helpers::{ordmap_for_each_mut, OrdMap},
1212
};
1313

14-
type ContractMap = OrdMap<AccountId, Contract>;
14+
// size_of::<Contract>() == 1064, if we don't box it, it would exceed the stack capacity
15+
// when inserting data, even if we have an 8MB stack size. Not sure why the OrdMap::insert
16+
// increases the size so significantly.
17+
type ContractMap = OrdMap<AccountId, Box<Contract>>;
1518

1619
#[derive(Default, Serialize, Deserialize, Clone, ::scale_info::TypeInfo)]
1720
pub struct ContractsKeeper {
@@ -24,19 +27,22 @@ pub struct ContractsKeeper {
2427

2528
impl ContractsKeeper {
2629
pub fn insert(&mut self, contract: Contract) {
27-
self.contracts.insert(contract.address().clone(), contract);
30+
self.contracts
31+
.insert(contract.address().clone(), Box::new(contract));
2832
}
2933

3034
pub fn keys(&self) -> impl Iterator<Item = &AccountId> {
3135
self.contracts.keys()
3236
}
3337

3438
pub fn get_mut(&mut self, id: &AccountId) -> Option<&mut Contract> {
35-
self.contracts.get_mut(id)
39+
let boxed = self.contracts.get_mut(id)?;
40+
Some(boxed)
3641
}
3742

3843
pub fn get(&self, id: &AccountId) -> Option<&Contract> {
39-
self.contracts.get(id)
44+
let boxed = self.contracts.get(id)?;
45+
Some(boxed)
4046
}
4147

4248
#[allow(clippy::len_without_is_empty)]
@@ -56,11 +62,11 @@ impl ContractsKeeper {
5662
#[allow(clippy::iter_kv_map)]
5763
std::mem::take(&mut self.contracts)
5864
.into_iter()
59-
.map(|(_, v)| v)
65+
.map(|(_, v)| *v)
6066
}
6167

6268
pub fn iter(&self) -> impl Iterator<Item = (&AccountId, &Contract)> {
63-
self.contracts.iter()
69+
self.contracts.iter().map(|(k, v)| (k, &**v))
6470
}
6571

6672
pub fn apply_local_cache_quotas(&self) {
@@ -73,7 +79,7 @@ pub(super) trait ToWeight {
7379
fn to_weight(&self) -> u32;
7480
}
7581

76-
impl ToWeight for Contract {
82+
impl ToWeight for Box<Contract> {
7783
fn to_weight(&self) -> u32 {
7884
self.weight
7985
}

crates/phactory/src/prpc_service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use phala_types::{
3131
ChallengeHandlerInfo, EncryptedWorkerKey, HandoverChallenge, SignedContentType,
3232
VersionedWorkerEndpoints, WorkerEndpointPayload, WorkerPublicKey, WorkerRegistrationInfoV2,
3333
};
34-
use pink_loader::types::{AccountId, ExecSideEffects, ExecutionMode};
3534
use phala_types::{DcapChallengeHandlerInfo, DcapHandoverChallenge};
35+
use pink_loader::types::{AccountId, ExecSideEffects, ExecutionMode};
3636
use sp_application_crypto::UncheckedFrom;
3737
use sp_core::hashing::blake2_256;
3838
use tracing::{error, info};

crates/phactory/src/system/gk.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ pub struct ComputingEconomics<MsgChan> {
639639
system_events: TypedReceiver<SystemEvent>,
640640
gatekeeper_events: TypedReceiver<GatekeeperEvent>,
641641
#[cfg_attr(not(test), codec(skip))]
642-
workers: OrdMap<WorkerPublicKey, WorkerInfo>,
642+
workers: OrdMap<WorkerPublicKey, Box<WorkerInfo>>,
643643
#[codec(skip)]
644644
tokenomic_params: tokenomic::Params,
645645
#[serde(skip, default)]
@@ -710,12 +710,12 @@ impl<MsgChan: MessageChannel<Signer = Sr25519Signer>> ComputingEconomics<MsgChan
710710
pub fn dump_workers_state(&self) -> Vec<(WorkerPublicKey, pb::WorkerState)> {
711711
self.workers
712712
.values()
713-
.map(|info| (info.state.pubkey, info.into()))
713+
.map(|info| (info.state.pubkey, (&**info).into()))
714714
.collect()
715715
}
716716

717717
pub fn worker_state(&self, pubkey: &WorkerPublicKey) -> Option<pb::WorkerState> {
718-
self.workers.get(pubkey).map(Into::into)
718+
self.workers.get(pubkey).map(|info| (&**info).into())
719719
}
720720

721721
pub fn will_process_block(&mut self, block: &BlockInfo<'_>) {
@@ -1019,7 +1019,7 @@ impl<MsgChan: MessageChannel<Signer = Sr25519Signer>> ComputingEconomics<MsgChan
10191019
let _ = self
10201020
.workers
10211021
.entry(*pubkey)
1022-
.or_insert_with(|| WorkerInfo::new(*pubkey));
1022+
.or_insert_with(|| Box::new(WorkerInfo::new(*pubkey)));
10231023
}
10241024

10251025
let log_on = log::log_enabled!(log::Level::Debug);

0 commit comments

Comments
 (0)