diff --git a/Cargo.lock b/Cargo.lock index f29fabb853d..83c28e6451a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3336,6 +3336,17 @@ dependencies = [ "parity-scale-codec", ] +[[package]] +name = "demo-big-data-section" +version = "1.7.0" +dependencies = [ + "gear-wasm-builder", + "gstd", + "gtest", + "parity-scale-codec", + "proptest", +] + [[package]] name = "demo-bls381" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0290b9b28b9..56a91c69d8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", @@ -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" } diff --git a/examples/big-data-section/Cargo.toml b/examples/big-data-section/Cargo.toml new file mode 100644 index 00000000000..c7a23d9401f --- /dev/null +++ b/examples/big-data-section/Cargo.toml @@ -0,0 +1,28 @@ +[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 + +[lints] +workspace = true + +[dependencies] +gstd = { workspace = true, features = ["debug"] } +parity-scale-codec.workspace = true + +[dev-dependencies] +gtest.workspace = true +proptest = { workspace = true } + +[build-dependencies] +gear-wasm-builder.workspace = true + +[features] +debug = ["gstd/debug"] +default = ["std"] +std = [] \ No newline at end of file diff --git a/examples/big-data-section/build.rs b/examples/big-data-section/build.rs new file mode 100644 index 00000000000..1024b4832a6 --- /dev/null +++ b/examples/big-data-section/build.rs @@ -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 . + +fn main() { + gear_wasm_builder::build(); +} diff --git a/examples/big-data-section/src/constants.rs b/examples/big-data-section/src/constants.rs new file mode 100644 index 00000000000..b8b3e3554da --- /dev/null +++ b/examples/big-data-section/src/constants.rs @@ -0,0 +1,396 @@ +// 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 . + +pub const SIZE: usize = 576; + +pub const ARRAY_1: [i128; SIZE] = [ + 185259344, 275845248, -46608948, -114210336, -222830608, -673885184, 402875744, -31931698, + 262381712, -396806944, 14803851, 221165056, -247776496, 145953728, 304126464, 173752304, + 315401152, 349545184, 239468800, 224126304, -151424496, 147048016, -15596633, 519028096, + 333166656, 33628552, 65969752, -195655040, -381632800, -225674512, 317270432, -362832160, + 59999196, -67498128, -209208672, 268933664, 253052864, -649277824, 482212416, 182985472, + 101074456, 849838080, 232720912, 50304600, 273787488, -42140560, 144824656, 788323712, + -79913784, 213462496, 179724240, 130679392, -742983296, 326448896, -344171136, -350555488, + -30945812, 86157312, -96127488, -320283520, -698658816, 456484544, 416750112, 341300832, + -328794208, -130746360, -256572416, -33985988, -144970656, -276737088, 182383456, -153568800, + -233734304, 396903200, 296106240, 88425376, 57666832, 133193280, 82655448, 488061024, 13411613, + -25852340, -7741807, -186196800, -712415360, -465579264, -431435008, -262139008, -246213872, + -22554810, -190250368, -135616352, -197961536, -211752864, -129892752, -274241984, 147947072, + -504534880, -393771840, -342419008, -368598400, -175208096, 8048182, 310791776, 134509168, + -497454720, 428577184, -200586176, -222151904, 230144816, -40710096, -696302784, 457704672, + 189981472, -281208160, 50225068, -165158080, -163002864, -154362016, 197692832, 540918016, + -177361040, -182371584, 13411851, -173728864, 75603432, 337545920, -242898992, 556080256, + 14683870, 297097024, -2994721, -286580224, -288687520, -70240752, 105447144, -99248056, + -452354528, -171912640, 151575120, 126017248, -309147136, 322532096, 46244272, 75535064, + 269369344, 71530560, 316826752, -12654291, 390725600, 280997568, 656924928, 572145536, + 329463360, 329505632, 36992216, -278345728, 93301912, -10032673, -74071040, -63154496, + 227079920, -15677310, 348370880, -231867168, -218361696, -95352952, -59261708, -74568672, + 431956640, 210973312, -24057004, -12685031, 254540336, -77736432, 215136912, -215227952, + 242403584, -4059858, -240722784, -374875232, 34201644, -135057664, -83427288, -328351680, + -159456688, 203876304, -143020848, -533395360, 8248299, 367404608, -136988688, -1258876, + 97568656, -241977648, -126017800, 247630384, -365683456, 53167904, -108469000, 251701248, + -123093736, 39585332, -154665888, -24703136, 7353051, -474126784, -113866144, -35694156, + 139195392, -468723296, 428311936, -380354976, -35491500, -62006780, -256468384, -41569924, + 360877280, -165529200, -740558848, -415832608, -52416796, -101624464, -287548640, 136351456, + -120774896, -427036864, 136166544, -30462714, -672141568, -302596032, -280948000, -339320288, + -320267584, 27822668, -322737600, -203237600, -267306144, -190168784, -110932816, 373300416, + -188565680, -306524224, 462912736, -549809344, -409228288, 686058, 435072864, 386936160, + -362037120, 241964848, -193144640, 9445155, -228799360, -33253348, -74785264, 256273472, + 48672468, -113725904, -62496964, -181029904, -593157504, 108819440, 327648512, -257753232, + -242533968, 345835936, -303105472, -633719744, 151319072, -170836864, -228132976, 41993528, + 109792840, 191082432, 299999424, 346609856, -11954250, 18103020, 365500704, 450601632, + -293279840, 410115872, -428409184, 10806831, -357557952, 167146912, -299529152, -169418480, + 325295584, -173299808, -119321512, -429315232, -82739520, 61903656, -142744544, 112576504, + -400224128, -356198048, -283269408, -81987232, -8439711, -309324096, -154002640, -57901380, + -200541312, 179866352, -513716288, 126892816, 226414656, -108391816, -181828272, 152568512, + -261004944, -321979936, -189406352, -104448352, 107962616, 4475896, -59804604, 95001136, + -134104416, 95748888, 161772992, 190884784, 335523936, -148432896, -250266128, -319213664, + -444774048, -597578944, -232546576, 13662133, -233548368, 27113340, -324866272, 189875312, + -237341808, 251040240, -66414108, -8329201, -93802792, -58177648, 97156328, -99594016, + -8800761, -26545964, -76714184, 115936992, -216734256, -532366240, 177291888, -168901472, + -20438246, 89076448, -54663484, 391083296, -347665184, 138460864, -402363840, -191675360, + 505309664, -174261408, 154287808, 439322752, -281694208, -78299088, -476966976, 454544192, + -136807456, 321403584, -645280128, 290781504, 370561728, -120226296, -191340736, -274505152, + 7600117, -296208672, -123001040, 353264192, -62220540, -272715968, 530681696, -135848832, + -20435758, -220753632, -66461336, 6293480, 110611736, -3750695, 171207552, 287082912, + -47161992, -217336272, -245879312, -299311264, -261571904, -347556448, -13899221, -190222192, + -273251936, -352062080, -17340484, 112228848, 143088416, -148114672, -51042608, -227284512, + 33873416, -6277949, 150919840, 158401616, -263917792, 109301200, 153021504, -33473522, + -152780752, -28138790, -174193680, 362017184, 107660496, 258988480, -206071440, 228356960, + -378499232, -367965472, -219616704, 103185424, 345019712, 190403120, -471843520, 62378788, + -215285680, -326261632, -69594480, 153489952, 465168288, -104403624, 327522048, 5634518, + -45592820, 157599152, 453765216, 208146128, -21353740, -287796480, 17141270, 299208384, + -110968392, 405470688, -78860656, 444913920, -126881808, 366696096, 92370736, 256593344, + 270020960, -383873120, 281649664, 499431808, -127975272, -319063840, -389828128, 674706624, + 327416160, 223183920, -323446016, 343003712, 580790720, -198993664, 7789194, 10385831, + 322121472, 264274000, 77480616, 311859616, -187917968, -176929408, 476268192, 34199576, + -93157904, -199693488, 193931904, 211032480, 307567168, -83165488, 48403276, 449228096, + -123644920, -38145148, -178283616, -264231552, 59010824, -20460946, -156295680, -236215488, + 148127856, 274448864, -374092128, -184211120, 117855976, -142691456, -52885936, -475083168, + -8107543, 274953120, -153812512, 95527568, -8515996, -77932360, 385550176, 283534400, + -64921928, -358301280, 458816544, 53863052, 108338360, -32215112, -402484192, 529489280, + 227225024, -632707456, 126525992, -77616552, 107321320, 42974872, 41306264, -304102848, + -210302704, 105640584, -15581995, 709008832, -51892404, -89874640, -245362000, -434606464, + 128471952, 135540816, 130442344, 441018336, -52002892, -94034104, 486049568, -15983755, + -272744960, 257284336, 256398656, -298914944, 284668992, 176771280, -214778560, 586679232, + -27826942, -18603918, 320549984, -302136000, 187849440, 78599184, 177303904, 391204064, + 245931520, 206651840, -568627904, 660635648, -745255104, -586737152, -356867136, -252797856, + -70632712, 224088, 152232560, -87407952, 214905152, -127812784, 491693312, +]; + +pub const ARRAY_2: [i128; SIZE] = [ + 288872256, 176901184, -33054390, 75897648, -16939240, -96398648, 118886064, 221654896, + 247921200, 378518560, 317700224, 29914160, 15309910, 80626912, 80177312, 73825496, 218224416, + 230689264, -175716544, 50749656, -424322400, 16576973, 71814088, 118382384, 121220632, + 238794832, -101961768, 68009592, 98220560, 365838688, 213465264, 441054720, -326987968, + 107701168, -185931184, 63026920, -285402400, 156097376, -32614682, 196221968, -301932416, + 153344912, -87901160, 58655520, 199611184, 880203, 48064404, -585273536, -291574624, + -303698848, 58959536, 138315952, -273401792, -203238736, -508152384, -316097856, -239454624, + 27061962, -86723768, -265413344, -134352736, -99330640, -82150464, -99673584, -260922416, + 92215576, -688179008, -84923688, -460267680, 9652966, 144236800, -217773312, -398144768, + -481144736, 144051952, -243574592, -660300160, -313697472, 133118696, -827963904, -381314784, + -114012632, 15576880, 185857584, -494267360, -344687680, -451522400, -242309040, 43677252, + -20079232, 114865432, -470224448, 61948076, -422077664, -302564800, -525972480, 389374912, + 64714984, 430201824, 125427168, 280279232, -54732576, 445024544, -366930688, 53749348, + -11738758, 447807552, 69285688, -181005792, -379955360, 184989904, 673995968, 315896064, + 401868128, -98846976, -312256992, 338616640, 283203680, -160925264, -38731576, -21628514, + -263067824, 60631268, -214777440, -20315556, 242091072, 159167520, 433718624, 39679636, + -121846664, 112310328, 52664044, 163255072, 179391520, -20232244, -57370484, 177320512, + 317720896, 205190928, 238609840, -78368208, 113280728, 91797688, 675118144, 54404964, + -78371432, 221314432, 306519840, -181511168, 261621872, -637716288, -199518000, -505504224, + -554708928, 67821376, 294012000, -362193312, 57764368, -86109880, -107493592, -354184832, + -499437280, -206058528, 29484994, -520724064, 222806160, -205932064, -82538848, -5789785, + -308398656, 318141312, 28151190, -311614272, 252632048, -134308672, -726637056, -183336656, + -201700240, -282201920, 280290016, -343404320, -131642408, -363006016, -147021744, -52775244, + -296851488, -34522028, -209874992, -229548640, -282603552, -180131776, -708631616, 254838064, + -163751088, 220289648, -67374720, 193490240, -652247744, -101529288, 200351040, -110564624, + -210796128, 363099840, -75932096, 277078624, 18116408, 405564832, -546635968, 219154368, + 126226176, -232646384, 328136544, -448269984, 379856320, -83106944, 400489856, -138012880, + -623076992, -12100082, -370672864, 128489200, 122944552, 186418480, -102300768, 228112032, + 154002720, 425682976, 223773680, 45249652, 9437093, -287605760, -69750640, 48358308, 540612032, + 496161920, 53161328, -235362512, -5914759, 37770224, 384073792, 332036000, -156154576, + -159243152, 152338384, -523961792, 600243520, -247784800, -117111384, 127899280, -513152000, + 312051136, -411862912, 39473256, 55227696, -204909536, 769302656, -258888400, -470265056, + -207995792, -239828736, -294279136, 25420562, 74417344, 329476384, -396537408, 215603712, + 545218624, -23628824, -177850160, -2861616, -193298288, -462928736, -290209152, -165458496, + 172290992, 490994784, 29715656, -478270272, -58006196, -185760160, -434667680, -236673248, + 327519776, 78004216, -8266036, -206823472, 89815864, -538574912, -161260704, -272389536, + -238283008, 39607132, 56446932, -123747664, 370547936, -1130717, 70712064, -413709184, 352448, + -224619664, -222278992, 209042288, 136330480, -692233792, -10914845, -250590816, 142651152, + 166540624, -719989056, -338566112, -799070976, -369973312, -498524576, -305237440, 433907680, + -210639488, -537100992, 85243208, -321344384, -480581536, 47783188, -385394592, -474255648, + 260286720, 30906422, -223647040, -89973480, -158713344, -454587392, 258989056, 598714368, + -177531520, -291302944, -140020208, 176769248, -117161968, -517442848, 310901600, 102802224, + 89144408, -21632932, -338699680, -741147904, -223355536, -458395808, -330528256, 349860032, + 20701210, -60405196, -294062880, 65137956, -195013248, -141214944, 33653244, 277156160, + -2455438, -118002552, -218637584, 230399472, 415901792, 144866096, 192824480, 74050264, + 199647264, -122960872, 39403400, -140150784, 448054048, -389670400, -373249888, -175444944, + 424758336, -585153280, 253651424, 567496832, 295738688, 114722256, 34267872, 339247296, + -115818568, 116704184, 342952768, 203196896, -235207504, -226367856, 154429296, -688974208, + -279952000, -37862760, -497212096, -273061312, 228346016, -286398848, -107114288, 129691104, + -39304732, -639020416, -55793880, 100293008, -392882720, -456434272, 53906188, 115423480, + -61023756, -49452412, -638041984, -707360768, -69566120, -543943488, -618716096, -25158500, + -310992608, -386911424, -499394784, -301549728, -202038096, -508178176, -61977072, -694555136, + 108379912, 13882482, 79484512, -277817088, 73003008, -198864368, 26087044, 165716448, + -147409328, 82219160, 4282795, 51912844, -569323200, -403206784, -179159984, -83766080, + 353988704, 2567320, -298469600, -542701888, -422747680, -458150912, -137980336, -18784102, + 192535104, -365013696, -163354816, 129129752, -272220512, 45935828, 285816864, -118413072, + 53693988, -67212120, 126273776, 490607296, -167312656, 291231776, -55735676, -232898, + -10148337, 424219776, -183453312, -12379551, 461812800, -611952192, 304949056, -27607322, + 555137536, -221313280, 184030240, -150531920, -430360128, 124285128, 5564783, -126986440, + -288731072, 123730488, -161085872, 104632768, 283545376, -49944748, 138312240, -109729848, + 135316640, 165323120, -296380736, -392346112, 275176096, 81496256, -83377160, 35696460, + -79764416, 165012240, -221866608, -204580400, -60152668, -167097744, -171642128, 249098064, + 24711516, 164546784, -510868032, -776786048, -401791968, -594947584, 80036288, 204138928, + -43398784, -139296352, -102752104, -216411392, -316911648, -431256800, -398486496, 21201568, + 147825520, 27968648, -282685440, -341569024, -150160688, -55973592, -15557964, 666724672, + 387477312, -221742960, 233717008, -83492656, -94803568, -254919664, -142300992, 333800032, + 21922374, -266925472, 158797472, -178071664, -333772576, -391150656, 373312192, -149633136, + -317777088, 219964608, -78530720, 315843456, -42740712, -236157776, -307901056, -72472224, + 2884609, 160114800, -123920632, -129023016, 92336104, -98004992, 466065312, 480221184, + 409094752, 279022816, -3340142, -328232480, 397757088, -252599936, -337976000, 114786184, + 300907936, -479123808, 379039360, 368747040, -410989248, 217811920, 366093152, 288789984, + -217175664, 657047744, 5677736, -15827627, 596849920, +]; + +pub const ARRAY_3: [i128; SIZE] = [ + -142346208, 370499168, -300695520, -48801300, -610282176, -176222672, 248691728, -147282640, + -227294112, -733976256, 51868196, 297267136, 112394112, -100853648, -151154368, -220678432, + 223882544, 379889984, -95206280, 218624896, -568852864, -107944528, 497351232, 420698656, + 360580096, -19598988, -287459936, -270530240, -179371456, 160780688, 199897392, -456591680, + -420767488, 37241340, 181428192, 385258688, 24866484, -252454304, -196741792, 42493948, + 180223344, 561971904, -108503136, 235964416, -3888038, -36349440, -23717476, 587977472, + -400254016, 39810844, 907469, -453187584, 113368064, 119565072, -66530520, -330494688, + -445503072, 188795648, -154973344, -268735488, -388793472, 410562976, 335605440, 157738240, + 238080224, 275438208, -52712040, -211667824, 156551040, -495485920, -357106656, 76638792, + -331126720, 378860192, -105084752, 584292224, -208689360, 635998016, -214695008, 720789568, + -348793760, -4617931, -144780176, 6293704, 40938488, -424899616, 60631296, -210551904, + -162649904, -17232284, -76131008, 166774608, 319723520, 194363376, 183646704, 79155008, + 207036048, -80588600, -101676352, -360018368, -237996976, -46179060, 7836504, -61661384, + 94745344, -526893920, -208287200, -153087424, 113191024, -120080360, -34172692, -247450000, + 276706848, 92732616, -60333548, 137346448, 59879652, -216855840, 125763696, -19572064, + 69536456, 368711680, 94585376, -182395392, 266272752, -478273024, 159206368, -463653280, + -95722296, -46709760, 62747244, -96599344, -273476992, -371002464, -312014656, 76340824, + -13692116, -347870016, 575925376, 214383776, -234671936, 194241520, -199529952, -160979648, + 268384688, 147356688, 376011200, 288886560, 103875112, -182103856, -251767424, -39038276, + 334111648, 182613456, -115214584, -219417968, -45144040, 350440448, 440652992, -208730944, + -48543704, -161673776, 149394576, 362869472, 306495264, -310010976, -83119040, 103424600, + 215041792, 478012192, 73952456, -273565728, 188069920, 149897360, 407116576, 417144768, + -132526368, 55560036, -96732472, 43241904, 38793932, 249451232, -386344448, -554845120, + -150652064, 249644816, 104180912, -122963608, 73696536, 165115968, -102857368, 439910272, + -26092526, 383434464, -323828032, 224525088, 12135141, -323263072, -21104362, 122535792, + -95065200, -41727636, -178448320, -501034080, 266349328, 136866112, -156969200, -199899696, + 5955719, -256253264, 15109000, 106312904, 86991512, -93336640, -474107680, -122719592, + 11581858, -239709424, -125870424, -147558608, 272441056, -376550560, -103650912, -237598304, + 1650973, -246738432, -363063680, -192011200, -141039328, -125240816, -20629078, 268110240, + 344786432, -495075040, 370029280, 153092512, -308773632, -247497008, 9915378, -847911040, + 258417696, -155047120, 55056236, -165055552, -562346240, -637125248, 132319848, 144154848, + -243448176, 89963384, -422793344, -484995072, -63867148, -330036576, 433174432, -505706240, + -413543744, 182264912, -414890368, -74882432, -138484912, -130425800, -120970720, 231666016, + -7796854, -306848896, -216913072, 58867780, -35243008, -302864096, -100402240, 8864765, + 155694240, 107743552, -140571984, -235523168, -262332496, 279610816, -116346056, -421949792, + 163378512, 201901424, -212435072, 86075536, -45706756, 1838202, 308524928, 144052848, + 337748384, 375635904, 346981408, 234363040, 32734744, 129737960, 150482640, 281959808, + 413209216, -19591414, -211240400, -263028592, -780184576, 383717024, -253231856, 38846228, + 118912632, -204221264, -114905616, 132797288, -217912944, 343643968, -310480960, -22236744, + -2230275, 128875560, 142461168, -236115888, -353460672, 50806556, -90756616, 396167136, + 139976976, 49343220, 104765616, -169768528, -3985143, 228537152, 101847640, -19650058, + 234595456, 41782748, 218695760, 204647216, -794408832, 306064032, -236241600, 264801184, + -250604256, 206603472, 78842536, 530616544, -315537632, -447924192, -138970224, 417184192, + 120195936, 358817056, 400526528, -125400584, -459691104, -141351328, -222124944, 63980680, + 38880020, -227433296, -208779440, -1525914, -389385984, 259641728, 57067364, -164584800, + -357475744, 187288272, 291720800, 486767136, -73710328, 454218368, -1562134, -114924496, + -79246912, 158066480, 453094144, 226727040, -429195936, -343838528, -386269824, 190860896, + 456092032, -211597968, 140629936, -58502716, -128261312, -473235360, 38158712, -108658392, + -15372098, -131631464, 279698752, -15832630, -300688928, 166087264, -197871664, 122719784, + 548060992, -663305408, 378761728, -435538720, -389736608, 88892448, 41286580, -472736992, + 6953150, -170798256, -105427752, 248844240, 62601608, -74841416, -306548384, 314318592, + 54533084, -86212360, -62299748, 15235858, -617841664, -372374080, -146128672, 355755424, + 118051720, 293685216, 110357016, 284053984, -136942928, 349504896, -116271368, 255138416, + 142333840, -383180928, -12631715, -307248576, -369954368, -44754536, -234625872, -342844128, + 205785792, -236852352, -114578016, -226054544, -358511232, 232486384, -325061600, -18517654, + -116122256, -335205600, -140786048, -6084959, -361122560, -343817472, -65702972, 24745736, + -30054438, 346982912, -42883332, 245873216, 159856976, -59322804, 281452288, -111122928, + 10115360, 213289136, 320242048, 56010648, -333856256, 311703904, -201011856, 434185248, + 74585576, -297865696, 227362816, -172029728, 41901556, -147549280, -71356192, 190846176, + -334061376, -127108160, -13881956, -17934650, 8327250, -175895328, 64092488, 11180150, + 94505120, -144399648, -177809104, -47244264, 115959184, 12663527, -170631008, 291502080, + 138154016, -269074720, 53910580, -425939296, -482076576, -557223296, -119491840, -306755840, + -31165712, -41816332, -394326976, 296462912, 214993872, -596322176, -327066336, -258251296, + 201177424, -246916912, 52188568, -89614824, -329553152, 140847840, -165309664, -408545984, + 21588854, 172218752, -58671572, 68303168, 177875008, -105693728, 446049376, -72809616, + 485313024, -265845424, 273110880, -531618880, -174913680, -354524608, 344340960, -477276384, + -34480228, -204972912, 31865560, 215958128, -33301890, 72363648, 40560444, 520483744, + 276653088, -542769984, -604075840, 149931264, 304423168, -217864256, -69559712, 168809312, + 213620048, 590299392, 602011328, 72760800, 154617504, -183877856, 641851520, -164428576, + 580283008, 212010480, 84977816, 127858712, -320271616, -215970576, -166179552, -100544920, + 615751296, -112587040, -115804304, 269751136, -55812084, 666634368, 203061344, 333296960, + -140660880, 301630080, -178678768, -226390656, 182357664, 88910608, 384810560, 92623360, +]; + +pub const ARRAY_4: [i128; SIZE] = [ + 127035448, 175800880, 281497536, -411941696, -264164080, -127694232, -45993100, 110923856, + 208558480, -115267088, 704105024, -394156608, -72113160, -196975088, -296649472, 258438064, + 77190560, -399469152, 32350040, 358328736, -523654144, 176738608, -590895424, 101327368, + -38921968, -28334198, 7058896, -209140208, -127765736, 7311183, 225270576, -211937024, + -420455712, 174436144, 242318992, -252048688, -479570016, -144796160, -45444004, -133723592, + 131915824, 288047328, 445757664, -365277408, 222786400, -292456032, 132004480, 16135940, + -233582288, -374484192, -141195376, 986802624, -780243200, -264348960, 407313952, 170913488, + -228384672, -22726678, 152216384, 127952592, 186925792, 463619232, -152702368, 192807840, + -191518080, 334951008, 205051936, -147308624, -67150016, 97064704, -41490148, 114929904, + 302218624, 404702880, 665118400, 18796058, 517905024, -110587256, -342727520, 488700256, + -28430586, 210217072, -624388928, 926040896, -591654976, 87983144, 225488112, -77603352, + 181253760, -297274816, -255007952, 218410800, 518976640, -268833792, -306650080, 147832576, + -261579840, 312590368, 379895872, 239279984, -726835648, -174617088, 293776736, -21657498, + -25575552, -240416624, 430961824, 114050552, 360426912, -379094976, -72823480, 149508656, + -468734432, -48128608, 136305024, 1018973376, -409616032, 164492528, -216742960, 233773680, + -464665568, -324696512, -124491656, 17075332, -640150144, 31053562, -198882176, -163147808, + -69681840, 24709148, 261711504, 205023904, 44082328, 102049944, -349655648, 338487136, + -249138944, 165197776, 636199104, -192810064, 515022720, -54892552, -267944304, 146754464, + -330387584, -278442880, -434854368, 472069984, -626530624, 122371088, 296007520, -277172960, + -361591744, 65485084, 53911020, 305993440, 135899648, 227196096, 194683408, -48348192, + -496362688, -148464592, 99542888, -164461520, -357384992, 228694560, 210119392, 586458368, + -203697840, -94649672, 392422688, -283373888, 686132608, -366901632, -211105280, 167521904, + -325632864, -519638144, -131494648, 1088629248, -198665072, 178327408, 299500480, -162591088, + 24351948, -279264480, -174249056, 151956336, -263512880, 206384240, -180853200, -256865808, + 51479832, 254732528, 345698720, -331017408, 162852272, 250261024, 298571552, -53903836, + 332612832, 52664796, 273553440, -66406160, 515259232, 183928624, 312200608, 143201008, + -213490480, 76265488, -480184448, 378068864, -756118912, 84117048, 190064032, -96641200, + -490705312, 141126016, 39601292, -120415816, -680528960, 193226704, -252341120, 29418230, + -117000648, 145997776, 80117424, -563609024, -62262140, 196627888, -142306448, -206754272, + -229997680, -330204480, 550456320, -90013704, 689411136, -49908960, -233673888, 177428240, + 45794456, -46763204, -112131832, 590128704, -363185600, -112057320, -195109680, 71244672, + -190788368, 134541616, -86260768, -38043648, 183274608, 178830032, -167130992, 97954192, + -92731760, -5644242, -139018992, -208032192, -103912400, 84232424, -238301920, 366373888, + -228921760, -70913896, 341710816, 46490432, 601084480, -36735992, -215139472, 153231104, + -105456384, -404656128, -167435648, 1053991040, -781715776, -146237184, 277285024, 304159136, + -174131856, 179870208, -248963152, 313521632, -233708128, 299800256, -376404032, 178163984, + 185259344, 275845248, -46608948, -114210336, -222830608, -673885184, 402875744, -31931698, + 262381712, -396806944, 14803851, 221165056, -247776496, 145953728, 304126464, 173752304, + 315401152, 349545184, 239468800, 224126304, -151424496, 147048016, -15596633, 519028096, + 333166656, 33628552, 65969752, -195655040, -381632800, -225674512, 317270432, -362832160, + 59999196, -67498128, -209208672, 268933664, 253052864, -649277824, 482212416, 182985472, + 101074456, 849838080, 232720912, 50304600, 273787488, -42140560, 144824656, 788323712, + -79913784, 213462496, 179724240, 130679392, -742983296, 326448896, -344171136, -350555488, + -30945812, 86157312, -96127488, -320283520, -698658816, 456484544, 416750112, 341300832, + -328794208, -130746360, -256572416, -33985988, -144970656, -276737088, 182383456, -153568800, + -233734304, 396903200, 296106240, 88425376, 57666832, 133193280, 82655448, 488061024, 13411613, + -25852340, -7741807, -186196800, -712415360, -465579264, -431435008, -262139008, -246213872, + -22554810, -190250368, -135616352, -197961536, -211752864, -129892752, -274241984, 147947072, + -504534880, -393771840, -342419008, -368598400, -175208096, 8048182, 310791776, 134509168, + -497454720, 428577184, -200586176, -222151904, 230144816, -40710096, -696302784, 457704672, + 189981472, -281208160, 50225068, -165158080, -163002864, -154362016, 197692832, 540918016, + -177361040, -182371584, 13411851, -173728864, 75603432, 337545920, -242898992, 556080256, + 14683870, 297097024, -2994721, -286580224, -288687520, -70240752, 105447144, -99248056, + -452354528, -171912640, 151575120, 126017248, -309147136, 322532096, 46244272, 75535064, + 269369344, 71530560, 316826752, -12654291, 390725600, 280997568, 656924928, 572145536, + 329463360, 329505632, 36992216, -278345728, 93301912, -10032673, -74071040, -63154496, + 227079920, -15677310, 348370880, -231867168, -218361696, -95352952, -59261708, -74568672, + 431956640, 210973312, -24057004, -12685031, 254540336, -77736432, 215136912, -215227952, + 242403584, -4059858, -240722784, -374875232, 34201644, -135057664, -83427288, -328351680, + -159456688, 203876304, -143020848, -533395360, 8248299, 367404608, -136988688, -1258876, + 97568656, -241977648, -126017800, 247630384, -365683456, 53167904, -108469000, 251701248, + -123093736, 39585332, -154665888, -24703136, 7353051, -474126784, -113866144, -35694156, + 139195392, -468723296, 428311936, -380354976, -35491500, -62006780, -256468384, -41569924, + 360877280, -165529200, -740558848, -415832608, -52416796, -101624464, -287548640, 136351456, + -120774896, -427036864, 136166544, -30462714, -672141568, -302596032, -280948000, -339320288, + -320267584, 27822668, -322737600, -203237600, -267306144, -190168784, -110932816, 373300416, + -188565680, -306524224, 462912736, -549809344, -409228288, 686058, 435072864, 386936160, + -362037120, 241964848, -193144640, 9445155, -228799360, -33253348, -74785264, 256273472, + 48672468, -113725904, -62496964, -181029904, -593157504, 108819440, 327648512, -257753232, + -242533968, 345835936, -303105472, -633719744, 151319072, -170836864, -228132976, 41993528, + 109792840, 191082432, 299999424, 346609856, -11954250, 18103020, 365500704, 450601632, + -293279840, 410115872, -428409184, 10806831, -357557952, 167146912, -299529152, +]; + +pub const ARRAY_5: [i128; SIZE] = [ + 127035448, 175800880, 281497536, -411941696, -264164080, -127694232, -45993100, 110923856, + 208558480, -115267088, 704105024, -394156608, -72113160, -196975088, -296649472, 258438064, + 77190560, -399469152, 32350040, 358328736, -523654144, 176738608, -590895424, 101327368, + -38921968, -28334198, 7058896, -209140208, -127765736, 7311183, 225270576, -211937024, + -420455712, 174436144, 242318992, -252048688, -479570016, -144796160, -45444004, -133723592, + 131915824, 288047328, 445757664, -365277408, 222786400, -292456032, 132004480, 16135940, + -233582288, -374484192, -141195376, 986802624, -780243200, -264348960, 407313952, 170913488, + -228384672, -22726678, 152216384, 127952592, 186925792, 463619232, -152702368, 192807840, + -191518080, 334951008, 205051936, -147308624, -67150016, 97064704, -41490148, 114929904, + 302218624, 404702880, 665118400, 18796058, 517905024, -110587256, -342727520, 488700256, + -28430586, 210217072, -624388928, 926040896, -591654976, 87983144, 225488112, -77603352, + 181253760, -297274816, -255007952, 218410800, 518976640, -268833792, -306650080, 147832576, + -261579840, 312590368, 379895872, 239279984, -726835648, -174617088, 293776736, -21657498, + -25575552, -240416624, 430961824, 114050552, 360426912, -379094976, -72823480, 149508656, + -468734432, -48128608, 136305024, 1018973376, -409616032, 164492528, -216742960, 233773680, + -464665568, -324696512, -124491656, 17075332, -640150144, 31053562, -198882176, -163147808, + -69681840, 24709148, 261711504, 205023904, 44082328, 102049944, -349655648, 338487136, + -249138944, 165197776, 636199104, -192810064, 515022720, -54892552, -267944304, 146754464, + -330387584, -278442880, -434854368, 472069984, -626530624, 122371088, 296007520, -277172960, + -361591744, 65485084, 53911020, 305993440, 135899648, 227196096, 194683408, -48348192, + -496362688, -148464592, 99542888, -164461520, -357384992, 228694560, 210119392, 586458368, + -203697840, -94649672, 392422688, -283373888, 686132608, -366901632, -211105280, 167521904, + -325632864, -519638144, -131494648, 1088629248, -198665072, 178327408, 299500480, -162591088, + 24351948, -279264480, -174249056, 151956336, -263512880, 206384240, -180853200, -256865808, + 51479832, 254732528, 345698720, -331017408, 162852272, 250261024, 298571552, -53903836, + 332612832, 52664796, 273553440, -66406160, 515259232, 183928624, 312200608, 143201008, + -213490480, 76265488, -480184448, 378068864, -756118912, 84117048, 190064032, -96641200, + -490705312, 141126016, 39601292, -120415816, -680528960, 193226704, -252341120, 29418230, + -117000648, 145997776, 80117424, -563609024, -62262140, 196627888, -142306448, -206754272, + -229997680, -330204480, 550456320, -90013704, 689411136, -49908960, -233673888, 177428240, + 45794456, -46763204, -112131832, 590128704, -363185600, -112057320, -195109680, 71244672, + -190788368, 134541616, -86260768, -38043648, 183274608, 178830032, -167130992, 97954192, + -92731760, -5644242, -139018992, -208032192, -103912400, 84232424, -238301920, 366373888, + -228921760, -70913896, 341710816, 46490432, 601084480, -36735992, -215139472, 153231104, + -105456384, -404656128, -167435648, 1053991040, -781715776, -146237184, 277285024, 304159136, + -174131856, 179870208, -248963152, 313521632, -233708128, 299800256, -376404032, 178163984, + 185259344, 275845248, -46608948, -114210336, -222830608, -673885184, 402875744, -31931698, + 262381712, -396806944, 14803851, 221165056, -247776496, 145953728, 304126464, 173752304, + 315401152, 349545184, 239468800, 224126304, -151424496, 147048016, -15596633, 519028096, + 333166656, 33628552, 65969752, -195655040, -381632800, -225674512, 317270432, -362832160, + 59999196, -67498128, -209208672, 268933664, 253052864, -649277824, 482212416, 182985472, + 101074456, 849838080, 232720912, 50304600, 273787488, -42140560, 144824656, 788323712, + -79913784, 213462496, 179724240, 130679392, -742983296, 326448896, -344171136, -350555488, + -30945812, 86157312, -96127488, -320283520, -698658816, 456484544, 416750112, 341300832, + -328794208, -130746360, -256572416, -33985988, -144970656, -276737088, 182383456, -153568800, + -233734304, 396903200, 296106240, 88425376, 57666832, 133193280, 82655448, 488061024, 13411613, + -25852340, -7741807, -186196800, -712415360, -465579264, -431435008, -262139008, -246213872, + -22554810, -190250368, -135616352, -197961536, -211752864, -129892752, -274241984, 147947072, + -504534880, -393771840, -342419008, -368598400, -175208096, 8048182, 310791776, 134509168, + -497454720, 428577184, -200586176, -222151904, 230144816, -40710096, -696302784, 457704672, + 189981472, -281208160, 50225068, -165158080, -163002864, -154362016, 197692832, 540918016, + -177361040, -182371584, 13411851, -173728864, 75603432, 337545920, -242898992, 556080256, + 14683870, 297097024, -2994721, -286580224, -288687520, -70240752, 105447144, -99248056, + -452354528, -171912640, 151575120, 126017248, -309147136, 322532096, 46244272, 75535064, + 269369344, 71530560, 316826752, -12654291, 390725600, 280997568, 656924928, 572145536, + 329463360, 329505632, 36992216, -278345728, 93301912, -10032673, -74071040, -63154496, + 227079920, -15677310, 348370880, -231867168, -218361696, -95352952, -59261708, -74568672, + 431956640, 210973312, -24057004, -12685031, 254540336, -77736432, 215136912, -215227952, + 242403584, -4059858, -240722784, -374875232, 34201644, -135057664, -83427288, -328351680, + -159456688, 203876304, -143020848, -533395360, 8248299, 367404608, -136988688, -1258876, + 97568656, -241977648, -126017800, 247630384, -365683456, 53167904, -108469000, 251701248, + -123093736, 39585332, -154665888, -24703136, 7353051, -474126784, -113866144, -35694156, + 139195392, -468723296, 428311936, -380354976, -35491500, -62006780, -256468384, -41569924, + 360877280, -165529200, -740558848, -415832608, -52416796, -101624464, -287548640, 136351456, + -120774896, -427036864, 136166544, -30462714, -672141568, -302596032, -280948000, -339320288, + -320267584, 27822668, -322737600, -203237600, -267306144, -190168784, -110932816, 373300416, + -188565680, -306524224, 462912736, -549809344, -409228288, 686058, 435072864, 386936160, + -362037120, 241964848, -193144640, 9445155, -228799360, -33253348, -74785264, 256273472, + 48672468, -113725904, -62496964, -181029904, -593157504, 108819440, 327648512, -257753232, + -242533968, 345835936, -303105472, -633719744, 151319072, -170836864, -228132976, 41993528, + 109792840, 191082432, 299999424, 346609856, -11954250, 18103020, 365500704, 450601632, + -293279840, 410115872, -428409184, 10806831, -357557952, 167146912, -299529152, +]; + +pub const CONSTANT: i128 = -58748348; diff --git a/examples/big-data-section/src/data_access.rs b/examples/big-data-section/src/data_access.rs new file mode 100644 index 00000000000..4b3043b5c8d --- /dev/null +++ b/examples/big-data-section/src/data_access.rs @@ -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 . + +use crate::constants::*; + +#[derive(Debug)] +pub struct DataAccess { + array_index: u8, + value_index: usize, +} + +impl DataAccess { + pub fn from_payload(payload: &[u8]) -> Result { + 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::() % 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, + } + } +} diff --git a/examples/big-data-section/src/lib.rs b/examples/big-data-section/src/lib.rs new file mode 100644 index 00000000000..9e502752f4d --- /dev/null +++ b/examples/big-data-section/src/lib.rs @@ -0,0 +1,76 @@ +// 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 . + +#![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; + +#[cfg(test)] +mod tests { + use crate::data_access::DataAccess; + use gtest::{Log, Program, System}; + + const USER_ID: u64 = gtest::constants::DEFAULT_USER_ALICE; + + use proptest::{ + arbitrary::any, collection::vec, proptest, test_runner::Config as ProptestConfig, + }; + // Testing random access to data section + proptest! { + #![proptest_config(ProptestConfig { + cases: 200, // Set the number of test cases to run + .. ProptestConfig::default() + })] + #[test] + fn test_big_data_section(payload in vec(any::(), 2..100)){ + 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); + + let expected_value = DataAccess::from_payload(&payload).expect("").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)); + } + } +} diff --git a/examples/big-data-section/src/wasm.rs b/examples/big-data-section/src/wasm.rs new file mode 100644 index 00000000000..d4f690f3c33 --- /dev/null +++ b/examples/big-data-section/src/wasm.rs @@ -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 . + +use crate::data_access::DataAccess; +use gstd::{msg, prelude::*}; + +#[unsafe(no_mangle)] +extern "C" fn init() { + 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"); +} diff --git a/utils/wasm-builder/src/wasm_project.rs b/utils/wasm-builder/src/wasm_project.rs index b1f6d2f7da4..f319e2616cf 100644 --- a/utils/wasm-builder/src/wasm_project.rs +++ b/utils/wasm-builder/src/wasm_project.rs @@ -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")?; } diff --git a/utils/wasm-optimizer/src/data_section.rs b/utils/wasm-optimizer/src/data_section.rs new file mode 100644 index 00000000000..4e28b86f10a --- /dev/null +++ b/utils/wasm-optimizer/src/data_section.rs @@ -0,0 +1,54 @@ +// 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 . + +use pwasm_utils::parity_wasm::elements::{DataSection, DataSegment, Instruction}; +use std::collections::BTreeMap; + +/// Returns statistics of zero bytes between data segments. +/// Key - number of zero bytes between data segments. +/// Value - number of occurrences. +pub fn zero_bytes_gap_statistics(data_section: &DataSection) -> BTreeMap { + let mut statistics = BTreeMap::new(); + for pair in data_section.entries().windows(2) { + let zero_bytes = match segments_zero_bytes_gap(&pair[0], &pair[1]) { + Some(zero_bytes) => zero_bytes, + // skip `passive` data segments + None => continue, + }; + *statistics.entry(zero_bytes).or_insert(0) += 1; + } + + statistics +} + +/// Returns the number of zero bytes between two data segments. +/// Formula: `end_segment_offset - start_segment_offset - start_segment_size` +pub fn segments_zero_bytes_gap( + start_segment: &DataSegment, + end_segment: &DataSegment, +) -> Option { + let offset = |segment: &DataSegment| match segment.offset().clone()?.code().first()? { + Instruction::I32Const(value) => Some(*value), + _ => None, + }; + + let start_segment_offset: usize = offset(start_segment)?.try_into().unwrap(); + let end_segment_offset: usize = offset(end_segment)?.try_into().unwrap(); + + Some(end_segment_offset - start_segment_offset - start_segment.value().len()) +} diff --git a/utils/wasm-optimizer/src/lib.rs b/utils/wasm-optimizer/src/lib.rs index c83d2c8996e..cfa6b6da3ee 100644 --- a/utils/wasm-optimizer/src/lib.rs +++ b/utils/wasm-optimizer/src/lib.rs @@ -18,6 +18,7 @@ mod cargo_command; mod cargo_toolchain; +mod data_section; mod optimize; mod stack_end; diff --git a/utils/wasm-optimizer/src/optimize.rs b/utils/wasm-optimizer/src/optimize.rs index 942662c2535..857ca5effb3 100644 --- a/utils/wasm-optimizer/src/optimize.rs +++ b/utils/wasm-optimizer/src/optimize.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::stack_end; +use crate::{data_section, stack_end}; use anyhow::{anyhow, Context, Result}; #[cfg(not(feature = "wasm-opt"))] use colored::Colorize; @@ -47,6 +47,8 @@ const OPTIMIZED_EXPORTS: [&str; 7] = [ STACK_END_EXPORT_NAME, ]; +const MAX_DATA_SEGMENTS_AMOUNT: usize = 1024; + /// Type of the output wasm. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum OptType { @@ -96,6 +98,88 @@ impl Optimizer { .retain(|section| !matches!(section, Section::Reloc(_) | Section::Custom(_))) } + /// Join data segments + /// + /// This function joins data segments that are separated by a small number of zero bytes. + /// Is is necessary to fit in the network limits - 1024 data segment limit. + pub fn join_data_sections(&mut self) { + let statistics = match self.module.data_section() { + Some(data_section) if data_section.entries().len() >= MAX_DATA_SEGMENTS_AMOUNT => { + data_section::zero_bytes_gap_statistics(data_section) + } + _ => return, + }; + + let mut data_sections_to_join = + self.module.data_section().unwrap().entries().len() - MAX_DATA_SEGMENTS_AMOUNT; + + let mut accumulated_sum = 0; + let threshold_zero_bytes = statistics + .iter() + .find_map(|(&key, &count)| { + accumulated_sum += count; + if accumulated_sum >= data_sections_to_join { + Some(key) + } else { + None + } + }) + .unwrap_or_else(|| *statistics.keys().last().unwrap()); + + let mut current_segment_index = 1; + while data_sections_to_join > 0 { + let previous_segment = self + .module + .data_section() + .unwrap() + .entries() + .get(current_segment_index - 1) + .unwrap(); + let current_segment = self + .module + .data_section() + .unwrap() + .entries() + .get(current_segment_index) + .unwrap(); + + let zero_bytes = + match data_section::segments_zero_bytes_gap(previous_segment, current_segment) { + Some(zero_bytes) => zero_bytes, + None => { + current_segment_index += 1; + // skip `passive` data segments + continue; + } + }; + + if zero_bytes <= threshold_zero_bytes { + let current_segment = self + .module + .data_section_mut() + .unwrap() + .entries_mut() + .remove(current_segment_index); + + let mut addition_part: Vec = vec![0u8; zero_bytes]; + addition_part.extend(current_segment.value()); + + self.module + .data_section_mut() + .unwrap() + .entries_mut() + .get_mut(current_segment_index - 1) + .unwrap() + .value_mut() + .extend(addition_part); + + data_sections_to_join -= 1; + } else { + current_segment_index += 1; + } + } + } + pub fn flush_to_file(self, path: &PathBuf) { fs::write(path, self.module.into_bytes().unwrap()).unwrap(); }