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

Investigate whether we need to charge for data section #3907

Closed
grishasobol opened this issue Apr 19, 2024 · 8 comments · Fixed by #3929
Closed

Investigate whether we need to charge for data section #3907

grishasobol opened this issue Apr 19, 2024 · 8 comments · Fixed by #3929
Assignees

Comments

@grishasobol
Copy link
Member

grishasobol commented Apr 19, 2024

Presently we have a benchmark, which calculates weight for module instantiation depending on code size:

// `c`: Size of the code in kilobytes.
instantiate_module_per_kb {
let c in 0 .. T::Schedule::get().limits.code_len / 1024;
let WasmModule { code, .. } = WasmModule::<T>::sized(c * 1024, Location::Init);
}: {
let ext = Externalities::new(ProcessorContext::new_mock());
Environment::new(ext, &code, DispatchKind::Init, Default::default(), max_pages::<T>().into()).unwrap();
}

Here we charge for instantiation (not the best naming yes):

// The last one thing is to load program memory. Adjust gas counters for memory pages.
let context = match core_processor::precharge_for_memory(block_config, context) {
Ok(context) => context,
Err(journal) => return journal,
};

The problem is that code size is summary size of all WASM code sections. But in benchmark we increase only code section, so actually it's time for wasm functions code instantiation. On other hand we can increase data section and check what will happen. Maybe data section instantiation is heavier than code instantiation, so here we have potential problem with gear::run time execution. Maybe data section instantiation weight is significantly smaller than code section instantiation weight, so here we have potential weight gain in case we will charge for code section size and data section size separately.

What can you do:

  1. you can run instantiation benchmark and see the results:
$ cargo build -p gear-cli --release --features=runtime-benchmarks
$ ./target/release/gear benchmark pallet --chain=vara-dev --pallet=pallet_gear  --extrinsic=instantiate_module_per_kb
  1. change instantiate_module_per_kb, so that two params will be taken into account, see two params example in gr_create_program_per_kb.
  2. analyse the results and think what can be done here
@ByteNacked ByteNacked self-assigned this Apr 22, 2024
@ByteNacked ByteNacked added the A1-inprogress Issue is in progress or PR draft is not ready to be reviewed label Apr 22, 2024
@ByteNacked
Copy link
Member

ByteNacked commented Apr 23, 2024

Bench results:

Data section (denoted as d) and code section (c):

Single data section

Proportions of code weight/data weight seems close to 10/9.

Model:
Time ~=    60.98
    + c    0.103
    + d    0.094
              µs
Full output
Min Squares Analysis
========
-- Extrinsic Time --

Data points distribution:
    c     d   mean µs  sigma µs       %
    0   512     110.3     0.906    0.8%
   10   512     111.3     0.774    0.6%
   20   512     111.1     0.381    0.3%
   31   512     112.6     1.803    1.6%
   41   512     114.7     1.772    1.5%
   52   512     115.8     3.052    2.6%
   62   512       118     2.228    1.8%
   73   512     119.9      2.55    2.1%
   83   512     117.6     1.562    1.3%
   94   512     116.6     0.474    0.4%
  104   512       124     3.948    3.1%
  114   512     120.8     2.626    2.1%
  125   512     123.1     4.288    3.4%
  135   512     123.2     2.509    2.0%
  146   512     130.3     3.754    2.8%
  156   512     126.3     3.341    2.6%
  167   512     128.2     3.292    2.5%
  177   512     125.8     3.737    2.9%
  188   512     124.3      4.35    3.4%
  198   512     136.6     8.966    6.5%
  208   512     131.8     2.089    1.5%
  219   512     123.7     2.878    2.3%
  229   512     126.3     2.878    2.2%
  240   512     130.8     4.283    3.2%
  250   512     146.5     5.853    3.9%
  261   512       138     3.229    2.3%
  271   512     139.5     5.542    3.9%
  282   512       138     6.525    4.7%
  292   512     137.5     3.829    2.7%
  303   512     141.3     7.554    5.3%
  313   512     138.8     2.224    1.6%
  323   512     141.6     4.247    2.9%
  334   512     135.5     2.992    2.2%
  344   512       145     3.964    2.7%
  355   512     140.9     5.522    3.9%
  365   512     140.3     4.278    3.0%
  376   512     136.1     3.265    2.3%
  386   512     138.4     2.917    2.1%
  397   512     139.9     2.136    1.5%
  407   512     146.9     4.471    3.0%
  417   512     145.8     7.678    5.2%
  428   512     156.7     8.214    5.2%
  438   512       166     5.747    3.4%
  449   512     163.6     7.622    4.6%
  459   512     168.8     7.577    4.4%
  470   512     166.4     7.703    4.6%
  480   512     164.6         6    3.6%
  491   512     177.4     13.05    7.3%
  501   512       164     8.268    5.0%
  512     0     75.36     1.976    2.6%
  512    10       140     6.544    4.6%
  512    20     129.4     4.885    3.7%
  512    31     123.6      2.64    2.1%
  512    41     123.3     6.061    4.9%
  512    52       126     8.512    6.7%
  512    62     121.6     2.565    2.1%
  512    73     126.4     7.211    5.7%
  512    83     126.9     4.496    3.5%
  512    94       126     2.729    2.1%
  512   104     126.5     5.703    4.5%
  512   114     128.1       3.7    2.8%
  512   125     130.1     6.894    5.2%
  512   135     124.5     2.858    2.2%
  512   146     135.5     7.685    5.6%
  512   156     130.2     3.788    2.9%
  512   167     137.9     10.23    7.4%
  512   177     131.2     3.833    2.9%
  512   188     127.5      3.54    2.7%
  512   198     129.5     5.261    4.0%
  512   208     127.8     4.527    3.5%
  512   219     127.3     1.654    1.2%
  512   229       126     2.204    1.7%
  512   240     143.7     6.209    4.3%
  512   250     154.3     6.548    4.2%
  512   261     135.7     3.986    2.9%
  512   271     129.6     3.761    2.9%
  512   282     130.7     4.018    3.0%
  512   292     129.8     2.488    1.9%
  512   303       131     2.768    2.1%
  512   313       131      3.56    2.7%
  512   323     146.4     9.439    6.4%
  512   334     131.7     2.625    1.9%
  512   344     129.2     1.009    0.7%
  512   355       133      2.24    1.6%
  512   365     137.8     5.044    3.6%
  512   376     149.7     13.91    9.2%
  512   386     145.7     4.934    3.3%
  512   397     151.7     8.682    5.7%
  512   407     142.6     4.005    2.8%
  512   417     155.1     7.943    5.1%
  512   428     143.1     4.284    2.9%
  512   438     144.5     5.706    3.9%
  512   449     148.4     4.201    2.8%
  512   459     152.2     5.285    3.4%
  512   470     171.4     10.98    6.4%
  512   480     178.2     12.52    7.0%
  512   491     174.1     8.321    4.7%
  512   501     197.2     13.74    6.9%
  512   512     164.3     12.04    7.3%

