Skip to content

Commit

Permalink
Lambda VM criterion benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Oppen committed Aug 13, 2024
1 parent 6bde7e7 commit 4603b17
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
21 changes: 11 additions & 10 deletions core/lib/multivm/src/versions/era_vm/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use era_vm::{
EraVM, VMState,
};
use once_cell::sync::Lazy;
use zksync_state::{InMemoryStorage, StoragePtr, StorageView, WriteStorage};
use zksync_state::{InMemoryStorage, ReadStorage, StoragePtr, StorageView, WriteStorage};
use zksync_types::{
l1::is_l1_tx_type, AccountTreeId, StorageKey, Transaction, BOOTLOADER_ADDRESS, H160,
KNOWN_CODES_STORAGE_ADDRESS, U256,
Expand Down Expand Up @@ -35,7 +35,7 @@ use crate::{
},
};

pub struct Vm<S: WriteStorage> {
pub struct Vm<S: ReadStorage> {
pub(crate) inner: EraVM,
suspended_at: u16,
gas_for_account_validation: u32,
Expand All @@ -55,7 +55,7 @@ pub struct Vm<S: WriteStorage> {
}

/// Encapsulates creating VM instance based on the provided environment.
impl<S: WriteStorage + 'static> VmFactory<S> for Vm<S> {
impl<S: ReadStorage + 'static> VmFactory<S> for Vm<S> {
/// Creates a new VM instance.
fn new(batch_env: L1BatchEnv, system_env: SystemEnv, storage: StoragePtr<S>) -> Self {
let bootloader_code = system_env
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<S: WriteStorage + 'static> VmFactory<S> for Vm<S> {

}

impl<S: WriteStorage + 'static> Vm<S> {
impl<S: ReadStorage + 'static> Vm<S> {
pub fn run(&mut self, execution_mode: VmExecutionMode) -> ExecutionResult {
loop {
let (result, blob_tracer) = self.inner.run_program_with_custom_bytecode();
Expand Down Expand Up @@ -342,7 +342,7 @@ impl<S: WriteStorage + 'static> Vm<S> {
}
}

impl<S: WriteStorage + 'static> VmInterface for Vm<S> {
impl<S: ReadStorage + 'static> VmInterface for Vm<S> {
type TracerDispatcher = ();

fn push_transaction(&mut self, tx: Transaction) {
Expand Down Expand Up @@ -449,7 +449,7 @@ impl<S: WriteStorage + 'static> VmInterface for Vm<S> {
}
}

impl<S: WriteStorage + 'static> VmInterfaceHistoryEnabled for Vm<S> {
impl<S: ReadStorage + 'static> VmInterfaceHistoryEnabled for Vm<S> {
fn make_snapshot(&mut self) {
todo!()
}
Expand All @@ -464,13 +464,13 @@ impl<S: WriteStorage + 'static> VmInterfaceHistoryEnabled for Vm<S> {
}

