-
Notifications
You must be signed in to change notification settings - Fork 105
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
ecol-master
wants to merge
5
commits into
master
Choose a base branch
from
4438-reduce-the-sizeamount-of-data-sections-in-optwasm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7139ac8
fix(wasm-builder): problem with a large number of data sections .opt.…
ecol-master 743580e
feat: added example with large data section
ecol-master f7b297c
chore: refactoring join data sections optimization
ecol-master b6288b2
chore: reorganization of methods to optimize data section
ecol-master c51047a
feat: add prop-testing
ecol-master File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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![ | ||
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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