Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4438 reduce the sizeamount of data sections in optwasm #4444

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ members = [
"examples/async-signal-entry",
"examples/async-tester",
"examples/autoreply",
"examples/big-data-section",
"examples/bls381",
"examples/calc-hash",
"examples/custom",
Expand Down Expand Up @@ -452,6 +453,7 @@ demo-async-reply-hook = { path = "examples/async-reply-hook" }
demo-async-recursion = { path = "examples/async-recursion" }
demo-async-signal-entry = { path = "examples/async-signal-entry" }
demo-async-tester = { path = "examples/async-tester" }
demo_big-data-section = {path = "examples/big-data-section"}
demo-bls381 = { path = "examples/bls381" }
demo-calc-hash = { path = "examples/calc-hash" }
demo-calc-hash-in-one-block = { path = "examples/calc-hash/in-one-block" }
Expand Down
30 changes: 30 additions & 0 deletions examples/big-data-section/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "demo-big-data-section"
version.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
rust-version.workspace = true

[lib]
name = "demo_big_data_section"

[lints]
workspace = true

[dependencies]
gstd = {workspace = true, features = ["debug"]}
parity-scale-codec.workspace = true

[dev-dependencies]
gtest.workspace = true

[build-dependencies]
gear-wasm-builder.workspace = true

[features]
debug = ["gstd/debug"]
default = ["std"]
std = []
21 changes: 21 additions & 0 deletions examples/big-data-section/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file is part of Gear.

// Copyright (C) 2021-2025 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

fn main() {
gear_wasm_builder::build();
}
396 changes: 396 additions & 0 deletions examples/big-data-section/src/constants.rs

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions examples/big-data-section/src/data_access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// This file is part of Gear.

// Copyright (C) 2025 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::constants::*;

#[derive(Debug)]
pub struct DataAccess {
array_index: u8,
value_index: usize,
}

impl DataAccess {
pub fn from_payload(payload: &[u8]) -> Result<Self, &'static str> {
if payload.len() < 2 {
return Err("Payload length must be at least 2 bytes");
}

Ok(Self {
array_index: payload[0],
value_index: payload[1..].iter().map(|&x| x as usize).sum::<usize>() % SIZE,
})
}

pub fn constant(&self) -> i128 {
match self.array_index {
1 => ARRAY_1[self.value_index],
2 => ARRAY_2[self.value_index],
3 => ARRAY_3[self.value_index],
4 => ARRAY_4[self.value_index],
5 => ARRAY_1[self.value_index],
_ => CONSTANT,
}
}
}
33 changes: 33 additions & 0 deletions examples/big-data-section/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This file is part of Gear.

// Copyright (C) 2025 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![no_std]

#[cfg(feature = "std")]
mod code {
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
}

#[cfg(feature = "std")]
pub use code::WASM_BINARY_OPT as WASM_BINARY;

#[cfg(not(feature = "std"))]
pub mod wasm;

pub mod constants;
pub mod data_access;
31 changes: 31 additions & 0 deletions examples/big-data-section/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This file is part of Gear.

// Copyright (C) 2025 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::data_access::DataAccess;
use gstd::{msg, prelude::*};

#[unsafe(no_mangle)]
extern "C" fn handle() {
let payload = msg::load_bytes().expect("Failed to load payload");

let value = DataAccess::from_payload(&payload)
.expect("Failed to decode incoming payload")
.constant();

msg::reply_bytes(value.to_be_bytes(), 0).expect("Failed to send reply");
}
56 changes: 56 additions & 0 deletions examples/big-data-section/tests/test_big_data_section.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#[cfg(test)]
mod tests {
use demo_big_data_section::data_access::DataAccess;
use gtest::{Log, Program, System};

const USER_ID: u64 = gtest::constants::DEFAULT_USER_ALICE;

// Testing random access to data section
#[test]
fn test_big_data_section() -> Result<(), &'static str> {
let sys = System::new();
sys.init_logger();

let prog = Program::from_file(
&sys,
"../../target/wasm32-unknown-unknown/debug/demo_big_data_section.opt.wasm",
);
sys.mint_to(gtest::constants::DEFAULT_USER_ALICE, 100000000000);

// Skipping program initialization
let _ = prog.send_bytes(USER_ID, b"");
let _ = sys.run_next_block();

let random_payload: Vec<Vec<u8>> = vec![
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you pls use here proptests to make sure its not coincidence? also run a few times locally with huge amount of repetitions

vec![1, 110, 115, 44],
vec![2, 244, 215, 99],
vec![4, 139, 72, 39],
vec![6, 20, 61, 59],
vec![4, 50, 249, 180],
vec![3, 195, 161, 132],
vec![10, 84, 39, 226],
vec![5, 125, 136, 188],
vec![3, 56, 246, 19],
vec![1, 49, 142, 82],
vec![4, 242, 66, 82],
vec![1, 110, 254, 189],
];

for payload in random_payload {
let expected_value = DataAccess::from_payload(&payload)?.constant();

let message_id = prog.send_bytes(USER_ID, payload);
let block_run_result = sys.run_next_block();

let log = Log::builder()
.source(prog.id())
.dest(USER_ID)
.payload_bytes(expected_value.to_be_bytes());

assert!(block_run_result.succeed.contains(&message_id));
assert!(block_run_result.contains(&log));
}

Ok(())
}
}
3 changes: 3 additions & 0 deletions utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,10 @@ extern "C" fn metahash() {{
optimizer
.insert_stack_end_export()
.unwrap_or_else(|err| log::info!("Cannot insert stack end export: {}", err));

optimizer.strip_custom_sections();
optimizer.join_data_sections();

fs::write(&opt_wasm_path, optimizer.optimize(OptType::Opt)?)
.context("Failed to write optimized WASM binary")?;
}
Expand Down
Loading
Loading