Skip to content

Commit

Permalink
do not process read/writes if they are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
ukint-vs committed Aug 20, 2023
1 parent 586ecf8 commit faa98f9
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions lazy-pages/src/host_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ pub fn pre_process_memory_accesses(
let mut ctx = ctx.borrow_mut();
let ctx = ctx.execution_context_mut()?;

let mut read_pages = BTreeSet::new();
accesses_pages(ctx, reads, &mut read_pages)?;

let gas_charger = {
GasCharger {
read_cost: ctx.weight(WeightNo::HostFuncRead),
Expand All @@ -150,34 +147,44 @@ pub fn pre_process_memory_accesses(
load_data_cost: ctx.weight(WeightNo::LoadPageDataFromStorage),
}
};

let status = process::process_lazy_pages(
ctx,
HostFuncAccessHandler {
is_write: false,
gas_counter,
gas_charger: gas_charger.clone(),
},
read_pages,
)?;
let mut status = Status::Normal;

if !reads.is_empty() {
let mut read_pages = BTreeSet::new();
accesses_pages(ctx, reads, &mut read_pages)?;

status = process::process_lazy_pages(
ctx,
HostFuncAccessHandler {
is_write: false,
gas_counter,
gas_charger: gas_charger.clone(),
},
read_pages,
)?;
}

// Does not process write accesses if gas exceeded.
if !matches!(status, Status::Normal) {
return Ok(status);
}

let mut write_pages = BTreeSet::new();
accesses_pages(ctx, writes, &mut write_pages)?;

process::process_lazy_pages(
ctx,
HostFuncAccessHandler {
is_write: true,
gas_counter,
gas_charger,
},
write_pages,
)
if !reads.is_empty() {
let mut write_pages = BTreeSet::new();
accesses_pages(ctx, writes, &mut write_pages)?;

status = process::process_lazy_pages(
ctx,
HostFuncAccessHandler {
is_write: true,
gas_counter,
gas_charger,
},
write_pages,
)?;
}

Ok(status)
})
.map_err(|err| match err {
Error::WasmMemAddrIsNotSet | Error::OutOfWasmMemoryAccess => {
Expand Down

0 comments on commit faa98f9

Please sign in to comment.