d2 file input performance

Steven Schveighoffer schveiguy at yahoo.com
Fri Aug 26 11:10:18 PDT 2011


On Fri, 26 Aug 2011 13:43:23 -0400, Christian Köstlin  
<christian.koestlin at gmail.com> wrote:

> Hi guys,
>
>
> i started the thread:  
> http://stackoverflow.com/questions/7202710/fastest-way-of-reading-bytes-in-d2  
> on stackoverflow, because i ran into kind of a problem.
>
> i wanted to read data from a file (or even better from a stream, but  
> lets stay with file), byte-by-byte. the whole thing was part of my  
> protobuf implementation for d2, and there you have to look at each byte  
> to read out the varints. i was very proud of my implementation until i
> benchmarked it first against java (ok ... i was a little slower than  
> java) and then against c++ (ok ... this was a complete different game).
>
> after some optimizing i got better, but was still way slower than c++.  
> so i started some small microbenchmarks regarding fileio:  
> https://github.com/gizmomogwai/performance in c++, java and d2.
>
> could you help me improve on the d2 performance? i am sure, that i am  
> missing something fundamental, because i thing it should be at least  
> possible be equal or better than java.
>
> thanks in advance

Two things:

First, there is a large difference:

C++ version:

  int read() {...}

D version:

int read(ubyte *bufferptr) {...}

This may not be optimized as well.  You should make it the same.

Second, use -inline, it will help tremendously.

I'd bet money that the largest slowdown is the function calls.  Inlining  
makes things run so much faster it's not even funny.

Also, note that FILE* is *already buffered*, there is no reason to do  
anything but fgetc.  In fact, it would probably be faster.

-Steve


More information about the Digitalmars-d-learn mailing list