Quality and confidence:
param     error
c         0.002
d         0.002

Model:
Time ~=    60.98
    + c    0.103
    + d    0.094
              µs

Overlapped data sections (50% overlap)

In this test data section overlapped by 50%, which seems lead to redundant memory copy during initialization.
Code/data weight proportion (~ 10/9) remains the same, because the total amount of bytes copied during data section initialization remains the same.

Model:
Time ~=    83.32
    + c    0.066
    + d    0.058
              µs
Full output
Pallet: "pallet_gear", Extrinsic: "instantiate_module_per_kb2", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info
========

Median Slopes Analysis
========
-- Extrinsic Time --

Model:
Time ~=    88.23
    + c    0.056
    + d    0.051
              µs

Reads = 0 + (0 * c) + (0 * d)
Writes = 0 + (0 * c) + (0 * d)
Recorded proof Size = 0 + (0 * c) + (0 * d)

Min Squares Analysis
========
-- Extrinsic Time --

Data points distribution:
    c     d   mean µs  sigma µs       %
    0   512       117     1.762    1.5%
   10   512     115.6     1.622    1.4%
   20   512       116     1.268    1.0%
   31   512       116     0.591    0.5%
   41   512     116.7     0.921    0.7%
   52   512       117     0.387    0.3%
   62   512     120.3     1.565    1.3%
   73   512     118.4     1.259    1.0%
   83   512     119.2     1.035    0.8%
   94   512     119.5     0.766    0.6%
  104   512     120.3     1.123    0.9%
  114   512     121.4     1.075    0.8%
  125   512     122.7     1.861    1.5%
  135   512     121.2     0.802    0.6%
  146   512     122.4      1.07    0.8%
  156   512       124     2.231    1.7%
  167   512     124.1     1.706    1.3%
  177   512     123.3     0.453    0.3%
  188   512     124.5     1.366    1.0%
  198   512     124.1     0.873    0.7%
  208   512     127.6      0.54    0.4%
  219   512     128.1     2.526    1.9%
  229   512     123.7     0.769    0.6%
  240   512     125.3     0.815    0.6%
  250   512     127.2     0.765    0.6%
  261   512     127.8     1.925    1.5%
  271   512     126.9     1.023    0.8%
  282   512     127.5      1.63    1.2%
  292   512     131.6      5.57    4.2%
  303   512     128.4     0.856    0.6%
  313   512     135.3     2.383    1.7%
  323   512     141.3     2.726    1.9%
  334   512     142.5     4.439    3.1%
  344   512     133.9     2.212    1.6%
  355   512     135.8     2.322    1.7%
  365   512     133.8     3.689    2.7%
  376   512     132.5     1.713    1.2%
  386   512     139.6     5.007    3.5%
  397   512     143.5      5.71    3.9%
  407   512     142.3     5.184    3.6%
  417   512     142.2     2.506    1.7%
  428   512     138.9     2.571    1.8%
  438   512     139.1      2.66    1.9%
  449   512     136.7     1.863    1.3%
  459   512     136.5     1.906    1.3%
  470   512     142.3     3.511    2.4%
  480   512     139.9     2.863    2.0%
  491   512     144.9     1.491    1.0%
  501   512     147.6     2.599    1.7%
  512     0     62.02     2.595    4.1%
  512    10     118.5     1.411    1.1%
  512    20     119.9     2.077    1.7%
  512    31       122       1.2    0.9%
  512    41     123.4     3.396    2.7%
  512    52     122.3     1.605    1.3%
  512    62     118.7     1.317    1.1%
  512    73     124.7     2.315    1.8%
  512    83     123.6     2.301    1.8%
  512    94     127.2     4.046    3.1%
  512   104     125.2     3.129    2.4%
  512   114     129.9     2.702    2.0%
  512   125     128.7     3.037    2.3%
  512   135     131.7      6.05    4.5%
  512   146     133.1     4.886    3.6%
  512   156       134     5.019    3.7%
  512   167     129.3     2.867    2.2%
  512   177     134.8     3.711    2.7%
  512   188     135.4     4.042    2.9%
  512   198     131.3     3.352    2.5%
  512   208     131.4     2.774    2.1%
  512   219     129.2     3.155    2.4%
  512   229     134.3     5.766    4.2%
  512   240     134.1     6.332    4.7%
  512   250     129.2     2.472    1.9%
  512   261     140.6     5.989    4.2%
  512   271     138.2     2.856    2.0%
  512   282     140.1     2.134    1.5%
  512   292     131.6     2.356    1.7%
  512   303     132.5     1.609    1.2%
  512   313     132.6     1.652    1.2%
  512   323     135.7     2.933    2.1%
  512   334     143.2     4.832    3.3%
  512   344     140.1     5.769    4.1%
  512   355     146.6     4.643    3.1%
  512   365     136.6     1.588    1.1%
  512   376       139     2.124    1.5%
  512   386     134.6      1.89    1.4%
  512   397     136.6     1.298    0.9%
  512   407     138.9     2.254    1.6%
  512   417     138.9     1.829    1.3%
  512   428     137.2     1.357    0.9%
  512   438     140.6     1.975    1.4%
  512   449     144.9      3.05    2.1%
  512   459     141.6     3.284    2.3%
  512   470     138.2     2.032    1.4%
  512   480     137.9     2.708    1.9%
  512   491     153.6     5.597    3.6%
  512   501     149.7     2.958    1.9%
  512   512     157.6     10.19    6.4%

Quality and confidence:
param     error
c         0.001
d         0.001

Model:
Time ~=    83.32
    + c    0.066
    + d    0.058
              µs

Reads = 0 + (0 * c) + (0 * d)
Writes = 0 + (0 * c) + (0 * d)
Recorded proof Size = 0 + (0 * c) + (0 * d)

@ByteNacked
Copy link
Member

ByteNacked commented Apr 23, 2024

Conclusion I

  • It seems that the instantiation weight of data and code sections differs insignificantly (approximately ~10%), so it's okay to charge for code size (the sum of all sections).
  • Overlapping of data memory sections doesn't affect the overall data section instantiation time because the total amount of bytes copied during initialization remains the same.

