How to speed up the Rust compiler in 2020
I have a question about LEB128. I use a similar format in my code, but with two changes:<p>1. It&#x27;s big endian.<p>2. The &quot;last byte&quot; bit is inverted (so it&#x27;s 00001 instead of 11110).<p>I changed the last byte flag so that it is easy to count the number of values in an array - you just do this:<p><pre><code>  let mut count = 0;
  for byte in bytes {
    count += byte &gt;&gt; 7;
  }
</code></pre>
And the reason I used big endian is because it makes decoding simpler. Encoding is more difficult but data is only ever encoded once, and is commonly decoded multiple times.<p><pre><code>  let mut value = 0;
  for byte in bytes {
    value = (value &lt;&lt; 7) | (byte &amp; 0x7F);
    if value &amp; 0x80 != 0 {
       return value;
    }
  }
</code></pre>
Vs the original implementation which has do to this basically:<p><pre><code>  let mut value = 0;
  let mut shift = 0;
  for byte in bytes {
    value |= (byte &amp; 0x7F) &lt;&lt; shift;
    if value &amp; 0x80 != 0 {
      return value;
    }
  }
</code></pre>
Here is the generated assembly in both cases: <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;6FbX_E" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;6FbX_E</a><p>No idea if it would be faster than the new code but surely it is faster than the old code?