<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: starsoccer</title>
    <description>The latest articles on DEV Community by starsoccer (@starsoccer).</description>
    <link>https://dev.to/starsoccer</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F636911%2Fe5a63bc2-7667-4f68-adb4-01df562d8141.png</url>
      <title>DEV Community: starsoccer</title>
      <link>https://dev.to/starsoccer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/starsoccer"/>
    <language>en</language>
    <item>
      <title>From Javascript to Rust to WASM</title>
      <dc:creator>starsoccer</dc:creator>
      <pubDate>Mon, 14 Jun 2021 22:19:17 +0000</pubDate>
      <link>https://dev.to/starsoccer/from-javascript-to-rust-to-wasm-8ci</link>
      <guid>https://dev.to/starsoccer/from-javascript-to-rust-to-wasm-8ci</guid>
      <description>&lt;p&gt;For a while Ive been interested in not only Rust but WASM but having limited familiarity with many of the languages that can be compiled to WASM I never really had a chance to try it out until recently. Over the last few months though I got the opportunity to learn Rust.&lt;/p&gt;

&lt;p&gt;Rust in my opinion is very much like typescript on steroids. While Typescript may enforce types in your code if you happen to pass a string to a type expecting a number things will still compile and may even work as expected still. With Rust this is not the case. If you provide an unexpected type either things will not compile in the first place or your software will crash. &lt;/p&gt;

&lt;p&gt;A personal project of mine (&lt;a href="https://github.com/starsoccer/cryptotithe"&gt;Cryptotithe&lt;/a&gt;) which is an open source tax software for cryptocurrencies was something I always though would benefit from WASM since it has some computation heavy parts. While I would not say it is extremely resource or computation heavy calculating gains does need a little bit of basic math. There is also a need to do some searches with in arrays depending on the users selection of alternative types of accounting such as LIFO, HCFO(Highest Cost First Out), etc.. which can increase the amount of calculations being done.&lt;/p&gt;

&lt;p&gt;So, a few weeks back I decided to try converting the heaviest parts into rust and then using wasm-bindgen convert it wasm for use in my typescript project. While creating a basic Rust project was easy moving building the WASM package and linking things proved to be the first challenge. &lt;/p&gt;

&lt;p&gt;My project has a few different functions but overall has a straightforward path of functions that more or less all rely on one another which is broken down below. The end goal being to convert all of them to Rust.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
                                           ┌─────────────────┐
                                           │                 │
                                      ┌────┤ calculate_gains │
                                      │    │                 │
       ┌──────────────────────────┐   │    └────────┬────────┘
       │                          │   │             │
       │ add_to_currency_holdings │◄──┤             │
       │                          │   │             │
       └──────────────────────────┘   │     ┌───────▼───────┐
                                      │     │               │
                                      └─────┤ process_trade │
                                            │               │
                                            └───────┬───────┘
                                                    │
                                                    │
                                                    │
   ┌───────────────────────────────┐      ┌─────────▼─────────┐
   │                               │      │                   │
   │ check_currency_holding_amount │◄─────┤ holding_selection │
   │                               │      │                   │
   └───────────────────────────────┘      └─────────┬─────────┘
                                                    │
                                                    │
                                                    │
                                         ┌──────────▼───────────┐
                                         │                      │
                                         │ get_currency_holding │
                                         │                      │
                                         └──────────────────────┘

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Gotchas
&lt;/h1&gt;

&lt;p&gt;While wasm-bindgen has support for automatically generating typescript types, in general there are some common gotchas. &lt;/p&gt;

&lt;p&gt;One of the biggest gotchas is that &lt;code&gt;u32&lt;/code&gt; are converted to regular typescript numbers but &lt;code&gt;u32&lt;/code&gt; are actually smaller.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// this is not valid
let num: u32 = 1621867244484;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This might not seem like a big deal but if your dealing with numbers on the higher end of this specturm it quickly becomes an issue. This means a &lt;code&gt;u64&lt;/code&gt; has to be used, but sadly this means the typescript interface wasm-bindgen generates will have this as a &lt;code&gt;BigInt&lt;/code&gt; instead of a &lt;code&gt;number&lt;/code&gt;. This simply pushes the complexity to the javascript side though.&lt;/p&gt;

