Skip to content

Instantly share code, notes, and snippets.

@BruJu
Last active Jun 11, 2020
Embed
What would you like to do?
Exporting numbers from Rust to Wasm

Initial problem : I tried to export to wasm different complex structures. Time benchmarks were strange and in linear time for every structure.

So I tried to export vectors of numbers

Exporting a vector of 6

   let mut v = Vec::new();

   for i in 0..10000000 {
       v.push(6);
   }
   
   // export v

0.10 sec

With an intermediate vector to store 6

   let mut v = Vec::new();

   for i in 0..10000000 {
       let mut vv = Vec::new();
       vv.push(6);
       v.push(vv[0]);
   }
   
   // export v

0.47 sec

Vectors of vectors

   let mut v = Vec::new();

   for i in 0..10000000 {
       let mut vv = Vec::new();
       vv.push(6);
       v.push(vv);
   }
   
   // export v

0.65 sec

Conclusion

Exporting probably forces wasm to deep copy the exported struct

This test is bad because sizeof(vec) >>>>> sizeof(int)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment