From 1bfaa39cafc78689375e84400af8af179f500bf5 Mon Sep 17 00:00:00 2001 From: clearloop <26088946+clearloop@users.noreply.github.com> Date: Tue, 7 Nov 2023 18:11:13 +0800 Subject: [PATCH] feat(wasm-builder): optimized used functions of metawasm --- utils/wasm-builder/src/optimize.rs | 19 ++++--------------- utils/wasm-builder/src/wasm_project.rs | 2 ++ 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/utils/wasm-builder/src/optimize.rs b/utils/wasm-builder/src/optimize.rs index 263eb95e6d7..43c15ce7ca5 100644 --- a/utils/wasm-builder/src/optimize.rs +++ b/utils/wasm-builder/src/optimize.rs @@ -6,7 +6,7 @@ use gear_core::code::{Code, TryNewCodeConfig}; use gear_wasm_instrument::{rules::CustomConstantCostRules, STACK_END_EXPORT_NAME}; use pwasm_utils::{ parity_wasm, - parity_wasm::elements::{Internal, Module, Section, Serialize}, + parity_wasm::elements::{Module, Section, Serialize}, }; #[cfg(not(feature = "wasm-opt"))] use std::process::Command; @@ -31,6 +31,8 @@ const OPTIMIZED_EXPORTS: [&str; 7] = [ STACK_END_EXPORT_NAME, ]; +const OPTIMIZED_META_EXPORTS: [&str; 1] = ["metadata"]; + /// Type of the output wasm. #[derive(PartialEq, Eq)] pub enum OptType { @@ -88,20 +90,7 @@ impl Optimizer { let exports = if ty == OptType::Opt { OPTIMIZED_EXPORTS.to_vec() } else { - self.module - .export_section() - .ok_or_else(|| anyhow::anyhow!("Export section not found"))? - .entries() - .iter() - .flat_map(|entry| { - if let Internal::Function(_) = entry.internal() { - let entry = entry.field(); - (!OPTIMIZED_EXPORTS.contains(&entry)).then_some(entry) - } else { - None - } - }) - .collect() + OPTIMIZED_META_EXPORTS.to_vec() }; pwasm_utils::optimize(&mut module, exports) diff --git a/utils/wasm-builder/src/wasm_project.rs b/utils/wasm-builder/src/wasm_project.rs index da77a64b9e0..9cddf5a8c31 100644 --- a/utils/wasm-builder/src/wasm_project.rs +++ b/utils/wasm-builder/src/wasm_project.rs @@ -286,6 +286,8 @@ extern "C" fn metahash() {{ )?; } + optimize::optimize_wasm(meta_wasm_path.clone(), meta_wasm_path.clone(), "4", false)?; + smart_fs::write( self.out_dir.join("wasm_binary.rs"), format!(