Skip to content

Commit

Permalink
bring back lazy static variable
Browse files Browse the repository at this point in the history
  • Loading branch information
elfedy committed Dec 3, 2024
1 parent 7c8d167 commit 18c971f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions crates/zksync/core/src/vm/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ use zksync_types::{
};
use zksync_utils::{be_words_to_bytes, h256_to_account_address, h256_to_u256, u256_to_h256};

use std::{collections::HashMap, fmt::Debug, sync::Arc};
use std::{
collections::HashMap,
fmt::Debug,
sync::{Arc, LazyLock},
};

use crate::{
convert::{ConvertAddress, ConvertH160, ConvertH256, ConvertRU256, ConvertU256},
Expand Down Expand Up @@ -756,25 +760,26 @@ where
.unwrap_or_default()
}

/// Default maximum size allowed for factory_deps during create.
/// Maximum size allowed for factory_deps during create.
/// We batch factory_deps till this upper limit if there are multiple deps.
/// These batches are then deployed individually.
const DEFAULT_MAX_FACTORY_DEPENDENCIES_SIZE_BYTES: usize = 100_000;
static MAX_FACTORY_DEPENDENCIES_SIZE_BYTES: LazyLock<usize> = LazyLock::new(|| {
std::env::var("MAX_FACTORY_DEPENDENCIES_SIZE_BYTES")
.ok()
.and_then(|value| value.parse::<usize>().ok())
.unwrap_or(100_000)
});

/// Batch factory deps on the basis of size.
///
/// For large factory_deps the VM can run out of gas. To avoid this case we batch factory_deps
/// on the basis of the max factory dependencies size and deploy all but the last batch
/// via empty transactions, with the last one deployed normally via create.
pub fn batch_factory_dependencies(mut factory_deps: Vec<Vec<u8>>) -> Vec<Vec<Vec<u8>>> {
let max_factory_deps_size_bytes = std::env::var("MAX_FACTORY_DEPENDENCIES_SIZE_BYTES")
.ok()
.and_then(|value| value.parse::<usize>().ok())
.unwrap_or(DEFAULT_MAX_FACTORY_DEPENDENCIES_SIZE_BYTES);
let factory_deps_count = factory_deps.len();
let factory_deps_sizes = factory_deps.iter().map(|dep| dep.len()).collect_vec();
let factory_deps_total_size = factory_deps_sizes.iter().sum::<usize>();
tracing::debug!(count=factory_deps_count, total=factory_deps_total_size, sizes=?factory_deps_sizes, max=max_factory_deps_size_bytes, "optimizing factory_deps");
tracing::debug!(count=factory_deps_count, total=factory_deps_total_size, sizes=?factory_deps_sizes, max=*MAX_FACTORY_DEPENDENCIES_SIZE_BYTES, "optimizing factory_deps");

let mut batches = vec![];
let mut current_batch = vec![];
Expand All @@ -785,7 +790,7 @@ pub fn batch_factory_dependencies(mut factory_deps: Vec<Vec<u8>>) -> Vec<Vec<Vec
for dep in factory_deps {
let len = dep.len();
let new_len = current_batch_len + len;
if new_len > max_factory_deps_size_bytes && !current_batch.is_empty() {
if new_len > *MAX_FACTORY_DEPENDENCIES_SIZE_BYTES && !current_batch.is_empty() {
batches.push(current_batch);
current_batch = vec![];
current_batch_len = 0;
Expand Down

0 comments on commit 18c971f

Please sign in to comment.