Bug 7033 – File.rawWrite is slow on Windows

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2011-11-29T19:00:04Z
Last change time
2020-08-15T10:07:44Z
Keywords
industry, performance, pull
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-11-29T19:00:04Z
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 by blah38621 — 2014-08-03T22:28:17Z
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 by bugzilla — 2019-12-06T13:42:06Z
Just a note: I don't know, if this has also been an issue using linux, but nowadays it works on linux.
Comment #3 by schveiguy — 2020-07-27T15:16:10Z
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 by dlang-bot — 2020-08-13T06:45:36Z
@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 by dlang-bot — 2020-08-15T10:07:44Z
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