Skip to content

Commit

Permalink
Do not eagerly clone compressed bytecodes
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Sep 4, 2024
1 parent b2dd9a5 commit 463254a
Show file tree
Hide file tree
Showing 22 changed files with 76 additions and 54 deletions.
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
tracer: Self::TracerDispatcher,
tx: Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
let tx_hash = tx.hash();
let main_result = self.main.inspect_transaction_with_bytecode_compression(
tracer,
Expand Down
4 changes: 2 additions & 2 deletions core/lib/multivm/src/versions/vm_1_3_2/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
tracer: Self::TracerDispatcher,
tx: Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
if let Some(storage_invocations) = tracer.storage_invocations {
self.vm
.execution_mode
Expand Down Expand Up @@ -156,7 +156,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
result,
)
} else {
(Ok(compressed_bytecodes), result)
(Ok(compressed_bytecodes.into()), result)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ impl BootloaderState {
l2_block.first_tx_index + l2_block.txs.len()
}

pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> Vec<CompressedBytecodeInfo> {
pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> &[CompressedBytecodeInfo] {
if let Some(tx) = self.last_l2_block().txs.last() {
tx.compressed_bytecodes.clone()
&tx.compressed_bytecodes
} else {
vec![]
&[]
}
}

Expand Down
7 changes: 5 additions & 2 deletions core/lib/multivm/src/versions/vm_1_4_1/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
tracer: Self::TracerDispatcher,
tx: Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
self.push_transaction_with_compression(tx, with_compression);
let result = self.inspect_inner(tracer, VmExecutionMode::OneTx, None);
if self.has_unpublished_bytecodes() {
Expand All @@ -115,7 +115,10 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
)
} else {
(
Ok(self.bootloader_state.get_last_tx_compressed_bytecodes()),
Ok(self
.bootloader_state
.get_last_tx_compressed_bytecodes()
.into()),
result,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ impl BootloaderState {
l2_block.first_tx_index + l2_block.txs.len()
}

pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> Vec<CompressedBytecodeInfo> {
pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> &[CompressedBytecodeInfo] {
if let Some(tx) = self.last_l2_block().txs.last() {
tx.compressed_bytecodes.clone()
&tx.compressed_bytecodes
} else {
vec![]
&[]
}
}

Expand Down
7 changes: 5 additions & 2 deletions core/lib/multivm/src/versions/vm_1_4_2/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
tracer: Self::TracerDispatcher,
tx: Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
self.push_transaction_with_compression(tx, with_compression);
let result = self.inspect_inner(tracer, VmExecutionMode::OneTx, None);
if self.has_unpublished_bytecodes() {
Expand All @@ -115,7 +115,10 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
)
} else {
(
Ok(self.bootloader_state.get_last_tx_compressed_bytecodes()),
Ok(self
.bootloader_state
.get_last_tx_compressed_bytecodes()
.into()),
result,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ impl BootloaderState {
l2_block.first_tx_index + l2_block.txs.len()
}

pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> Vec<CompressedBytecodeInfo> {
pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> &[CompressedBytecodeInfo] {
if let Some(tx) = self.last_l2_block().txs.last() {
tx.compressed_bytecodes.clone()
&tx.compressed_bytecodes
} else {
vec![]
&[]
}
}

Expand Down
7 changes: 5 additions & 2 deletions core/lib/multivm/src/versions/vm_boojum_integration/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
tracer: Self::TracerDispatcher,
tx: Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
self.push_transaction_with_compression(tx, with_compression);
let result = self.inspect_inner(tracer, VmExecutionMode::OneTx);
if self.has_unpublished_bytecodes() {
Expand All @@ -116,7 +116,10 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
)
} else {
(
Ok(self.bootloader_state.get_last_tx_compressed_bytecodes()),
Ok(self
.bootloader_state
.get_last_tx_compressed_bytecodes()
.into()),
result,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ impl BootloaderState {
l2_block.first_tx_index + l2_block.txs.len()
}

pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> Vec<CompressedBytecodeInfo> {
pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> &[CompressedBytecodeInfo] {
if let Some(tx) = self.last_l2_block().txs.last() {
tx.compressed_bytecodes.clone()
&tx.compressed_bytecodes
} else {
vec![]
&[]
}
}

Expand Down
12 changes: 6 additions & 6 deletions core/lib/multivm/src/versions/vm_fast/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use super::{
use crate::{
glue::GlueInto,
interface::{
storage::ReadStorage, BytecodeCompressionError, CompressedBytecodeInfo,
storage::ReadStorage, BytecodeCompressionError, BytecodeCompressionResult,
CurrentExecutionState, ExecutionResult, FinishedL1Batch, Halt, L1BatchEnv, L2BlockEnv,
Refunds, SystemEnv, TxRevertReason, VmEvent, VmExecutionLogs, VmExecutionMode,
VmExecutionResultAndLogs, VmExecutionStatistics, VmInterface, VmInterfaceHistoryEnabled,
Expand Down Expand Up @@ -548,17 +548,17 @@ impl<S: ReadStorage> VmInterface for Vm<S> {
(): Self::TracerDispatcher,
tx: zksync_types::Transaction,
with_compression: bool,
) -> (
Result<Vec<CompressedBytecodeInfo>, BytecodeCompressionError>,
VmExecutionResultAndLogs,
) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
self.push_transaction_inner(tx, 0, with_compression);
let result = self.inspect((), VmExecutionMode::OneTx);

let compression_result = if self.has_unpublished_bytecodes() {
Err(BytecodeCompressionError::BytecodeCompressionFailed)
} else {
Ok(self.bootloader_state.get_last_tx_compressed_bytecodes())
Ok(self
.bootloader_state
.get_last_tx_compressed_bytecodes()
.into())
};
(compression_result, result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ impl BootloaderState {
l2_block.first_tx_index + l2_block.txs.len()
}

pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> Vec<CompressedBytecodeInfo> {
pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> &[CompressedBytecodeInfo] {
if let Some(tx) = self.last_l2_block().txs.last() {
tx.compressed_bytecodes.clone()
&tx.compressed_bytecodes
} else {
vec![]
&[]
}
}

Expand Down
7 changes: 5 additions & 2 deletions core/lib/multivm/src/versions/vm_latest/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
tracer: Self::TracerDispatcher,
tx: Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
self.push_transaction_with_compression(tx, with_compression);
let result = self.inspect_inner(tracer, VmExecutionMode::OneTx, None);
if self.has_unpublished_bytecodes() {
Expand All @@ -151,7 +151,10 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
)
} else {
(
Ok(self.bootloader_state.get_last_tx_compressed_bytecodes()),
Ok(self
.bootloader_state
.get_last_tx_compressed_bytecodes()
.into()),
result,
)
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/multivm/src/versions/vm_m5/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ impl<S: Storage, H: HistoryMode> VmInterface for Vm<S, H> {
_tracer: Self::TracerDispatcher,
tx: Transaction,
_with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
crate::vm_m5::vm_with_bootloader::push_transaction_to_bootloader_memory(
&mut self.vm,
&tx,
self.system_env.execution_mode.glue_into(),
);
// Bytecode compression isn't supported
(Ok(vec![]), self.inspect((), VmExecutionMode::OneTx))
(Ok(vec![].into()), self.inspect((), VmExecutionMode::OneTx))
}

fn record_vm_memory_metrics(&self) -> VmMemoryMetrics {
Expand Down
4 changes: 2 additions & 2 deletions core/lib/multivm/src/versions/vm_m6/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<S: Storage, H: HistoryMode> VmInterface for Vm<S, H> {
tracer: Self::TracerDispatcher,
tx: Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
if let Some(storage_invocations) = tracer.storage_invocations {
self.vm
.execution_mode
Expand Down Expand Up @@ -182,7 +182,7 @@ impl<S: Storage, H: HistoryMode> VmInterface for Vm<S, H> {
result,
)
} else {
(Ok(compressed_bytecodes), result)
(Ok(compressed_bytecodes.into()), result)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ impl BootloaderState {
l2_block.first_tx_index + l2_block.txs.len()
}

pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> Vec<CompressedBytecodeInfo> {
pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> &[CompressedBytecodeInfo] {
if let Some(tx) = self.last_l2_block().txs.last() {
tx.compressed_bytecodes.clone()
&tx.compressed_bytecodes
} else {
vec![]
&[]
}
}

Expand Down
7 changes: 5 additions & 2 deletions core/lib/multivm/src/versions/vm_refunds_enhancement/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
dispatcher: Self::TracerDispatcher,
tx: Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
self.push_transaction_with_compression(tx, with_compression);
let result = self.inspect(dispatcher, VmExecutionMode::OneTx);
if self.has_unpublished_bytecodes() {
Expand All @@ -109,7 +109,10 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
)
} else {
(
Ok(self.bootloader_state.get_last_tx_compressed_bytecodes()),
Ok(self
.bootloader_state
.get_last_tx_compressed_bytecodes()
.into()),
result,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ impl BootloaderState {
l2_block.first_tx_index + l2_block.txs.len()
}

pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> Vec<CompressedBytecodeInfo> {
pub(crate) fn get_last_tx_compressed_bytecodes(&self) -> &[CompressedBytecodeInfo] {
if let Some(tx) = self.last_l2_block().txs.last() {
tx.compressed_bytecodes.clone()
&tx.compressed_bytecodes
} else {
vec![]
&[]
}
}

Expand Down
7 changes: 5 additions & 2 deletions core/lib/multivm/src/versions/vm_virtual_blocks/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
tracer: TracerDispatcher<S, H::VmVirtualBlocksMode>,
tx: Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
self.push_transaction_with_compression(tx, with_compression);
let result = self.inspect_inner(tracer, VmExecutionMode::OneTx);
if self.has_unpublished_bytecodes() {
Expand All @@ -109,7 +109,10 @@ impl<S: WriteStorage, H: HistoryMode> VmInterface for Vm<S, H> {
)
} else {
(
Ok(self.bootloader_state.get_last_tx_compressed_bytecodes()),
Ok(self
.bootloader_state
.get_last_tx_compressed_bytecodes()
.into()),
result,
)
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/vm_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<S: ReadStorage, H: HistoryMode> VmInterface for VmInstance<S, H> {
dispatcher: Self::TracerDispatcher,
tx: zksync_types::Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
dispatch_vm!(self.inspect_transaction_with_bytecode_compression(
dispatcher.into(),
tx,
Expand Down
9 changes: 5 additions & 4 deletions core/lib/vm_executor/src/batch/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl<S: ReadStorage + 'static> CommandReceiver<S> {
.unwrap_or_default();
return Ok(BatchTransactionExecutionResult {
tx_result: Box::new(tx_result),
compressed_bytecodes,
compressed_bytecodes: compressed_bytecodes.into_owned(),
call_traces,
});
}
Expand All @@ -269,8 +269,9 @@ impl<S: ReadStorage + 'static> CommandReceiver<S> {

let (compression_result, tx_result) =
vm.inspect_transaction_with_bytecode_compression(tracer.into(), tx.clone(), false);
let compressed_bytecodes =
compression_result.context("compression failed when it wasn't applied")?;
let compressed_bytecodes = compression_result
.context("compression failed when it wasn't applied")?
.into_owned();

// TODO implement tracer manager which will be responsible
// for collecting result from all tracers and save it to the database
Expand Down Expand Up @@ -308,7 +309,7 @@ impl<S: ReadStorage + 'static> CommandReceiver<S> {
.unwrap_or_default();
Ok(BatchTransactionExecutionResult {
tx_result: Box::new(tx_result),
compressed_bytecodes,
compressed_bytecodes: compressed_bytecodes.into_owned(),
call_traces,
})
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use crate::CompressedBytecodeInfo;

/// Errors related to bytecode compression.
Expand All @@ -9,4 +11,5 @@ pub enum BytecodeCompressionError {
}

/// Result of compressing bytecodes used by a transaction.
pub type BytecodeCompressionResult = Result<Vec<CompressedBytecodeInfo>, BytecodeCompressionError>;
pub type BytecodeCompressionResult<'a> =
Result<Cow<'a, [CompressedBytecodeInfo]>, BytecodeCompressionError>;
4 changes: 2 additions & 2 deletions core/lib/vm_interface/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub trait VmInterface {
tracer: Self::TracerDispatcher,
tx: Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs);
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs);

/// Record VM memory metrics.
fn record_vm_memory_metrics(&self) -> VmMemoryMetrics;
Expand All @@ -63,7 +63,7 @@ pub trait VmInterfaceExt: VmInterface {
&mut self,
tx: Transaction,
with_compression: bool,
) -> (BytecodeCompressionResult, VmExecutionResultAndLogs) {
) -> (BytecodeCompressionResult<'_>, VmExecutionResultAndLogs) {
self.inspect_transaction_with_bytecode_compression(
Self::TracerDispatcher::default(),
tx,
Expand Down

0 comments on commit 463254a

Please sign in to comment.