Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pruntime: Box large value in OrdMap #1577

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions crates/phactory/src/contracts/support/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use crate::{
im_helpers::{ordmap_for_each_mut, OrdMap},
};

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

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

impl ContractsKeeper {
pub fn insert(&mut self, contract: Contract) {
self.contracts.insert(contract.address().clone(), contract);
self.contracts
.insert(contract.address().clone(), Box::new(contract));
}

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

pub fn get_mut(&mut self, id: &AccountId) -> Option<&mut Contract> {
self.contracts.get_mut(id)
let boxed = self.contracts.get_mut(id)?;
Some(boxed)
}

pub fn get(&self, id: &AccountId) -> Option<&Contract> {
self.contracts.get(id)
let boxed = self.contracts.get(id)?;
Some(boxed)
}

#[allow(clippy::len_without_is_empty)]
Expand All @@ -56,11 +62,11 @@ impl ContractsKeeper {
#[allow(clippy::iter_kv_map)]
std::mem::take(&mut self.contracts)
.into_iter()
.map(|(_, v)| v)
.map(|(_, v)| *v)
}

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

pub fn apply_local_cache_quotas(&self) {
Expand All @@ -73,7 +79,7 @@ pub(super) trait ToWeight {
fn to_weight(&self) -> u32;
}

impl ToWeight for Contract {
impl ToWeight for Box<Contract> {
fn to_weight(&self) -> u32 {
self.weight
}
Expand Down
2 changes: 1 addition & 1 deletion crates/phactory/src/prpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use phala_types::{
ChallengeHandlerInfo, EncryptedWorkerKey, HandoverChallenge, SignedContentType,
VersionedWorkerEndpoints, WorkerEndpointPayload, WorkerPublicKey, WorkerRegistrationInfoV2,
};
use pink_loader::types::{AccountId, ExecSideEffects, ExecutionMode};
use phala_types::{DcapChallengeHandlerInfo, DcapHandoverChallenge};
use pink_loader::types::{AccountId, ExecSideEffects, ExecutionMode};
use sp_application_crypto::UncheckedFrom;
use sp_core::hashing::blake2_256;
use tracing::{error, info};
Expand Down
8 changes: 4 additions & 4 deletions crates/phactory/src/system/gk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ pub struct ComputingEconomics<MsgChan> {
system_events: TypedReceiver<SystemEvent>,
gatekeeper_events: TypedReceiver<GatekeeperEvent>,
#[cfg_attr(not(test), codec(skip))]
workers: OrdMap<WorkerPublicKey, WorkerInfo>,
workers: OrdMap<WorkerPublicKey, Box<WorkerInfo>>,
#[codec(skip)]
tokenomic_params: tokenomic::Params,
#[serde(skip, default)]
Expand Down Expand Up @@ -710,12 +710,12 @@ impl<MsgChan: MessageChannel<Signer = Sr25519Signer>> ComputingEconomics<MsgChan
pub fn dump_workers_state(&self) -> Vec<(WorkerPublicKey, pb::WorkerState)> {
self.workers
.values()
.map(|info| (info.state.pubkey, info.into()))
.map(|info| (info.state.pubkey, (&**info).into()))
.collect()
}

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

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

let log_on = log::log_enabled!(log::Level::Debug);
Expand Down
Loading