Skip to content

Instantly share code, notes, and snippets.

@BruJu
Last active June 11, 2020 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BruJu/e7bef929496345afc04a06cbb67704b6 to your computer and use it in GitHub Desktop.
Save BruJu/e7bef929496345afc04a06cbb67704b6 to your computer and use it in GitHub Desktop.
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