#[derive(Debug, Clone)]
pub struct World<S: WriteStorage> {
pub struct World<S: ReadStorage> {
pub storage: StoragePtr<S>,
pub contract_storage: Rc<RefCell<HashMap<U256, Vec<U256>>>>,
pub l2_to_l1_logs: Vec<L2ToL1Log>,
}

impl<S: WriteStorage> World<S> {
impl<S: ReadStorage> World<S> {
pub fn new_empty(storage: StoragePtr<S>) -> Self {
let contract_storage = Rc::new(RefCell::new(HashMap::new()));
let l2_to_l1_logs = Vec::new();
Expand All @@ -493,7 +493,7 @@ impl<S: WriteStorage> World<S> {
}
}

impl<S: WriteStorage> era_vm::store::InitialStorage for World<S> {
impl<S: ReadStorage> era_vm::store::InitialStorage for World<S> {
fn storage_read(&self, key: EraStorageKey) -> Result<Option<U256>, StorageError> {
let mut storage = RefCell::borrow_mut(&self.storage);
Ok(Some(
Expand All @@ -508,7 +508,7 @@ impl<S: WriteStorage> era_vm::store::InitialStorage for World<S> {
}
}

impl<S: WriteStorage> era_vm::store::ContractStorage for World<S> {
impl<S: ReadStorage> era_vm::store::ContractStorage for World<S> {
fn decommit(&self, hash: U256) -> Result<Option<Vec<U256>>, StorageError> {
Ok(Some(
self.contract_storage
Expand Down Expand Up @@ -537,6 +537,7 @@ impl<S: WriteStorage> era_vm::store::ContractStorage for World<S> {

#[cfg(test)]
mod tests {
use super::*;
use std::{cell::RefCell, path::PathBuf, rc::Rc};

use once_cell::sync::Lazy;
Expand Down
5 changes: 4 additions & 1 deletion core/tests/vm-benchmark/benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use criterion::{
use zksync_types::Transaction;
use zksync_vm_benchmark_harness::{
cut_to_allowed_bytecode_size, get_deploy_tx, get_heavy_load_test_tx, get_load_test_deploy_tx,
get_load_test_tx, get_realistic_load_test_tx, BenchmarkingVm, BenchmarkingVmFactory, Fast,
get_load_test_tx, get_realistic_load_test_tx, BenchmarkingVm, BenchmarkingVmFactory, Fast, Lambda,
Legacy, LoadTestParams,
};

Expand Down Expand Up @@ -90,9 +90,12 @@ criterion_group!(
benches,
benches_in_folder::<Fast, false>,
benches_in_folder::<Fast, true>,
benches_in_folder::<Lambda, false>,
benches_in_folder::<Lambda, true>,
benches_in_folder::<Legacy, false>,
benches_in_folder::<Legacy, true>,
bench_load_test::<Fast>,
bench_load_test::<Lambda>,
bench_load_test::<Legacy>
);
criterion_main!(benches);
24 changes: 23 additions & 1 deletion core/tests/vm-benchmark/harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use once_cell::sync::Lazy;
pub use zksync_contracts::test_contracts::LoadnextContractExecutionParams as LoadTestParams;
use zksync_contracts::{deployer_contract, BaseSystemContracts, TestContract};
use zksync_multivm::{
era_vm::vm::Vm,
era_vm,
interface::{
ExecutionResult, L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode, VmExecutionMode,
VmExecutionResultAndLogs, VmFactory, VmInterface, VmInterfaceHistoryEnabled,
Expand Down Expand Up @@ -75,6 +75,7 @@ static PRIVATE_KEY: Lazy<K256PrivateKey> =
#[derive(Debug, Clone, Copy)]
pub enum VmLabel {
Fast,
Lambda,
Legacy,
}

Expand All @@ -83,6 +84,7 @@ impl VmLabel {
pub const fn as_str(self) -> &'static str {
match self {
Self::Fast => "fast",
Self::Lambda => "lambda",
Self::Legacy => "legacy",
}
}
Expand All @@ -91,6 +93,7 @@ impl VmLabel {
pub const fn as_suffix(self) -> &'static str {
match self {
Self::Fast => "",
Self::Lambda => "/lambda",
Self::Legacy => "/legacy",
}
}
Expand All @@ -112,6 +115,25 @@ pub trait BenchmarkingVmFactory {
) -> Self::Instance;
}

/// Factory for the LambdaClass VM.
#[derive(Debug)]
pub struct Lambda(());

impl BenchmarkingVmFactory for Lambda {
const LABEL: VmLabel = VmLabel::Lambda;

type Instance = era_vm::vm::Vm<InMemoryStorage>;

fn create(
batch_env: L1BatchEnv,
system_env: SystemEnv,
storage: &'static InMemoryStorage,
) -> Self::Instance {
let storage = Rc::new(RefCell::new(storage.clone()));
era_vm::vm::Vm::new(batch_env, system_env, storage)
}
}

/// Factory for the new / fast VM.
#[derive(Debug)]
pub struct Fast(());
Expand Down

0 comments on commit 4603b17

Please sign in to comment.