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.

Unbuffered I/O Can Make Your Rust Programs Much Slower

45 pointsby akbarnamaover 3 years ago

10 comments

otterleyover 3 years ago
Unbuffered I&#x2F;O is slower than buffered I&#x2F;O regardless of language. It’s not just a Rust phenomenon.<p>Making syscalls like write() every time you want to send even a character of output down a stream is much more latent than just sticking bytes in a buffer.<p>In general, the only time anyone should use unbuffered output is when there’s a real person consuming it - for example, when you’re prompting a user for feedback or input.
评论 #29675309 未加载
评论 #29675098 未加载
评论 #29676188 未加载
评论 #29677793 未加载
PaulDavisThe1stover 3 years ago
Alternative Title: Many Problems That Have Affected Software Development Since Denis Ritchie Roamed the Steppes Continue to Affect Rust Programming
评论 #29675636 未加载
halayliover 3 years ago
Unbuffered I&#x2F;O can make any language you use much slower. It has nothing to do with Rust &#x2F; language choice.
评论 #29679303 未加载
评论 #29676856 未加载
scottlambover 3 years ago
I&#x27;m surprised serde_json takes a std::io::Read rather than a std::io::BufRead. I wonder why they made this choice.<p>Not only does using plain Read make this mistake possible, but it also means they have to be doing byte-by-byte processing. [1] So not really possible to do SIMD-oriented parsing acceleration like simdjson uses. (I gather simdjson&#x27;s approach is quite sophisticated, but a simpler version is to scan for the next delimiter via a memchr2 on &quot; and \, then have a routine that validates the characters until then are valid via SIMD methods.)<p>[1] or do larger reads into their own buffer. But they&#x27;re clearly not doing that given this strace output. And it&#x27;d be redundant when folks do use BufRead.
评论 #29675863 未加载
评论 #29676529 未加载
qalmakkaover 3 years ago
I kind of understand why Rust made buffered IO explicit and on-demand instead of going down the C route and making it the default, but still I think there&#x27;s a significant risk lots of programs are going to be suboptimal due to developer ignorance or the expectation of having buffered IO by default like with stdio and iostreams.<p>It would have been IMHO probably less clean but safer to make the standard IO constructs like File buffered by default (which is what users expect in the vast majority of cases) and provide on the side unbuffered variants. Thankfully modern OSes with caches and stuff somewhat mitigate this thing, but it can make IO on less advanced systems way more problematic than it should be.
评论 #29675418 未加载
dmw_ngover 3 years ago
Alternative title: Poor Interface Design Makes Most Rust Programs Much Slower<p>That programmers need to consider this stuff on each new project is a failure of the standard library design, it&#x27;s a problem not even C++ suffers from
评论 #29675534 未加载
评论 #29675675 未加载
评论 #29675558 未加载
评论 #29675602 未加载
评论 #29675530 未加载
FpUserover 3 years ago
&gt;&quot;In this post, we will look at a common source of bad performance on Rust code that can trip up even veteran developers, and what you can do when this happens in your programs.&quot;<p>&quot;Veteran developers&quot;, the sort of who would use Rust after having worked in C&#x2F;C++&#x2F;etc would know very well that buffering on very granular IO is a must except some specific situations. Otherwise they&#x27;re anything but. And it has nothing to do with Rust. This is of course when IO libraries used do not already take care of that.
评论 #29676462 未加载
ericbarrettover 3 years ago
I started a Rust project several months ago and quickly became frustrated with the state of basic terminal libraries. None of them seemed to implement the buffering and minimal-refresh of ncurses, so I was forced to rely on one of the many shifting ncurses binding libraries and all the unsafeness that comes with that (or reimplement my own—way out of scope for the little toy I was building). What&#x27;s the state of the art for raw terminal libraries (not TUI helpers) in Rust nowadays?
评论 #29675555 未加载
lor_louisover 3 years ago
I am not all that knowledgeable about rust but I think that if you really want to decrease the numbers of system calls when interacting with files, writev and readv should be considered. I wonder if BuffWriter&#x2F;Reader could be made to use it, as it would have the same &quot;atomic&quot; behaviour and would forgo the need to concat strings. Still, the char*s could become invalidated once the system call is made.
评论 #29678292 未加载
评论 #29675781 未加载
评论 #29675575 未加载
eskaover 3 years ago
Multiple of your “senior” developers didn’t catch this in a “senior” developer’s PR? Have they ever read any of the IO documentation, since this is pointed out literally everywhere in the docs?<p><a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;fs&#x2F;struct.File.html" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;fs&#x2F;struct.File.html</a><p><a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;io&#x2F;trait.Read.html" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;io&#x2F;trait.Read.html</a>
评论 #29676339 未加载
评论 #29677391 未加载
评论 #29676080 未加载