&lt;p&gt;After trying a few different ways I could not find a great solution that didn't involve a lot of extra boilerplate code. In the end I personally found it easier to simply give up on having correct typescript types and instead accepted that &lt;code&gt;Any&lt;/code&gt; were going to be there.&lt;/p&gt;

&lt;p&gt;While not specifically a wasm-bindgen issue debugging wasm can be quite a challenge. Perhaps this is due to the way I was converting types or maybe there are tools Im not aware of that make this easier. The majority of time there was an issue I basically got a standard unreachable code error which would link to some wasm that was not at all helpful.&lt;/p&gt;

&lt;p&gt;Solving issues like this basically became a guessing game to see where exactly it stopped working and then try to backtrack to understand the why.&lt;/p&gt;

&lt;p&gt;One helpful way of debugging is by logging right in your wasm code which wasm-bindgen natively supports&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use web_sys::console;
console::log_2(&amp;amp;"Logging arbitrary values looks like".into(), &amp;amp;some_variable_here);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The best part about using console log in rust is you can also log javascript objects passed directly into rust relatively easily by simply first converting them to a JSValue as seen below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use web_sys::console;
console::log_2(&amp;amp;"Logging arbitrary values looks like".into(), &amp;amp;JsValue::from_serde(&amp;amp;some_variable_here).unwrap());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Slow Data Transfer
&lt;/h1&gt;

&lt;p&gt;While not a gotcha, one thing to be aware of is that transferring complex types between Javascript and WASM can be slow. This means its often not worth it to simply pass an object to WASM for one or two small computations. If you can simply pass a number instead it may be significantly faster, but in scenarios where thats not an option, WASM may actually be slower. This means when planning to convert some area of your code to WASM you should first investigate what data would be passed around and how much you may need to rewrite in order to reap the benefits.&lt;/p&gt;

&lt;p&gt;I originally started working by simply converting the bottom most function in my project, &lt;code&gt;get_currency_holding&lt;/code&gt; and exposing it as a proof of concept. As a proof of concept this was great, but it was significantly slower.&lt;/p&gt;

&lt;p&gt;The slowness made sense since &lt;code&gt;holding_selection&lt;/code&gt;, the function that calls &lt;code&gt;get_currency_holding&lt;/code&gt; does so repeatably possibly multiple times per trade. This made it clear to me that I needed to rewrite this function as well which began a snowball effect. First &lt;code&gt;holding_selection&lt;/code&gt; but that requires calling &lt;code&gt;check_currency_holding_amount&lt;/code&gt;; But still to slow since &lt;code&gt;holding_selection&lt;/code&gt; is simply called repeatably per trade by &lt;code&gt;process_trade&lt;/code&gt;. &lt;code&gt;process_trade&lt;/code&gt; requires &lt;code&gt;add_to_currency_holdings&lt;/code&gt;. &lt;code&gt;process_trade&lt;/code&gt; though is repeatably called by &lt;code&gt;calculate_gains&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Its only at this final function &lt;code&gt;calculate_gains&lt;/code&gt; where the speed benefits became clear and the whole conversion ended up being worth it since this function is called one and only has a one time transfer cost typically. &lt;/p&gt;

&lt;h1&gt;
  
  
  Results
&lt;/h1&gt;

&lt;p&gt;Overall I would consider the work a success as it took the time to execute on a personal data file of mine from ~130ms to less then 10ms. A 10x improvement. I have yet to push this new WASM powered version live quite yet as I need to do some clean things up a bit but you can take a look at the rust version here, &lt;a href="https://github.com/starsoccer/cryptotithe-rs"&gt;CryptoTithe-RS&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>rust</category>
      <category>webassembly</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
