Skip to content

Commit

Permalink
feat(core): change BTreeSet to IntervalsTree in allocations (#3791)
Browse files Browse the repository at this point in the history
  • Loading branch information
grishasobol authored May 18, 2024
1 parent 463e2ae commit 440815e
Show file tree
Hide file tree
Showing 34 changed files with 762 additions and 463 deletions.
10 changes: 5 additions & 5 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use gear_core::{
ids::{CodeId, MessageId, ProgramId},
memory::PageBuf,
message::DispatchKind,
pages::{GearPage, WasmPage, WasmPagesAmount},
pages::{numerated::tree::IntervalsTree, GearPage, WasmPage, WasmPagesAmount},
program::MemoryInfix,
reservation::GasReservationMap,
};
Expand Down Expand Up @@ -281,10 +281,10 @@ impl<BlockNumber: Copy + Saturating> core::convert::TryFrom<Program<BlockNumber>
#[codec(crate = codec)]
#[scale_info(crate = scale_info)]
pub struct ActiveProgram<BlockNumber: Copy + Saturating> {
/// Set of dynamic wasm page numbers, which are allocated by the program.
pub allocations: BTreeSet<WasmPage>,
/// Set of gear pages numbers, which has data in storage.
pub pages_with_data: BTreeSet<GearPage>,
/// Set of wasm pages, that were allocated by the program.
pub allocations: IntervalsTree<WasmPage>,
/// Set of gear pages, that have data in storage.
pub pages_with_data: IntervalsTree<GearPage>,
pub memory_infix: MemoryInfix,
pub gas_reservation_map: GasReservationMap,
pub code_hash: H256,
Expand Down
12 changes: 8 additions & 4 deletions common/src/paused_program_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ impl From<(BTreeSet<WasmPage>, H256, MemoryMap)> for Item {

impl<BlockNumber: Copy + Saturating> From<(ActiveProgram<BlockNumber>, MemoryMap)> for Item {
fn from((program, memory_pages): (ActiveProgram<BlockNumber>, MemoryMap)) -> Self {
From::from((program.allocations, program.code_hash, memory_pages))
From::from((
program.allocations.points_iter().collect(),
program.code_hash,
memory_pages,
))
}
}

Expand Down Expand Up @@ -130,7 +134,7 @@ pub trait PausedProgramStorage: super::ProgramStorage {
let memory_pages = match Self::get_program_data_for_pages(
program_id,
program.memory_infix,
program.pages_with_data.iter(),
program.pages_with_data.points_iter(),
) {
Ok(memory_pages) => memory_pages,
Err(e) => {
Expand Down Expand Up @@ -268,7 +272,7 @@ pub trait PausedProgramStorage: super::ProgramStorage {
let memory_pages = Self::get_program_data_for_pages(
session.program_id,
MemoryInfix::new(session_id),
session.pages_with_data.iter(),
session.pages_with_data.iter().copied(),
)
.unwrap_or_default();
let code_hash = session.code_hash.into_origin();
Expand All @@ -285,7 +289,7 @@ pub trait PausedProgramStorage: super::ProgramStorage {
remaining_pages,
} = item;
let program = ActiveProgram {
allocations,
allocations: allocations.into_iter().collect(),
pages_with_data: memory_pages
.keys()
.copied()
Expand Down
8 changes: 4 additions & 4 deletions common/src/program_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,16 @@ pub trait ProgramStorage {
}

/// Return program data for each page from `pages`.
fn get_program_data_for_pages<'a>(
fn get_program_data_for_pages(
program_id: ProgramId,
memory_infix: MemoryInfix,
pages: impl Iterator<Item = &'a GearPage>,
pages: impl Iterator<Item = GearPage>,
) -> Result<MemoryMap, Self::Error> {
let mut pages_data = BTreeMap::new();
for page in pages {
let data = Self::MemoryPageMap::get(&program_id, &memory_infix, page)
let data = Self::MemoryPageMap::get(&program_id, &memory_infix, &page)
.ok_or(Self::InternalError::cannot_find_page_data())?;
pages_data.insert(*page, data);
pages_data.insert(page, data);
}

Ok(pages_data)
Expand Down
12 changes: 6 additions & 6 deletions core-processor/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use gear_core::{
message::{
ContextStore, Dispatch, DispatchKind, IncomingDispatch, MessageWaitedType, StoredDispatch,
},
pages::{GearPage, WasmPage, WasmPagesAmount},
pages::{numerated::tree::IntervalsTree, GearPage, WasmPage, WasmPagesAmount},
program::{MemoryInfix, Program},
reservation::{GasReservationMap, GasReserver},
};
Expand Down Expand Up @@ -82,7 +82,7 @@ pub struct DispatchResult {
/// Page updates.
pub page_update: BTreeMap<GearPage, PageBuf>,
/// New allocations set for program if it has been changed.
pub allocations: Option<BTreeSet<WasmPage>>,
pub allocations: Option<IntervalsTree<WasmPage>>,
/// Whether this execution sent out a reply.
pub reply_sent: bool,
}
Expand Down Expand Up @@ -250,7 +250,7 @@ pub enum JournalNote {
/// Program id.
program_id: ProgramId,
/// New allocations set for the program.
allocations: BTreeSet<WasmPage>,
allocations: IntervalsTree<WasmPage>,
},
/// Send value
SendValue {
Expand Down Expand Up @@ -379,7 +379,7 @@ pub trait JournalHandler {
/// Process page update.
fn update_pages_data(&mut self, program_id: ProgramId, pages_data: BTreeMap<GearPage, PageBuf>);
/// Process [JournalNote::UpdateAllocations].
fn update_allocations(&mut self, program_id: ProgramId, allocations: BTreeSet<WasmPage>);
fn update_allocations(&mut self, program_id: ProgramId, allocations: IntervalsTree<WasmPage>);
/// Send value.
fn send_value(&mut self, from: ProgramId, to: Option<ProgramId>, value: u128);
/// Store new programs in storage.
Expand Down Expand Up @@ -511,8 +511,8 @@ pub struct Actor {
/// Executable actor data.
#[derive(Clone, Debug)]
pub struct ExecutableActorData {
/// Set of dynamic wasm page numbers, which are allocated by the program.
pub allocations: BTreeSet<WasmPage>,
/// Set of wasm pages, which are allocated by the program.
pub allocations: IntervalsTree<WasmPage>,
/// The infix of memory pages in a storage.
pub memory_infix: MemoryInfix,
/// Id of the program code.
Expand Down
6 changes: 4 additions & 2 deletions core-processor/src/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ pub struct ExtCosts {
pub syscalls: SyscallCosts,
/// Rent costs.
pub rent: RentCosts,
/// Memory grow cost per page.
pub mem_grow: CostOf<WasmPagesAmount>,
/// Memory grow cost.
pub mem_grow: CostOf<CallsAmount>,
/// Memory grow per page cost.
pub mem_grow_per_page: CostOf<WasmPagesAmount>,
}

/// Costs for message processing
Expand Down
8 changes: 4 additions & 4 deletions core-processor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
configs::{BlockInfo, ExecutionSettings},
ext::{ProcessorContext, ProcessorExternalities},
};
use alloc::{collections::BTreeSet, format, string::String, vec::Vec};
use alloc::{format, string::String, vec::Vec};
use gear_core::{
code::InstrumentedCode,
env::Externalities,
Expand All @@ -35,7 +35,7 @@ use gear_core::{
ContextSettings, DispatchKind, IncomingDispatch, IncomingMessage, MessageContext,
WasmEntryPoint,
},
pages::WasmPage,
pages::{numerated::tree::IntervalsTree, WasmPage},
program::{MemoryInfix, Program},
reservation::GasReserver,
};
Expand Down Expand Up @@ -249,7 +249,7 @@ where
pub fn execute_for_reply<Ext, EP>(
function: EP,
instrumented_code: InstrumentedCode,
allocations: Option<BTreeSet<WasmPage>>,
allocations: Option<IntervalsTree<WasmPage>>,
program_info: Option<(ProgramId, MemoryInfix)>,
payload: Vec<u8>,
gas_limit: u64,
Expand All @@ -267,7 +267,7 @@ where
let program = Program::new(program_id, memory_infix, instrumented_code);
let static_pages = program.static_pages();
let allocations = allocations.unwrap_or_else(|| program.allocations().clone());
let memory_size = allocations.last().map(|p| p.inc()).unwrap_or(static_pages);
let memory_size = allocations.end().map(|p| p.inc()).unwrap_or(static_pages);

let message_context = MessageContext::new(
IncomingDispatch::new(
Expand Down
18 changes: 10 additions & 8 deletions core-processor/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ use gear_core::{
ContextOutcomeDrain, ContextStore, Dispatch, GasLimit, HandlePacket, InitPacket,
MessageContext, Packet, ReplyPacket,
},
pages::{numerated::interval::Interval, GearPage, WasmPage, WasmPagesAmount},
pages::{
numerated::{interval::Interval, tree::IntervalsTree},
GearPage, WasmPage, WasmPagesAmount,
},
program::MemoryInfix,
reservation::GasReserver,
};
Expand Down Expand Up @@ -150,7 +153,7 @@ pub struct ExtInfo {
pub gas_amount: GasAmount,
pub gas_reserver: GasReserver,
pub system_reservation_context: SystemReservationContext,
pub allocations: Option<BTreeSet<WasmPage>>,
pub allocations: Option<IntervalsTree<WasmPage>>,
pub pages_data: BTreeMap<GearPage, PageBuf>,
pub generated_dispatches: Vec<(Dispatch, u32, Option<ReservationId>)>,
pub awakening: Vec<(MessageId, u32)>,
Expand Down Expand Up @@ -381,7 +384,7 @@ impl ProcessorExternalities for Ext {
let mut accessed_pages = gear_lazy_pages_interface::get_write_accessed_pages();
accessed_pages.retain(|p| {
let wasm_page: WasmPage = p.to_page();
wasm_page < static_pages || allocations.contains(&wasm_page)
wasm_page < static_pages || allocations.contains(wasm_page)
});
log::trace!("accessed pages numbers = {:?}", accessed_pages);

Expand Down Expand Up @@ -771,16 +774,15 @@ impl Externalities for Ext {
let pages = WasmPagesAmount::try_from(pages_num)
.map_err(|_| AllocError::ProgramAllocOutOfBounds)?;

// Charge for pages amount
self.charge_gas_if_enough(self.context.costs.syscalls.alloc_per_page.cost_for(pages))?;

self.context
.allocations_context
.alloc::<Context, LazyGrowHandler>(ctx, mem, pages, |pages| {
let gas_for_call = self.context.costs.mem_grow.cost_for_one();
let gas_for_pages = self.context.costs.mem_grow_per_page.cost_for(pages);
Ext::charge_gas_if_enough(
&mut self.context.gas_counter,
&mut self.context.gas_allowance_counter,
self.context.costs.mem_grow.cost_for(pages),
gas_for_call.saturating_add(gas_for_pages),
)
})
.map_err(Into::into)
Expand Down Expand Up @@ -1352,7 +1354,7 @@ mod tests {

let allocations_context = AllocationsContext::try_new(
512.into(),
BTreeSet::from([existing_page]),
[existing_page].into_iter().collect(),
1.into(),
None,
512.into(),
Expand Down
8 changes: 4 additions & 4 deletions core-processor/src/precharge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ use crate::{
processing::{process_allowance_exceed, process_execution_error, process_success},
ContextChargedForCode, ContextChargedForInstrumentation,
};
use alloc::{collections::BTreeSet, vec::Vec};
use alloc::vec::Vec;
use gear_core::{
costs::BytesAmount,
gas::{ChargeResult, GasAllowanceCounter, GasCounter},
ids::ProgramId,
message::{IncomingDispatch, MessageWaitedType},
pages::{WasmPage, WasmPagesAmount},
pages::{numerated::tree::IntervalsTree, WasmPage, WasmPagesAmount},
};

/// Operation related to gas charging.
Expand Down Expand Up @@ -150,14 +150,14 @@ impl<'a> GasPrecharger<'a> {
/// Returns size of wasm memory buffer which must be created in execution environment.
pub fn charge_gas_for_pages(
&mut self,
allocations: &BTreeSet<WasmPage>,
allocations: &IntervalsTree<WasmPage>,
static_pages: WasmPagesAmount,
) -> Result<WasmPagesAmount, PrechargeError> {
// Charging gas for static pages.
let amount = self.costs.static_page.cost_for(static_pages);
self.charge_gas(PreChargeGasOperation::StaticPages, amount)?;

if let Some(page) = allocations.last() {
if let Some(page) = allocations.end() {
Ok(page.inc())
} else {
Ok(static_pages)
Expand Down
3 changes: 0 additions & 3 deletions core/src/costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ pub struct SyscallCosts {
/// Cost of calling `alloc`.
pub alloc: CostOf<CallsAmount>,

/// Cost per allocated page for `alloc`.
pub alloc_per_page: CostOf<WasmPagesAmount>,

/// Cost of calling `free`.
pub free: CostOf<CallsAmount>,

Expand Down
Loading

0 comments on commit 440815e

Please sign in to comment.