wrap-cli icon indicating copy to clipboard operation
wrap-cli copied to clipboard

Rust Codegen Uses Too Many Mutable References

Open dOrgJelli opened this issue 3 years ago • 1 comments

Describe the bug Currently the Rust codegen uses too many mutable references.

Please provide the steps to reproduce and if possible a minimal demo of the problem. See this demo, where we need to make a copy of a string, since it is being passed as a mutable reference. This should not be needed: https://github.com/polywrap/demos/blob/a5c0b753bd47086a1485fe30edb5a2e2b92b02d4/common-interface/wrapper-b/src/lib.rs#L8-L20

Expected behavior Able to pass values as non-mutable references.

dOrgJelli avatar Aug 29 '22 17:08 dOrgJelli

based on convs with jordan, game plan should be:

  • we should in fact NOT use & reference passing when using scalar types (u32|16|8, i32|16|8). This is because when you're passing by reference & you're passing a pointer to the value in memory, which is 32 bits. This is not efficient when passing values that are less than 32 bits in size (ex: u16|8).
  • Based on the above point, let's remove reference passing in all methods that accept scalar types. We should however keep it for larger types like String, Map, Bytes, Array, etc.
  • Looks like we're not needlessly passing mut & mutable references anywhere, so the code actually looks really good in that regard. I don't think we have to do anything else really...

tl;dr: we SHOULD NOT use & reference passing when using scalar types (u32|16|8, i32|16|8) and we SHOULD be passing complex types like AnotherType into functions as reference &. This way we don't make copies of it on the stack every time we pass it to a function. It's more efficient this way, since the pointer is 32 bits

cbrzn avatar Sep 02 '22 12:09 cbrzn