<story><title>Making Rust as Fast as Go</title><url>https://www.christianfscott.com/making-rust-as-fast-as-go/</url></story><parent_chain><item><author>molf</author><text>The Rust version uses `target.chars().count()` to initialise the cache, while the Go version counts up to `len(target)`. These are not equivalent: the Rust version counts Unicode code points, the Go version counts bytes.&lt;p&gt;I am confused by the implementations, although I have not spent any time testing them. Both versions contain a mix of code that counts bytes (`.len()` and `len(...)`) and Unicode code points (`chars()` and `[]rune(...)`). My guess is that the implementation might not work correctly for certain non-ASCII strings, but I have not verified this.&lt;p&gt;Of course, if only ASCII strings are valid as input for this implementation then both versions will be a lot faster if they exclusively operate on bytes instead.</text></item></parent_chain><comment><author>eis</author><text>Yep.&lt;p&gt;Here a Go playground example showing that the result is indeed wrong:&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;play.golang.org&amp;#x2F;p&amp;#x2F;vmctMFUevPc&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;play.golang.org&amp;#x2F;p&amp;#x2F;vmctMFUevPc&lt;/a&gt;&lt;p&gt;It should output 3 but outputs 5 because each ö is two bytes, len(&amp;quot;föö&amp;quot;) = 5.&lt;p&gt;I would suggest using &amp;quot;range&amp;quot; to iterate over the unicode characters.</text></comment>
<story><title>Making Rust as Fast as Go</title><url>https://www.christianfscott.com/making-rust-as-fast-as-go/</url></story><parent_chain><item><author>molf</author><text>The Rust version uses `target.chars().count()` to initialise the cache, while the Go version counts up to `len(target)`. These are not equivalent: the Rust version counts Unicode code points, the Go version counts bytes.&lt;p&gt;I am confused by the implementations, although I have not spent any time testing them. Both versions contain a mix of code that counts bytes (`.len()` and `len(...)`) and Unicode code points (`chars()` and `[]rune(...)`). My guess is that the implementation might not work correctly for certain non-ASCII strings, but I have not verified this.&lt;p&gt;Of course, if only ASCII strings are valid as input for this implementation then both versions will be a lot faster if they exclusively operate on bytes instead.</text></item></parent_chain><comment><author>steveklabnik</author><text>Yes, especially because that changes it from constant time (strings know their length in bytes) to linear time (counting chars means a loop through the text.)&lt;p&gt;I was a bit suspicious of the conclusion, but didn’t dig in myself. I imagine this would be a much larger source of difference.</text></comment>