Skip to content

Commit

Permalink
perf: Skip some clones and hash calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed Feb 12, 2025
1 parent 0132aae commit b95c684
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 26 deletions.
15 changes: 7 additions & 8 deletions script/src/syscalls/debugger.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
use crate::types::{DebugContext, DebugPrinter, VmData};
use crate::{cost_model::transferred_byte_cycles, syscalls::DEBUG_PRINT_SYSCALL_NUMBER};
use ckb_types::packed::Byte32;
use ckb_vm::{
registers::{A0, A7},
Error as VMError, Memory, Register, SupportMachine, Syscalls,
};
use std::sync::Arc;

pub struct Debugger {
hash: Byte32,
pub struct Debugger<DL> {
vm_data: Arc<VmData<DL>>,
printer: DebugPrinter,
}

impl Debugger {
pub fn new<DL>(vm_data: &Arc<VmData<DL>>, debug_context: &DebugContext) -> Debugger {
impl<DL> Debugger<DL> {
pub fn new(vm_data: &Arc<VmData<DL>>, debug_context: &DebugContext) -> Debugger<DL> {
Debugger {
hash: vm_data.current_script_hash(),
vm_data: Arc::clone(vm_data),
printer: Arc::clone(&debug_context.debug_printer),
}
}
}

impl<Mac: SupportMachine> Syscalls<Mac> for Debugger {
impl<Mac: SupportMachine, DL: Send + Sync> Syscalls<Mac> for Debugger<DL> {
fn initialize(&mut self, _machine: &mut Mac) -> Result<(), VMError> {
Ok(())
}
Expand Down Expand Up @@ -50,7 +49,7 @@ impl<Mac: SupportMachine> Syscalls<Mac> for Debugger {
machine.add_cycles_no_checking(transferred_byte_cycles(buffer.len() as u64))?;
let s = String::from_utf8(buffer)
.map_err(|e| VMError::External(format!("String from buffer {e:?}")))?;
(self.printer)(&self.hash, s.as_str());
(self.printer)(self.vm_data.current_script_hash(), s.as_str());

Ok(true)
}
Expand Down
16 changes: 8 additions & 8 deletions script/src/syscalls/load_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ use crate::{
syscalls::{utils::store_data, LOAD_SCRIPT_SYSCALL_NUMBER, SUCCESS},
types::VmData,
};
use ckb_types::{packed::Script, prelude::*};
use ckb_types::prelude::*;
use ckb_vm::{
registers::{A0, A7},
Error as VMError, Register, SupportMachine, Syscalls,
};
use std::sync::Arc;

#[derive(Debug)]
pub struct LoadScript {
script: Script,
pub struct LoadScript<DL> {
vm_data: Arc<VmData<DL>>,
}

impl LoadScript {
pub fn new<DL>(vm_data: &Arc<VmData<DL>>) -> Self {
impl<DL> LoadScript<DL> {
pub fn new(vm_data: &Arc<VmData<DL>>) -> Self {
Self {
script: vm_data.sg_data.script_group.script.clone(),
vm_data: Arc::clone(vm_data),
}
}
}

impl<Mac: SupportMachine> Syscalls<Mac> for LoadScript {
impl<Mac: SupportMachine, DL: Send + Sync> Syscalls<Mac> for LoadScript<DL> {
fn initialize(&mut self, _machine: &mut Mac) -> Result<(), VMError> {
Ok(())
}
Expand All @@ -33,7 +33,7 @@ impl<Mac: SupportMachine> Syscalls<Mac> for LoadScript {
return Ok(false);
}

let data = self.script.as_slice();
let data = self.vm_data.sg_data.script_group.script.as_slice();
let wrote_size = store_data(machine, data)?;

machine.add_cycles_no_checking(transferred_byte_cycles(wrote_size))?;
Expand Down
15 changes: 7 additions & 8 deletions script/src/syscalls/load_script_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@ use crate::{
syscalls::{utils::store_data, LOAD_SCRIPT_HASH_SYSCALL_NUMBER, SUCCESS},
types::VmData,
};
use ckb_types::packed::Byte32;
use ckb_vm::{
registers::{A0, A7},
Error as VMError, Register, SupportMachine, Syscalls,
};
use std::sync::Arc;

#[derive(Debug)]
pub struct LoadScriptHash {
hash: Byte32,
pub struct LoadScriptHash<DL> {
vm_data: Arc<VmData<DL>>,
}

impl LoadScriptHash {
pub fn new<DL>(vm_data: &Arc<VmData<DL>>) -> LoadScriptHash {
impl<DL> LoadScriptHash<DL> {
pub fn new(vm_data: &Arc<VmData<DL>>) -> LoadScriptHash<DL> {
LoadScriptHash {
hash: vm_data.sg_data.script_group.script.calc_script_hash(),
vm_data: Arc::clone(vm_data),
}
}
}

impl<Mac: SupportMachine> Syscalls<Mac> for LoadScriptHash {
impl<Mac: SupportMachine, DL: Send + Sync> Syscalls<Mac> for LoadScriptHash<DL> {
fn initialize(&mut self, _machine: &mut Mac) -> Result<(), VMError> {
Ok(())
}
Expand All @@ -33,7 +32,7 @@ impl<Mac: SupportMachine> Syscalls<Mac> for LoadScriptHash {
return Ok(false);
}

let data = self.hash.as_reader().raw_data();
let data = self.vm_data.current_script_hash().as_reader().raw_data();
let wrote_size = store_data(machine, data)?;

machine.add_cycles_no_checking(transferred_byte_cycles(wrote_size))?;
Expand Down
2 changes: 2 additions & 0 deletions script/src/syscalls/tests/vm_latest/syscalls_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,7 @@ fn _test_load_current_script_hash(data: &[u8]) -> Result<(), TestCaseError> {
// Swap the internal script in VmData
let vm_data = {
let mut sg_data = vm_data.sg_data.as_ref().clone();
sg_data.script_hash = script.calc_script_hash();
sg_data.script_group.script = script;
Arc::new(VmData {
sg_data: Arc::new(sg_data),
Expand Down Expand Up @@ -1458,6 +1459,7 @@ fn _test_load_script(data: &[u8]) -> Result<(), TestCaseError> {
// Swap the internal script in VmData
let vm_data = {
let mut sg_data = vm_data.sg_data.as_ref().clone();
sg_data.script_hash = script.calc_script_hash();
sg_data.script_group.script = script.clone();
Arc::new(VmData {
sg_data: Arc::new(sg_data),
Expand Down
8 changes: 6 additions & 2 deletions script/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,12 +825,15 @@ pub struct SgData<DL> {
pub script_version: ScriptVersion,
/// Currently executed script group
pub script_group: ScriptGroup,
/// Currently executed script hash
pub script_hash: Byte32,
/// DataPieceId for the root program
pub program_data_piece_id: DataPieceId,
}

impl<DL> SgData<DL> {
pub fn new(tx_data: &Arc<TxData<DL>>, script_group: &ScriptGroup) -> Result<Self, ScriptError> {
let script_hash = script_group.script.calc_script_hash();
let script_version = tx_data.select_version(&script_group.script)?;
let dep_index = tx_data
.extract_referenced_dep_index(&script_group.script)?
Expand All @@ -839,6 +842,7 @@ impl<DL> SgData<DL> {
Ok(Self {
tx_data: Arc::clone(tx_data),
script_version,
script_hash,
script_group: script_group.clone(),
program_data_piece_id: DataPieceId::CellDep(dep_index),
})
Expand Down Expand Up @@ -946,8 +950,8 @@ impl<DL> VmData<DL> {
&self.sg_data.tx_data.outputs
}

pub fn current_script_hash(&self) -> Byte32 {
self.sg_data.script_group.script.calc_script_hash()
pub fn current_script_hash(&self) -> &Byte32 {
&self.sg_data.script_hash
}
}

Expand Down

0 comments on commit b95c684

Please sign in to comment.