STL icon indicating copy to clipboard operation
STL copied to clipboard

`<xiosbase>`: `std::ios_base::sync_with_stdio` has no effect in VS

Open fsb4000 opened this issue 2 years ago • 4 comments

Describe the bug

std::ios_base::sync_with_stdio has no effect in the STL but cout becomes much faster after calling sync_with_stdio on mingw + libstdc++ .

#include <cstdio>
#include <ctime>
#include <iostream>
using namespace std;
int main() {
    ios::sync_with_stdio(false);

    clock_t start, end;
    start = clock();

    for (int i = 0; i < 1e+4; i++) {
        cout << i;
    }

    end = clock();
    cout << endl;
    cout << end - start << endl;
    return 0;
}

Expected behavior

sync_with_stdio should give some performance.

STL version

Microsoft Visual Studio Community 2022
Version 17.6.0 Preview 3.0

Additional context

Devcom-10347093

I suggested to the user to add setvbuf(stdout, nullptr, _IOLBF, 16384); after sync_with_stdio but this optimization is still missing.

fsb4000 avatar Apr 24 '23 13:04 fsb4000

FWIW, I believe std::ios_base::sync_with_stdio has no effect in libc++ either (see also LLVM-21192).

I'm not sure whether this is by design.

frederick-vs-ja avatar Apr 24 '23 16:04 frederick-vs-ja

But this problem has nothing to do with syn_with_stdio, it is caused by stdour not buffering. This means that functions such as printf will also be affected and the output will be slower.

heheda123123 avatar Apr 25 '23 07:04 heheda123123

@heheda123123 I believe we cannot enable buffering by default: https://github.com/microsoft/STL/issues/605#issuecomment-881972414 But I think we can enable buffering for cout, wcout, etc when ios::sync_with_stdio(false); is called

fsb4000 avatar Apr 25 '23 12:04 fsb4000

@barcharcraz made the excellent point that we totally don't care about ancient msvcrt.dll behavior on Windows, so it would be a useful comparison to see how MinGW using the modern ucrtbase.dll behaves - this would be a relevant comparison for us as we use the same UCRT.

StephanTLavavej avatar Jul 24 '24 21:07 StephanTLavavej