@ByteNacked
Copy link
Member

ByteNacked commented Apr 25, 2024

There was a strange non-linearity discovered in the benchmark test results.

Min Squares Analysis
========
-- Extrinsic Time --

Data points distribution:
    c     d   mean µs  sigma µs       %
  ....
  512     0     75.36     1.976    2.6%
  512    10       140     6.544    4.6%    <=== HERE
  512    20     129.4     4.885    3.7%
  512    31     123.6      2.64    2.1%
  512    41     123.3     6.061    4.9%
  512    52       126     8.512    6.7%
  512    62     121.6     2.565    2.1%
  512    73     126.4     7.211    5.7%
  512    83     126.9     4.496    3.5%
  512    94       126     2.729    2.1%
  ....

It seems like an arbitrary constant time is spent on something and is not dependent on the data section size.

After some investigation, the reason behind this was found. In wasmer, during data section initialization, memcpy occurs.

https://github.com/wasmerio/wasmer/blob/2.3.0/lib/vm/src/instance/mod.rs#L1399

fn initialize_memories(
    instance: &Instance,
    data_initializers: &[DataInitializer<'_>],
) -> Result<(), Trap> {
       // ........................

        unsafe {
            let mem_slice = get_memory_slice(init, instance);
            let end = start + init.data.len();
            let to_init = &mut mem_slice[start..end];
            to_init.copy_from_slice(init.data);   <== HERE
        }

This action causes a freshly allocated memory page to write some data in it, resulting in a page fault (more specifically, a TLB fault).

This can easily be verified by writing something to this page beforehand or using mlock during the allocation of the memory.

Theory confirming test
    #[test]
    fn foo() {
        use wabt::Wat2Wasm;

        let wat = r###"
        (module
            (import "env" "memory" (memory 1))
            (data (i32.const 0) "\02\00\00\00\04")
        )"###;

        let wasm = Wat2Wasm::new()
            .validate(true)
            .write_debug_names(true)
            .convert(wat)
            .expect("failed to parse module");

        let compiler = sandbox_wasmer::Singlepass::default();
        let store = sandbox_wasmer::Store::new(&sandbox_wasmer::Universal::new(compiler).engine());

        let module = Module::new(&store, wasm).unwrap();

        struct MyResolver {
            mem: Memory,
        }

        impl Resolver for MyResolver {
            fn resolve(&self, _: u32, _field: &str, _: &str) -> Option<Export> {
                Some(Extern::Memory(self.mem.clone()).to_export())
            }
        }

        let import_object = MyResolver {
            mem: Memory::new(&store, MemoryType::new(0x1000, None, false)).unwrap(),
        };

        unsafe { 
            let mem_ptr = *import_object.mem.get_vm_memory().from.vmmemory().as_ptr();
            println!("Memory ptr: {:X?}", mem_ptr.base);
            mem_ptr.base.write(0x01);  // <== TRIGGERING PAGE FAULT BEFOREHAND
        };


          let t1 = std::time::Instant::now();
          let instance = sandbox_wasmer::Instance::new(&module, &import_object).unwrap();
          std::hint::black_box(instance);
          println!("Time: {:?}", t1.elapsed());
    }

Test results:

Original case:

---- sandbox::wasmer_backend::test::foo stdout ----
Memory ptr: 0x7004b8000000
Time: 108.683µs

Triggering page fault before wasmer data section init:

---- sandbox::wasmer_backend::test::foo stdout ----
Memory ptr: 0x78780c000000
Time: 10.449µs

@ByteNacked
Copy link
Member

ByteNacked commented Apr 25, 2024

Bench test (multiple data segments, different OS pages)

It initializes multiple segments in data section (segment size = 64 bytes) and every segment resides in a different OS page (OS page size = 4Kb).

Bench result
Min Squares Analysis
========
-- Extrinsic Time --

Data points distribution:
    c     d   mean µs  sigma µs       %
    0   512     491.5     3.182    0.6%
   10   512       491     1.223    0.2%
   20   512     571.7     39.02    6.8%
   31   512     494.3     5.778    1.1%
   41   512     491.5     3.844    0.7%
   52   512       489     2.713    0.5%
   62   512     495.3     3.819    0.7%
   73   512     492.3     4.329    0.8%
   83   512     491.5      3.06    0.6%
   94   512     491.6     2.125    0.4%
  104   512     494.6     3.609    0.7%
  114   512     492.6     2.394    0.4%
  125   512     503.9      4.97    0.9%
  135   512     504.4     4.811    0.9%
  146   512     504.4     5.027    0.9%
  156   512     499.2     1.685    0.3%
  167   512     547.5      37.1    6.7%
  177   512     518.8     9.871    1.9%
  188   512     506.5     5.281    1.0%
  198   512       520     9.838    1.8%
  208   512     515.2     4.992    0.9%
  219   512     507.8     3.984    0.7%
  229   512       520     6.579    1.2%
  240   512     516.4     5.679    1.0%
  250   512     531.3     13.64    2.5%
  261   512     518.6     6.535    1.2%
  271   512     523.7         5    0.9%
  282   512     571.9      27.3    4.7%
  292   512     539.1     8.721    1.6%
  303   512     545.4     14.55    2.6%
  313   512     541.1     7.742    1.4%
  323   512     543.5     9.683    1.7%
  334   512     536.9     9.361    1.7%
  344   512     542.2     10.16    1.8%
  355   512     551.6     8.621    1.5%
  365   512     539.9     3.983    0.7%
  376   512     560.2     10.42    1.8%
  386   512     549.5     6.615    1.2%
  397   512     554.4      10.3    1.8%
  407   512     544.1     6.913    1.2%
  417   512     560.8     13.76    2.4%
  428   512     566.8     6.141    1.0%
  438   512     580.9     14.47    2.4%
  449   512     565.7     7.064    1.2%
  459   512     617.1     15.05    2.4%
  470   512     610.6      13.1    2.1%
  480   512     639.2     13.95    2.1%
  491   512     631.5     10.59    1.6%
  501   512      1041     168.1   16.1%
  512     0     55.11     0.504    0.9%
  512    10     114.7     1.222    1.0%
  512    20     122.4     4.929    4.0%
  512    31     121.2     2.526    2.0%
  512    41     119.1      2.14    1.7%
  512    52     120.5     0.723    0.5%
  512    62     119.8     1.535    1.2%
  512    73     170.8     1.761    1.0%
  512    83     169.6     1.347    0.7%
  512    94     171.5     0.665    0.3%
  512   104     172.9     1.102    0.6%
  512   114     173.9      0.75    0.4%
  512   125     174.9     0.865    0.4%
  512   135     231.3     5.174    2.2%
  512   146       232     4.185    1.8%
  512   156     225.9     0.996    0.4%
  512   167     231.6     2.375    1.0%
  512   177       230     2.053    0.8%
  512   188     229.6     1.334    0.5%
  512   198       277     1.151    0.4%
  512   208     284.1     2.146    0.7%
  512   219     298.1     4.129    1.3%
  512   229     301.6     6.844    2.2%
  512   240     298.3     5.857    1.9%
  512   250     301.6     6.613    2.1%
  512   261     339.8       3.9    1.1%
  512   271     340.9     3.676    1.0%
  512   282     344.8     2.687    0.7%
  512   292     355.3     9.938    2.7%
  512   303     346.9      4.51    1.2%
  512   313       346      4.26    1.2%
  512   323     415.3     7.063    1.7%
  512   334     416.5     13.43    3.2%
  512   344     409.5     4.465    1.0%
  512   355     409.3      2.79    0.6%
  512   365     414.2     6.249    1.5%
  512   376     416.6     6.398    1.5%
  512   386     482.2      5.01    1.0%
  512   397       476     6.345    1.3%
  512   407     493.2     10.61    2.1%
  512   417     501.8     31.02    6.1%
  512   428     482.5     4.093    0.8%
  512   438     498.4     5.883    1.1%
  512   449     611.9     11.86    1.9%
  512   459     608.3     14.19    2.3%
  512   470       606     20.23    3.3%
  512   480     611.8     9.189    1.5%
  512   491     627.5     32.35    5.1%
  512   501     615.1      9.59    1.5%
  512   512     640.6     14.28    2.2%

@ByteNacked
Copy link
Member

Please disregard the previous benchmark results as they were conducted with huge pages enabled

So, to determine the weight of data section instantiation, we can separate it into three parameters:

  • Data section size
  • Number of data segments inside the data section
  • Number of OS pages used during data section initialization

Code, data size, number of data segments bench

Let's conduct a benchmark test for the first two, where:

  • 'c' represents the code size in kilobytes (Kb),
  • 'd' represents the data section size in kilobytes (Kb), and
  • 's' represents the number of data segments.
Quality and confidence:
param     error
c         0.002
d         0.002
s             0

Model:
Time ~=    56.65
    + c     0.03
    + d    0.364
    + s    0.009
              µs
Full
Pallet: "pallet_gear", Extrinsic: "instantiate_module_with_code_data_section_per_kb", Lowest values: [], Highest values: [], Steps: 100, Repeat: 100
Raw Storage Info
========

Median Slopes Analysis
========
-- Extrinsic Time --

Model:
Time ~=    27.52
    + c    0.223
    + d    0.452
    + s    0.008
              µs

Reads = 0 + (0 * c) + (0 * d) + (0 * s)
Writes = 0 + (0 * c) + (0 * d) + (0 * s)
Recorded proof Size = 0 + (0 * c) + (0 * d) + (0 * s)

Min Squares Analysis
========
-- Extrinsic Time --

Data points distribution:
    c     d     s   mean µs  sigma µs       %
    0   256 26214     371.4     4.268    1.1%
    2   256 26214     370.2     4.689    1.2%
    5   256 26214     380.4     5.786    1.5%
    7   256 26214     374.8     3.839    1.0%
   10   256 26214     384.9     4.552    1.1%
   12   256 26214     386.6     4.428    1.1%
   15   256 26214     378.8     3.783    0.9%
   18   256 26214     381.7     4.536    1.1%
   20   256 26214     380.6     4.083    1.0%
   23   256 26214     380.8      5.48    1.4%
   25   256 26214     392.4     5.846    1.4%
   28   256 26214     386.9     5.821    1.5%
   31   256 26214     388.6     4.878    1.2%
   33   256 26214     382.4     6.333    1.6%
   36   256 26214     382.3     5.141    1.3%
   38   256 26214     379.7     4.172    1.0%
   41   256 26214     377.7     4.558    1.2%
   43   256 26214     390.7     8.009    2.0%
   46   256 26214     382.8     4.958    1.2%
   49   256 26214     382.1     5.112    1.3%
   51   256 26214     380.9     6.196    1.6%
   54   256 26214     382.3     5.759    1.5%
   56   256 26214     384.8     5.906    1.5%
   59   256 26214     384.1     6.025    1.5%
   62   256 26214     385.4     8.271    2.1%
   64   256 26214     384.5     8.972    2.3%
   67   256 26214     381.7     7.619    1.9%
   69   256 26214     380.1     7.416    1.9%
   72   256 26214     384.6     7.998    2.0%
   74   256 26214     390.4     8.561    2.1%
   77   256 26214     392.1     9.222    2.3%
   80   256 26214     390.1     8.165    2.0%
   82   256 26214     392.4     8.747    2.2%
   85   256 26214     391.3     8.275    2.1%
   87   256 26214     388.1     7.138    1.8%
   90   256 26214     390.2     9.729    2.4%
   93   256 26214     391.1     9.976    2.5%
   95   256 26214     388.1     8.177    2.1%
   98   256 26214     391.9     10.12    2.5%
  100   256 26214     391.6     7.262    1.8%
  103   256 26214     398.6     26.44    6.6%
  106   256 26214     437.7     7.405    1.6%
  108   256 26214     380.9     5.103    1.3%
  111   256 26214     382.3     4.845    1.2%
  113   256 26214     386.9     4.409    1.1%
  116   256 26214     407.8      4.36    1.0%
  118   256 26214     398.8     5.673    1.4%
  121   256 26214     401.3     3.865    0.9%
  124   256 26214       389     5.819    1.4%
  126   256 26214     391.1     5.539    1.4%
  129   256 26214     391.8     4.627    1.1%
  131   256 26214     392.3     4.461    1.1%
  134   256 26214     389.8     3.675    0.9%
  137   256 26214     396.6     5.842    1.4%
  139   256 26214     405.2      6.41    1.5%
  142   256 26214     413.5     5.431    1.3%
  144   256 26214     394.8     8.641    2.1%
  147   256 26214     401.4     9.138    2.2%
  149   256 26214     418.8      6.88    1.6%
  152   256 26214       421     7.217    1.7%
  155   256 26214     418.5     6.222    1.4%
  157   256 26214       427      6.92    1.6%
  160   256 26214     425.3     7.783    1.8%
  162   256 26214       408      6.52    1.5%
  165   256 26214     418.5      5.65    1.3%
  168   256 26214     418.1       8.1    1.9%
  170   256 26214     413.9     6.775    1.6%
  173   256 26214     429.1     7.778    1.8%
  175   256 26214     433.7     7.549    1.7%
  178   256 26214     430.2     7.151    1.6%
  181   256 26214       429     5.603    1.3%
  183   256 26214     419.9     7.257    1.7%
  186   256 26214     431.8     9.103    2.1%
  188   256 26214     434.4     11.74    2.7%
  191   256 26214     432.9     6.462    1.4%
  193   256 26214     435.5     8.538    1.9%
  196   256 26214     431.9     7.055    1.6%
  199   256 26214     416.6     5.115    1.2%
  201   256 26214     402.7     8.172    2.0%
  204   256 26214     420.5     5.926    1.4%
  206   256 26214     420.6     7.539    1.7%
  209   256 26214     422.9     7.288    1.7%
  212   256 26214     421.5     8.091    1.9%
  214   256 26214     423.9     7.551    1.7%
  217   256 26214       443     8.436    1.9%
  219   256 26214     438.7     7.576    1.7%
  222   256 26214       427     8.945    2.0%
  224   256 26214     417.6     5.653    1.3%
  227   256 26214     398.8     7.992    2.0%
  230   256 26214     406.7     8.214    2.0%
  232   256 26214     408.5      6.96    1.7%
  235   256 26214     419.9     8.779    2.0%
  237   256 26214     420.1     9.037    2.1%
  240   256 26214     420.1     8.648    2.0%
  243   256 26214     419.5      5.93    1.4%
  245   256 26214     420.1     7.041    1.6%
  248   256 26214     428.3      10.5    2.4%
  250   256 26214     443.3     11.89    2.6%
  253   256 26214     525.8     28.79    5.4%
  256     0 26214     58.06     1.072    1.8%
  256     2 26214     293.9     4.892    1.6%
  256     5 26214     286.7     3.206    1.1%
  256     7 26214     282.3     3.206    1.1%
  256    10 26214     288.9     4.979    1.7%
  256    12 26214     289.8     5.783    1.9%
  256    15 26214     302.5     4.882    1.6%
  256    18 26214     309.2     5.722    1.8%
  256    20 26214       311     6.364    2.0%
  256    23 26214     296.1     6.175    2.0%
  256    25 26214     293.1     4.654    1.5%
  256    28 26214     331.8     11.35    3.4%
  256    31 26214     355.2     9.125    2.5%
  256    33 26214     329.9     13.57    4.1%
  256    36 26214     316.6      6.05    1.9%
  256    38 26214     317.1     6.417    2.0%
  256    41 26214     311.6     5.122    1.6%
  256    43 26214     327.4     8.988    2.7%
  256    46 26214     339.6     13.58    3.9%
  256    49 26214     356.3     9.751    2.7%
  256    51 26214     354.9      8.87    2.4%
  256    54 26214     337.5     8.517    2.5%
  256    56 26214     318.7     5.202    1.6%
  256    59 26214     326.4     4.523    1.3%
  256    62 26214     335.5     8.483    2.5%
  256    64 26214     324.6     6.377    1.9%
  256    67 26214       328     7.786    2.3%
  256    69 26214       320      5.95    1.8%
  256    72 26214     320.2     4.543    1.4%
  256    74 26214       315     4.796    1.5%
  256    77 26214     330.8     4.406    1.3%
  256    80 26214     327.9     5.855    1.7%
  256    82 26214     327.7     5.619    1.7%
  256    85 26214     327.5     4.497    1.3%
  256    87 26214     342.2     10.54    3.0%
  256    90 26214     336.3     7.633    2.2%
  256    93 26214     335.9     7.571    2.2%
  256    95 26214     344.4     5.531    1.6%
  256    98 26214     336.5     8.056    2.3%
  256   100 26214     383.2     12.27    3.2%
  256   103 26214     362.2      9.31    2.5%
  256   106 26214     405.4     20.01    4.9%
  256   108 26214     361.3     6.793    1.8%
  256   111 26214     395.6     12.95    3.2%
  256   113 26214     373.1     7.436    1.9%
  256   116 26214     376.3     8.302    2.2%
  256   118 26214     378.9     10.38    2.7%
  256   121 26214     367.9     5.797    1.5%
  256   124 26214     367.8     6.463    1.7%
  256   126 26214     370.1     4.914    1.3%
  256   129 26214     368.6      5.52    1.4%
  256   131 26214     371.2     5.211    1.4%
  256   134 26214       389      11.6    2.9%
  256   137 26214     352.8     5.743    1.6%
  256   139 26214     348.4     4.815    1.3%
  256   142 26214     359.2     6.913    1.9%
  256   144 26214       397     10.43    2.6%
  256   147 26214     375.2     34.29    9.1%
  256   149 26214       390     25.26    6.4%
  256   152 26214     366.7     24.14    6.5%
  256   155 26214     348.9     2.313    0.6%
  256   157 26214     354.3     4.296    1.2%
  256   160 26214     361.1     6.818    1.8%
  256   162 26214       366     5.715    1.5%
  256   165 26214     357.2     5.468    1.5%
  256   168 26214     373.3     4.872    1.3%
  256   170 26214     371.9     6.932    1.8%
  256   173 26214     380.2     7.454    1.9%
  256   175 26214     372.3     5.233    1.4%
  256   178 26214     368.8       4.8    1.3%
  256   181 26214     401.3     10.23    2.5%
  256   183 26214     397.3      8.59    2.1%
  256   186 26214     382.7     8.004    2.0%
  256   188 26214     396.2     22.06    5.5%
  256   191 26214     382.2     8.359    2.1%
  256   193 26214     377.1     7.248    1.9%
  256   196 26214     378.3     9.884    2.6%
  256   199 26214     377.5     8.953    2.3%
  256   201 26214     374.2     8.126    2.1%
  256   204 26214     387.4     9.445    2.4%
  256   206 26214     381.7     5.642    1.4%
  256   209 26214     407.9     11.02    2.7%
  256   212 26214       400     6.558    1.6%
  256   214 26214       439     11.91    2.7%
  256   217 26214     371.5     4.075    1.0%
  256   219 26214     378.1     3.451    0.9%
  256   222 26214     374.3     6.799    1.8%
  256   224 26214     370.3     5.408    1.4%
  256   227 26214     386.7      8.25    2.1%
  256   230 26214     380.5     7.633    2.0%
  256   232 26214     402.8     9.776    2.4%
  256   235 26214     430.2     10.67    2.4%
  256   237 26214     427.6     10.57    2.4%
  256   240 26214     434.7     11.53    2.6%
  256   243 26214     424.4     13.71    3.2%
  256   245 26214     406.1     7.824    1.9%
  256   248 26214     402.7     7.294    1.8%
  256   250 26214     440.5     14.02    3.1%
  256   253 26214     462.4     16.77    3.6%
  256   256     0     57.92     0.896    1.5%
  256   256   264     159.8     1.905    1.1%
  256   256   529     161.7     1.983    1.2%
  256   256   794       172     5.195    3.0%
  256   256  1059     166.2     3.628    2.1%
  256   256  1323     168.6     3.822    2.2%
  256   256  1588     171.2     3.262    1.9%
  256   256  1853     173.4     3.809    2.1%
  256   256  2118     174.7     4.089    2.3%
  256   256  2383     178.6     3.471    1.9%
  256   256  2647     183.8     5.404    2.9%
  256   256  2912     182.1       4.5    2.4%
  256   256  3177     184.8     3.737    2.0%
  256   256  3442     187.7     4.786    2.5%
  256   256  3707     190.6     4.981    2.6%
  256   256  3971     192.2     5.103    2.6%
  256   256  4236     199.2      4.02    2.0%
  256   256  4501     199.6     4.819    2.4%
  256   256  4766     201.2     4.354    2.1%
  256   256  5030     209.6     3.412    1.6%
  256   256  5295     216.9     5.369    2.4%
  256   256  5560     218.6     5.625    2.5%
  256   256  5825     212.4     4.674    2.2%
  256   256  6090     210.9     2.206    1.0%
  256   256  6354     211.3     3.984    1.8%
  256   256  6619     214.4     4.981    2.3%
  256   256  6884     214.8     4.306    2.0%
  256   256  7149     221.1      2.53    1.1%
  256   256  7414     227.1     3.197    1.4%
  256   256  7678       234     8.564    3.6%
  256   256  7943     224.3      2.77    1.2%
  256   256  8208     221.6     3.277    1.4%
  256   256  8473     220.9     1.967    0.8%
  256   256  8738     227.2     3.347    1.4%
  256   256  9002       240     3.074    1.2%
  256   256  9267     241.4      5.59    2.3%
  256   256  9532     259.8     20.37    7.8%
  256   256  9797       234     1.877    0.8%
  256   256 10061     237.2     1.802    0.7%
  256   256 10326     253.5     7.224    2.8%
  256   256 10591     245.3     4.449    1.8%
  256   256 10856     248.7     5.848    2.3%
  256   256 11121     244.3     1.647    0.6%
  256   256 11385     249.8     2.544    1.0%
  256   256 11650     250.4     4.031    1.6%
  256   256 11915     257.2     4.755    1.8%
  256   256 12180     267.4     5.639    2.1%
  256   256 12445     266.1     4.676    1.7%
  256   256 12709       270     5.973    2.2%
  256   256 12974     281.9     8.786    3.1%
  256   256 13239     271.8     6.507    2.3%
  256   256 13504     286.3     8.641    3.0%
  256   256 13768     286.9     8.438    2.9%
  256   256 14033     282.3     8.939    3.1%
  256   256 14298     280.8     7.033    2.5%
  256   256 14563     288.8     8.292    2.8%
  256   256 14828     310.3      16.8    5.4%
  256   256 15092     290.2     4.996    1.7%
  256   256 15357     291.9     3.991    1.3%
  256   256 15622     290.9     4.912    1.6%
  256   256 15887     299.5     7.453    2.4%
  256   256 16152     305.3     7.937    2.5%
  256   256 16416     309.1      7.65    2.4%
  256   256 16681     307.9     6.615    2.1%
  256   256 16946     304.4     5.182    1.7%
  256   256 17211     307.6     5.306    1.7%
  256   256 17476       331     15.04    4.5%
  256   256 17740     313.1     4.159    1.3%
  256   256 18005     318.3     4.851    1.5%
  256   256 18270     333.3     8.074    2.4%
  256   256 18535       337      8.46    2.5%
  256   256 18799     347.6      6.97    2.0%
  256   256 19064       340      9.29    2.7%
  256   256 19329     358.3     15.86    4.4%
  256   256 19594     348.3     11.99    3.4%
  256   256 19859     387.2     39.08   10.0%
  256   256 20123       343     7.591    2.2%
  256   256 20388     351.5     11.74    3.3%
  256   256 20653     355.7      12.9    3.6%
  256   256 20918     341.9     5.109    1.4%
  256   256 21183     348.3     9.411    2.7%
  256   256 21447     340.9     2.882    0.8%
  256   256 21712     369.4     10.59    2.8%
  256   256 21977     355.5     7.038    1.9%
  256   256 22242     348.4     1.991    0.5%
  256   256 22506     375.9     13.78    3.6%
  256   256 22771     348.8     2.961    0.8%
  256   256 23036     344.7     4.688    1.3%
  256   256 23301     339.3     2.156    0.6%
  256   256 23566     350.8     7.619    2.1%
  256   256 23830       342     1.747    0.5%
  256   256 24095     338.5     2.105    0.6%
  256   256 24360     345.9     4.767    1.3%
  256   256 24625     354.1      7.57    2.1%
  256   256 24890     353.4     5.703    1.6%
  256   256 25154     353.5     4.605    1.3%
  256   256 25419     368.4     22.24    6.0%
  256   256 25684     425.9     20.93    4.9%
  256   256 25949     389.6     3.533    0.9%
  256   256 26214       425     18.76    4.4%

Quality and confidence:
param     error
c         0.002
d         0.002
s             0

Model:
Time ~=    56.65
    + c     0.03
    + d    0.364
    + s    0.009
              µs

Reads = 0 + (0 * c) + (0 * d) + (0 * s)
Writes = 0 + (0 * c) + (0 * d) + (0 * s)
Recorded proof Size = 0 + (0 * c) + (0 * d) + (0 * s)

As we can see, the weight of data section instantiation, in comparison to the code section, is at least ~5-10 times higher. The number of data segments contributes significantly less to the total result.

OS pages

Let's examine how the number of OS pages contributes to the total weight of the data section. The benchmark was conducted in such a way that for every OS 4Kb page (denoted as 'p'), a single data segment of 1 byte size was created.

Model:
Time ~=    43.74
    + p    0.979
              µs
Full

instantiate_module_per_page_data_segment_1byte

Data points distribution:
    p   mean µs  sigma µs       %
    0     33.97     0.266    0.7%
    2     43.37     0.316    0.7%
    5     47.17     0.938    1.9%
    7     50.18     0.236    0.4%
   10     53.11      0.37    0.6%
   13     55.57     0.403    0.7%
   15     58.26     0.249    0.4%
   18     60.39     0.313    0.5%
   20     64.23     0.552    0.8%
   23     66.64     0.283    0.4%
   26     70.11     0.533    0.7%
   28     73.82     4.088    5.5%
   31     74.72     0.754    1.0%
   33     76.96     0.293    0.3%
   36     79.86     0.492    0.6%
   39     83.78     0.529    0.6%
   41     85.11     0.266    0.3%
   44     88.07     0.739    0.8%
   47      91.2     0.612    0.6%
   49     92.78      0.69    0.7%
   52     95.83     0.524    0.5%
   54     100.3     1.243    1.2%
   57     100.5     0.443    0.4%
   60     103.9     1.465    1.4%
   62     106.2      0.92    0.8%
   65     107.7     1.079    1.0%
   67     109.3     0.217    0.1%
   70     113.1     0.495    0.4%
   73     114.4     0.443    0.3%
   75     117.4     0.311    0.2%
   78     121.1     0.301    0.2%
   80     123.4     0.696    0.5%
   83     126.3     0.473    0.3%
   86     129.8     0.797    0.6%
   88     129.8     0.238    0.1%
   91     133.2     0.696    0.5%
   94       134     0.424    0.3%
   96     140.4     1.736    1.2%
   99     138.9     0.505    0.3%
  101       141     0.408    0.2%
  104     144.3      0.41    0.2%
  107     147.3     0.309    0.2%
  109     148.9     0.363    0.2%
  112     152.4     0.567    0.3%
  114     154.7      0.52    0.3%
  117       157     0.231    0.1%
  120     159.9     0.457    0.2%
  122     162.2     0.697    0.4%
  125     164.9     0.418    0.2%
  128     168.3     0.305    0.1%

Quality and confidence:
param     error
p         0.002

Model:
Time ~=    43.74
    + p    0.979
              µs

Though, this OS 4Kb page weight 0.979 is already included in data section size weight from previous test.
So it can be discarded.

data_section_sizer_per_1Kb = os_4kb_page_weight / 4 + data_init_memcpy +  ...
0.364 = 0.979 / 4 + data_init_memcpy + ...
0.364 = 0.2435 + data_init_memcpy +...

Conclusion:

  • We should charge separately for data section instantiation depending on the size of the data section.
  • The number of data segments contributes significantly less to the total weight of data section instantiation, but its impact becomes noticeable only with a high number of segments. Perhaps, we should consider charging for this separately as well.
  • The number of used OS pages contributes significantly to the total weight, but it's already included in the data section size weight.

@ByteNacked
Copy link
Member

ByteNacked commented Apr 26, 2024

Additional bench results:
bench_result_instantiate_module_per_page_data_segment.txt

@ByteNacked
Copy link
Member

Another bench run from different machine, ubuntu@ec2-54-153-113-123.us-west-1.compute.amazonaws.com.

Result:

Model:
Time ~=    357.4
    + c    0.083
    + d    1.014
    + s    0.014
              µs
Full result
Pallet: "pallet_gear", Extrinsic: "instantiate_module_with_code_data_section_per_kb", Lowest values: [], Highest values: [], Steps: 50, Repeat: 20
Raw Storage Info
========

Median Slopes Analysis
========
-- Extrinsic Time --

Model:
Time ~=    323.8
    + c    0.398
    + d    0.782
    + s    0.015
              µs

Reads = 0 + (0 * c) + (0 * d) + (0 * s)
Writes = 0 + (0 * c) + (0 * d) + (0 * s)
Recorded proof Size = 0 + (0 * c) + (0 * d) + (0 * s)

Min Squares Analysis
========
-- Extrinsic Time --

Data points distribution:
    c     d     s   mean µs  sigma µs       %
    0   512 52428      1549     20.37    1.3%
   10   512 52428      1562     6.556    0.4%
   20   512 52428      1516     10.86    0.7%
   31   512 52428      1553     15.06    0.9%
   41   512 52428      1541     9.768    0.6%
   52   512 52428      1543     8.692    0.5%
   62   512 52428      1585     12.97    0.8%
   73   512 52428      1551     11.26    0.7%
   83   512 52428      1576     8.634    0.5%
   94   512 52428      1590     16.55    1.0%
  104   512 52428      1595      12.5    0.7%
  114   512 52428      1645     14.92    0.9%
  125   512 52428      1637     12.65    0.7%
  135   512 52428      1660     12.08    0.7%
  146   512 52428      1614     14.89    0.9%
  156   512 52428      1667     16.07    0.9%
  167   512 52428      1679     16.28    0.9%
  177   512 52428      1694     16.02    0.9%
  188   512 52428      1668     19.33    1.1%
  198   512 52428      1674     8.132    0.4%
  208   512 52428      1674     13.07    0.7%
  219   512 52428      1665     6.477    0.3%
  229   512 52428      1645     8.811    0.5%
  240   512 52428      1668     10.76    0.6%
  250   512 52428      1668      10.1    0.6%
  261   512 52428      1676     10.39    0.6%
  271   512 52428      1696     13.31    0.7%
  282   512 52428      1702     11.22    0.6%
  292   512 52428      1698     14.61    0.8%
  303   512 52428      1694     9.083    0.5%
  313   512 52428      1693      17.2    1.0%
  323   512 52428      1736     16.19    0.9%
  334   512 52428      1691     18.54    1.0%
  344   512 52428      1694     19.42    1.1%
  355   512 52428      1706     16.75    0.9%
  365   512 52428      1705     9.802    0.5%
  376   512 52428      1686     11.64    0.6%
  386   512 52428      1705     11.28    0.6%
  397   512 52428      1718     13.86    0.8%
  407   512 52428      1717     16.05    0.9%
  417   512 52428      1712     15.18    0.8%
  428   512 52428      1700     10.07    0.5%
  438   512 52428      1718     12.82    0.7%
  449   512 52428      1712     17.97    1.0%
  459   512 52428      1731      14.9    0.8%
  470   512 52428      1751     10.92    0.6%
  480   512 52428      1751     14.81    0.8%
  491   512 52428      1762     16.44    0.9%
  501   512 52428      1771     7.664    0.4%
  512     0 52428       376     4.308    1.1%
  512    10 52428     377.1     6.109    1.6%
  512    20 52428     373.1     4.927    1.3%
  512    31 52428       384      4.89    1.2%
  512    41 52428     375.8     6.703    1.7%
  512    52 52428      1405     11.55    0.8%
  512    62 52428      1405     13.16    0.9%
  512    73 52428      1416     15.02    1.0%
  512    83 52428      1419     11.81    0.8%
  512    94 52428      1421     17.52    1.2%
  512   104 52428      1485     11.62    0.7%
  512   114 52428      1484     13.84    0.9%
  512   125 52428      1474      11.7    0.7%
  512   135 52428      1475     18.46    1.2%
  512   146 52428      1471     13.17    0.8%
  512   156 52428      1545     14.53    0.9%
  512   167 52428      1573     12.56    0.7%
  512   177 52428      1544     14.76    0.9%
  512   188 52428      1531     17.96    1.1%
  512   198 52428      1531      10.5    0.6%
  512   208 52428      1571      13.5    0.8%
  512   219 52428      1570     18.05    1.1%
  512   229 52428      1556     8.941    0.5%
  512   240 52428      1559     14.05    0.9%
  512   250 52428      1537     15.31    0.9%
  512   261 52428      1594     16.01    1.0%
  512   271 52428      1588     13.11    0.8%
  512   282 52428      1568     12.13    0.7%
  512   292 52428      1566     17.76    1.1%
  512   303 52428      1558      13.7    0.8%
  512   313 52428      1595     7.864    0.4%
  512   323 52428      1617      8.26    0.5%
  512   334 52428      1609     11.72    0.7%
  512   344 52428      1633     8.777    0.5%
  512   355 52428      1644     19.62    1.1%
  512   365 52428      1668     9.006    0.5%
  512   376 52428      1670      14.4    0.8%
  512   386 52428      1668     18.03    1.0%
  512   397 52428      1660     16.12    0.9%
  512   407 52428      1668     13.95    0.8%
  512   417 52428      1686      17.5    1.0%
  512   428 52428      1703     12.98    0.7%
  512   438 52428      1684     12.36    0.7%
  512   449 52428      1669     17.67    1.0%
  512   459 52428      1668     11.22    0.6%
  512   470 52428      1726     12.91    0.7%
  512   480 52428      1708       8.1    0.4%
  512   491 52428      1715     12.43    0.7%
  512   501 52428      1717     13.07    0.7%
  512   512     0     368.9     6.524    1.7%
  512   512  1069     898.6     30.83    3.4%
  512   512  2139       906     11.95    1.3%
  512   512  3209       919     19.12    2.0%
  512   512  4279     957.3      13.6    1.4%
  512   512  5349       989      21.3    2.1%
  512   512  6419      1000     15.02    1.5%
  512   512  7489      1053     59.65    5.6%
  512   512  8559      1024     19.07    1.8%
  512   512  9629      1044     13.88    1.3%
  512   512 10699      1058     12.99    1.2%
  512   512 11769      1071      23.6    2.2%
  512   512 12839      1079     15.31    1.4%
  512   512 13909      1110     26.17    2.3%
  512   512 14979      1119      31.2    2.7%
  512   512 16049      1125     19.77    1.7%
  512   512 17119      1149     11.06    0.9%
  512   512 18189      1161     8.753    0.7%
  512   512 19259      1191     8.795    0.7%
  512   512 20329      1217     12.15    0.9%
  512   512 21399      1213     13.51    1.1%
  512   512 22469      1227     13.03    1.0%
  512   512 23539      1262      8.89    0.7%
  512   512 24609      1278     11.14    0.8%
  512   512 25679      1284     11.71    0.9%
  512   512 26748      1288     6.887    0.5%
  512   512 27818      1298     7.855    0.6%
  512   512 28888      1312     13.93    1.0%
  512   512 29958      1336     13.56    1.0%
  512   512 31028      1337     10.76    0.8%
  512   512 32098      1386     10.42    0.7%
  512   512 33168      1369     10.99    0.8%
  512   512 34238      1413     13.72    0.9%
  512   512 35308      1415     16.15    1.1%
  512   512 36378      1438     13.61    0.9%
  512   512 37448      1465     11.71    0.7%
  512   512 38518      1473      11.2    0.7%
  512   512 39588      1495     4.369    0.2%
  512   512 40658      1516      7.66    0.5%
  512   512 41728      1524     6.888    0.4%
  512   512 42798      1581     13.87    0.8%
  512   512 43868      1555     10.26    0.6%
  512   512 44938      1573     10.76    0.6%
  512   512 46008      1604     6.333    0.3%
  512   512 47078      1653     16.73    1.0%
  512   512 48148      1617     5.912    0.3%
  512   512 49218      1636     9.173    0.5%
  512   512 50288      1781     13.84    0.7%
  512   512 51358      1705     14.47    0.8%
  512   512 52428      1766     12.89    0.7%

Quality and confidence:
param     error
c         0.021
d         0.021
s             0

Model:
Time ~=    357.4
    + c    0.083
    + d    1.014
    + s    0.014
              µs

Reads = 0 + (0 * c) + (0 * d) + (0 * s)
Writes = 0 + (0 * c) + (0 * d) + (0 * s)
Recorded proof Size = 0 + (0 * c) + (0 * d) + (0 * s)

@ByteNacked
Copy link
Member

In continuation of the statement that it is not necessary to consider the number of segments, it was decided to limit the maximum number of segments with some limit.

To assess this hypothetical limit, the number of data segments in programs in dapps/contracts was counted. As a result, it turned out that the number of data segments does not exceed 41 .

This bash script was used:

wasm_count_max_data_seg_in_dir.sh
#!/bin/bash

if [ "$#" -ne 1 ]; then
  echo "Usage: $0 <directory_path>"
  exit 1
fi

dir_path="$1"

if [ ! -d "$dir_path" ]; then
  echo "Error: Directory does not exist: $dir_path"
  exit 1
fi

max_seg_num=0

# Loop through all files with the .wasm extension in the directory
for file in "$dir_path"/*.wasm; do
  if [ -f "$file" ]; then
  	data_segment_num=$(wasm-objdump -h "${file}" | awk '/Data/ {print $NF}')
  	#echo "Output of $file: $data_segment_num"

  	if [ "$data_segment_num" -gt "$max_seg_num" ]; then
  		max_seg_num=$data_segment_num
  	fi
  fi
done

echo '*************************************************************'
echo "MAX NUMBER of data segments: $max_seg_num"
echo '*************************************************************'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants