Skip to content

Commit

Permalink
fix unncessary heap allocations in engine
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Dec 12, 2024
1 parent 221ca6c commit 890188f
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 31 deletions.
8 changes: 3 additions & 5 deletions dan_layer/engine/src/runtime/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2345,9 +2345,9 @@ impl<TTemplateProvider: TemplateProvider<Template = LoadedTemplate>> RuntimeInte
Ok(InvokeResult::encode(&address)?)
}

fn publish_template(&self, template: &[u8]) -> Result<(), RuntimeError> {
fn publish_template(&self, template: Vec<u8>) -> Result<(), RuntimeError> {
self.tracker.write_with(|state| {
let binary_hash = template_hasher32().chain(template).result();
let binary_hash = template_hasher32().chain(&template).result();
let template_address = PublishedTemplateAddress::from_hash(
hasher32(EngineHashDomainLabel::TemplateAddress)
.chain(&self.transaction_signer_public_key)
Expand All @@ -2356,9 +2356,7 @@ impl<TTemplateProvider: TemplateProvider<Template = LoadedTemplate>> RuntimeInte
);
state.new_substate(
template_address,
SubstateValue::Template(PublishedTemplate {
binary: template.to_vec(),
}),
SubstateValue::Template(PublishedTemplate { binary: template }),
)?;
let scope_mut = state.current_call_scope_mut()?;
scope_mut.move_node_to_owned(&template_address.into())?;
Expand Down
2 changes: 1 addition & 1 deletion dan_layer/engine/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub trait RuntimeInterface: Send + Sync {
fn push_call_frame(&self, frame: PushCallFrame) -> Result<(), RuntimeError>;
fn pop_call_frame(&self) -> Result<(), RuntimeError>;

fn publish_template(&self, template: &[u8]) -> Result<(), RuntimeError>;
fn publish_template(&self, template: Vec<u8>) -> Result<(), RuntimeError>;
}

#[derive(Clone)]
Expand Down
2 changes: 1 addition & 1 deletion dan_layer/engine/src/transaction/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ impl<TTemplateProvider: TemplateProvider<Template = LoadedTemplate> + 'static> T
WasmModule::load_template_from_code(binary.as_slice())?;

// creating new substate
runtime.interface().publish_template(binary.as_slice())?;
runtime.interface().publish_template(binary)?;

Ok(InstructionResult::empty())
}
Expand Down
4 changes: 4 additions & 0 deletions dan_layer/engine/src/wasm/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ impl WasmModule {
&self.code
}

pub fn into_code(self) -> Vec<u8> {
self.code
}

fn create_engine() -> Engine {
const MEMORY_PAGE_LIMIT: Pages = Pages(32); // 2MiB = 32 * 65,536
let base = BaseTunables::for_target(&Target::default());
Expand Down
14 changes: 6 additions & 8 deletions dan_layer/engine/tests/publish_template.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::iter;

use rand::random;
use tari_dan_engine::wasm::compile::compile_template;
use tari_engine_types::{
Expand Down Expand Up @@ -29,7 +31,7 @@ fn publish_template_success() {
let result = test.execute_expect_success(
Transaction::builder()
.fee_transaction_pay_from_component(account_address, Amount(200_000))
.publish_template(template.code())
.publish_template(template.code().to_vec())
.sign(&account_key)
.build(),
vec![owner_proof],
Expand Down Expand Up @@ -62,7 +64,7 @@ fn publish_template_invalid_binary() {
let result = test.execute_expect_failure(
Transaction::builder()
.fee_transaction_pay_from_component(account_address, Amount(200_000))
.publish_template([1, 2, 3])
.publish_template(vec![1, 2, 3])
.sign(&account_key)
.build(),
vec![owner_proof],
Expand Down Expand Up @@ -103,10 +105,6 @@ fn publish_template_too_big_binary() {
}
}

fn generate_random_binary(size_in_bytes: u64) -> Vec<u8> {
let mut result = vec![];
for _ in 1..=size_in_bytes {
result.push(random::<u8>());
}
result
fn generate_random_binary(size_in_bytes: usize) -> Vec<u8> {
iter::repeat_with(random).take(size_in_bytes).collect()
}
57 changes: 45 additions & 12 deletions dan_layer/template_test_tooling/templates/faucet/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions dan_layer/transaction/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,8 @@ impl TransactionBuilder {
}

/// Publishing a WASM template.
pub fn publish_template<T: AsRef<[u8]>>(self, binary: T) -> Self {
self.add_instruction(Instruction::PublishTemplate {
binary: binary.as_ref().to_vec(),
})
pub fn publish_template(self, binary: Vec<u8>) -> Self {
self.add_instruction(Instruction::PublishTemplate { binary })
}

pub fn claim_burn(self, claim: ConfidentialClaim) -> Self {
Expand Down

0 comments on commit 890188f

Please sign in to comment.