microsoft / microsoft/STL

basic_filebuf::setbuf does only work with binary mode

Open
#2,646 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

I observe that basic_filebuf::setbuf only actually respects the manual buffering (using the provided buffer), if std::ios::binary is set, otherwise a call to setbuf is ignored. For testing this I captured a WPA trace of the following Code, with and without std::ios::binary set and looked at the "Storage > Size by Process, Stack for Read/Write" report for the test executable. Sadly we don't have strace on Windows ;) ... so the repro and testing is a bit more involved

#include <fstream>

int main() {
  size_t constexpr buf_size = 1024 * 1024;
  auto buf = std::make_unique<char[]>(buf_size);
  std::ofstream test("test.txt", std::ios::binary);
  test.rdbuf()->pubsetbuf(buf.get(), buf_size);
  for (int i = 0; i < 10 * 1024 * 1024; ++i) {
    test << "test";
  }
}

with std::ios::binary:
image
47 Writes/Reads of 1MiB each on Avg. (this is what one would hope/expect)

without std::ios::binary:
image
9.777 Writes/Reads of ~5KiB each on Avg. (this one not)

Expected behavior
I would expect basic_filebuf::setbuf to sets the buffering in any case.

STL version

Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.1.3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by building the supplied C++ reproducer with and without std::ios::binary, then compare the WPA Storage > Size by Process, Stack for Read/Write results. Investigate basic_filebuf::setbuf behavior in the MSVC STL; done when the provided buffer is respected in text mode as well as binary mode.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.