TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A Rust optimization story

192 pointsby francoismassotover 3 years ago

11 comments

colatkinsonover 3 years ago
For what it&#x27;s worth, it appears a C++ version [0] of that binary search algorithm with Clang 13 requires 9.45 cycles [1]. The clang++-generated ASM is quite similar to the rustc-generated ASM in the article. Not sure what accounts for the difference from the C++ implementations the author was comparing against.<p>Regardless, it&#x27;s neat to see the two languages are so close in performance here. I wonder if down the line, the richer information about lifetimes, etc. that Rust provides will allow optimizations beyond those available to C++ -- or if this is already the case.<p>[0] <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;Mj7PWevex" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;Mj7PWevex</a> [1] <a href="https:&#x2F;&#x2F;bit.ly&#x2F;3CawbUe" rel="nofollow">https:&#x2F;&#x2F;bit.ly&#x2F;3CawbUe</a>
评论 #28968445 未加载
superjanover 3 years ago
I did not know there was a CPU instruction timing simulator online!<p><a href="https:&#x2F;&#x2F;uica.uops.info&#x2F;" rel="nofollow">https:&#x2F;&#x2F;uica.uops.info&#x2F;</a>
评论 #28969622 未加载
评论 #28967954 未加载
mwcampbellover 3 years ago
I wonder what critics of optimizer-oriented programming, such as Marcel Weiher (mpweiher), think of this post. Weiher has been repeatedly critical of Swift for relying way too much on LLVM&#x27;s optimizations; I wonder if Rust, with its emphasis on zero-overhead abstraction, falls into the same category for him.
评论 #28967745 未加载
mjw1007over 3 years ago
Idle question: how did we end up with the term &quot;inverted index&quot; for this sort of thing?<p>The term &quot;index&quot; is used because of an analogy to the index of a book. But the so-called &quot;inverted&quot; index is the same way round as a book&#x27;s index: you look up a word and it gives something analogous to page numbers.
评论 #28969185 未加载
评论 #28968265 未加载
评论 #28968088 未加载
AlexanderTheGr8over 3 years ago
I am not very experienced with SIMD but wouldn&#x27;t a SIMD-based binary search be faster?<p>In this particular case (assuming SIMD can make 4 comparisons at once), we have an array with 128 elements. So we can compare the needle with indices {24, 48, 72, 96} in one CPU cycle; so we narrow down to approx 24 elements in 1 cycle. Repeat this until we get the answer.<p>This^ is a very approximate idea. There are a lot of edge-cases and things to consider. But couldn&#x27;t we solve this problem in 4-5 cycles with SIMD binary search?
评论 #28970416 未加载
aydwiover 3 years ago
Excellent writeup, very succinct and easy to follow.
sbt567over 3 years ago
Nice writeup! especially on the explanation of low level asm stuff
brundolfover 3 years ago
Pretty interesting that a branchless linear search would be faster than a branching binary search! It makes sense, it just goes against the normal intuition. Just goes to show that benchmarking is always important
评论 #28974342 未加载
aksxover 3 years ago
So many interesting findings in the post.<p>The tantivy binary search &amp; rust binary search only has one different at the end, knowing the size of the slice ahead of time, right?<p>Could the rust compiler infer the size ahead of time?
评论 #28976027 未加载
secondcomingover 3 years ago
I assume that the lack of cmov generation affects C++ too with clang?
评论 #28967971 未加载
jdubover 3 years ago
If the author is reading, the phrase,<p>&gt; Please follow me in my rabbit hole.<p>... means something rather different to what you intended, which is probably,<p>&gt; Please follow me down the rabbit hole.<p>But thank you for the giggle. :-)
评论 #28967660 未加载
评论 #28968190 未加载
评论 #28967318 未加载