Memory allocation in Javascript when consuming a serialized value from Rust

35 Views Asked by At

Using prost I build this serialized type for my SecretKey

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(default, rename_all = "camelCase")]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct SecretKey {
    #[prost(bytes = "vec", tag = "1")]
    pub coefficients: ::prost::alloc::vec::Vec<u8>,
}

In order to consume that inside a web app function, I declare the following function

#[wasm_bindgen]
pub fn state0_bindgen() -> JsValue {

    let secret_key = state0();

    serde_wasm_bindgen::to_value(&secret_key).unwrap()
}

Here's a guide that explains how types are converted from Rust to JS using serde_wasm_bindgen.

wasm-pack allows me to compile this function to wasm (using web as target) and eventually consume it inside a HTML file

<!doctype html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <title>mp-psi example</title>
  </head>
  <body>
    <script type="module">
      import init, { state0_bindgen } from "./mp_psi.js";

      init().then(() => {
        const sk = state0_bindgen();
      });
    </script>
  </body>
</html>

Now I'm trying to benchmark how much memory is allocated for the serialized sk array in Javascript. The rust type of coefficients is Vec<u8>. Is it being serialized as a Uint8Array? Can you help me determine how much memory is allocated to this array?

0

There are 0 best solutions below