[libcxx-commits] [libcxx] [libc++] Optimize fstream::read (PR #165223)

Jinsong Ji via libcxx-commits libcxx-commits at lists.llvm.org
Sat Nov 8 20:53:09 PST 2025


================
@@ -308,6 +308,19 @@ protected:
     return basic_streambuf<_CharT, _Traits>::xsputn(__str, __len);
   }
 
+  _LIBCPP_HIDE_FROM_ABI_VIRTUAL streamsize xsgetn(char_type* __str, streamsize __len) override {
+    if (__always_noconv_) {
+      const streamsize __n = std::min(this->egptr() - this->gptr(), __len);
+      if (__n != 0) {
+        traits_type::copy(__str, this->gptr(), __n);
+        this->__gbump_ptrdiff(__n);
+      }
+      if (__len - __n >= this->egptr() - this->eback())
+        return std::fread(__str + __n, sizeof(char_type), __len - __n, __file_);
----------------
jsji wrote:

We are seeing regressions with this commit. Applications failing with message like:
```InvalidWordCount:  WordCount exceeds remaining input stream size: expected size = 47564 bytes, remaining size = 544 bytes```

Should we return `total number of characters read` not only the number of characters read by `fread` here?
Also should we return __n; after the if?

https://github.com/llvm/llvm-project/pull/165223


More information about the libcxx-commits mailing list