Skip to content

<fstream>: Non-char types are 30 to 50x slower when reading/writing than regular char #817

@jessey-git

Description

@jessey-git

Describe the bug
Reading and writing to ifstream/ofstream is 30 to 50x slower when using uint8_t or std::byte vs. using regular char.

Attached is a small repro program demonstrating the poor behavior.

This caught me off guard in a project of mine where I attempted to do the "right" thing and was swiftly penalized for it. I tried to represent a buffer as a vector<uint8_t> rather than vector<char> and suddenly reading/writing that buffer became enormously slow. This was just before std::byte became available. I worked around it using reinterpret_cast<char *>

Now that std::byte is well enough into the ecosystem I decided to test again to see if that's affected as well and it sure is. I can see this becoming a large issue if folks actually embrace the byte in modern, idiomatic c++.

Compiled Version 16.6.0 Preview 6.0

Machine 1:

Write to ofstream<char> 0.9732 ms
Write to ofstream<uint8_t> 43.1419 ms
Write to ofstream<std::byte> 45.2306 ms
Read from ifstream<char> 1.0306 ms
Read from ifstream<uint8_t> 43.053 ms
Read from ifstream<std::byte> 43.2853 ms

Machine 2:

Write to ofstream<char> 0.8127 ms
Write to ofstream<uint8_t> 34.6434 ms
Write to ofstream<std::byte> 34.0428 ms
Read from ifstream<char> 0.9546 ms
Read from ifstream<uint8_t> 32.4374 ms
Read from ifstream<std::byte> 31.9037 ms

Command-line test case
Build and run the attached vs solution in x64 Release mode

Expected behavior
Equivalent performance for all similar data types char, uint8_t, and std::byte

STL version
16.6.0 Preview 6.0

SlowIO.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixedSomething works now, yay!performanceMust go faster

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions