D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7033 - File.rawWrite is slow on Windows
Summary: File.rawWrite is slow on Windows
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: industry, performance, pull
Depends on:
Blocks:
 
Reported: 2011-11-29 19:00 UTC by bearophile_hugs
Modified: 2020-08-15 10:07 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-11-29 19:00:04 UTC
This D2 program runs in about 5.13 seconds on my PC, on Windows:


import std.stdio;
void main() {
    auto f = File("bytes_test.dat", "wb");
    ubyte[3] RGB;
    foreach (_; 0 .. 1_000_000)
        f.rawWrite(RGB);
}



While this C program runs in about 0.14 seconds:


#include <stdio.h>
int main() {
    FILE *f = fopen("bytes_test.dat", "wb");
    unsigned char RGB[3] = {0};
    int i;
    for (i = 0; i < 1000000; i++)
        fwrite(RGB, 1, 3, f);
    return 0;
}


See also:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=30858

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=30866
Comment 1 Orvid King 2014-08-03 22:28:17 UTC
The reason for this is because rawWrite calls flush, not once, but twice every 
time you call it, on the underlying file. This is absolutely absurd, and is a 
massive performance bottleneck, especially when it's not needed because the 
file may very well have already been opened for writing in binary mode to begin 
with. In this particular example, it's flushing to disk every 3 bytes.
Comment 2 berni44 2019-12-06 13:42:06 UTC
Just a note: I don't know, if this has also been an issue using linux, but nowadays it works on linux.
Comment 3 Steven Schveighoffer 2020-07-27 15:16:10 UTC
Just was doing some development and found this same bug. This is absolutely unnecessary, especially if the file is already open in binary mode. We currently don't store what mode the file is in, but we absolutely could (we are allocating a heap struct for this, no reason we can't store that). And only switch if necessary.

This should be an easy change and I'm kind of surprised this has been open for so long.
Comment 4 Dlang Bot 2020-08-13 06:45:36 UTC
@canopyofstars created dlang/phobos pull request #7590 "Fix issue 7033: File.rawWrite is slow on Windows" fixing this issue:

- Fix issue 7033

https://github.com/dlang/phobos/pull/7590
Comment 5 Dlang Bot 2020-08-15 10:07:44 UTC
dlang/phobos pull request #7590 "Fix issue 7033: File.rawWrite is slow on Windows" was merged into master:

- 8643ecad370508f6adf7ba2a34eee1924e47a2f7 by starcanopy:
  Fix issue 7033

https://github.com/dlang/phobos/pull/7590