Skip to content

Commit

Permalink
[loader-v2] Remove extra hash calculation from runtime environment
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemitenkov committed Nov 13, 2024
1 parent cb4dd96 commit de7b721
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
4 changes: 1 addition & 3 deletions aptos-move/block-executor/src/code_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ impl<'a, T: Transaction, S: TStateView<Key = T::Key>, X: Executable> ModuleCodeB
.map_err(|err| err.finish(Location::Undefined))?
.map(|state_value| {
let extension = Arc::new(AptosModuleExtension::new(state_value));

// TODO(loader_v2): This recomputes module hash twice, we should avoid it.
let (compiled_module, _, _) = self
let compiled_module = self
.runtime_environment()
.deserialize_into_compiled_module(extension.bytes())?;
Ok(ModuleCode::from_deserialized(compiled_module, extension))
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/block-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ where

// Since we have successfully serialized the module when converting into this transaction
// write, the deserialization should never fail.
let (compiled_module, _, _) = runtime_environment
let compiled_module = runtime_environment
.deserialize_into_compiled_module(state_value.bytes())
.map_err(|err| {
let msg = format!("Failed to construct the module from state value: {:?}", err);
Expand Down
17 changes: 5 additions & 12 deletions third_party/move/move-vm/runtime/src/storage/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use move_core_types::{
};
#[cfg(any(test, feature = "testing"))]
use move_vm_types::loaded_data::runtime_types::{StructIdentifier, StructNameIndex};
use move_vm_types::sha3_256;
use std::sync::Arc;

/// [MoveVM] runtime environment encapsulating different configurations. Shared between the VM and
Expand Down Expand Up @@ -192,21 +191,15 @@ impl RuntimeEnvironment {
result.map_err(|e| e.finish(Location::Undefined))
}

/// Deserializes bytes into a compiled module, also returning its size and hash.
pub fn deserialize_into_compiled_module(
&self,
bytes: &Bytes,
) -> VMResult<(CompiledModule, usize, [u8; 32])> {
let compiled_module =
CompiledModule::deserialize_with_config(bytes, &self.vm_config().deserializer_config)
.map_err(|err| {
/// Deserializes bytes into a compiled module.
pub fn deserialize_into_compiled_module(&self, bytes: &Bytes) -> VMResult<CompiledModule> {
CompiledModule::deserialize_with_config(bytes, &self.vm_config().deserializer_config)
.map_err(|err| {
let msg = format!("Deserialization error: {:?}", err);
PartialVMError::new(StatusCode::CODE_DESERIALIZATION_ERROR)
.with_message(msg)
.finish(Location::Undefined)
})?;

Ok((compiled_module, bytes.len(), sha3_256(bytes)))
})
}

/// Deserializes bytes into a compiled script.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ use move_core_types::{
account_address::AccountAddress, identifier::IdentStr, language_storage::ModuleId,
metadata::Metadata,
};
use move_vm_types::code::{
ambassador_impl_ModuleCache, ModuleBytesStorage, ModuleCache, ModuleCode, ModuleCodeBuilder,
UnsyncModuleCache, WithBytes, WithHash,
use move_vm_types::{
code::{
ambassador_impl_ModuleCache, ModuleBytesStorage, ModuleCache, ModuleCode,
ModuleCodeBuilder, UnsyncModuleCache, WithBytes, WithHash,
},
sha3_256,
};
use std::{borrow::Borrow, ops::Deref, sync::Arc};

Expand Down Expand Up @@ -137,9 +140,10 @@ impl<'s, S: ModuleBytesStorage, E: WithRuntimeEnvironment> ModuleCodeBuilder
Some(bytes) => bytes,
None => return Ok(None),
};
let (compiled_module, _, hash) = self
let compiled_module = self
.runtime_environment()
.deserialize_into_compiled_module(&bytes)?;
let hash = sha3_256(&bytes);
let extension = Arc::new(BytesWithHash::new(bytes, hash));
let module = ModuleCode::from_deserialized(compiled_module, extension);
Ok(Some(module))
Expand Down

0 comments on commit de7b721

Please sign in to comment.