Code speed (and back to the memory leaks...)

Joseph Wakeling joseph.wakeling at webdrake.net
Fri Apr 16 04:00:30 PDT 2010


bearophile wrote:
> You are right, sorry.

No need to apologise!  You helped me significantly improve my code,
helped me understand D a lot better and left me feeling generally very
positive about developing further in D.  I'd call that a result. :-)

>> So I think it's probably just compiler difference that's to blame for speed differences<
> 
> This can be true, but such differences are not magic, they can be found, and eventually they can even become precise enhancement requests for llvm developers, they are open to such requests.

I hope my questions here have been useful in this respect ...

>> After all, D in general and DMD in particular is in development.<
> 
> DMD has a quite old back-end that is not in active development. Maybe it will become 64 bit.

The DMD backend seems a generally problematic piece of code given the
proprietary restrictions that apply to it.  I have the impression that
it's being used simply because it's the backend that is known and tested
and that over the longer term there may be a switch ... ?

The one compiler-related concern I have is whether there will be an
effective free-as-in-freedom compiler for D2 in the near future.  Work
is going on GDC but it's moving slowly -- and I don't see a clear LDC
roadmap to D2 support.

> In this program I have seen that a good percentage of the speed difference between ldc/dmd is caused by foreach loops. When you iterate over structs longer than size_t use ref (or in D2 better ref const(...)).
> foreach (Rating r; ratings)
> ==>
> foreach (ref const(Rating) r; ratings)
> 
> There is nothing algorithmically strange going on here, but to be sure you can take a look at the produced asm.

Interesting ... !  I'm afraid the asm is over my head, but I will read
up and try to understand it.

> To find why the C++ code is faster you can show me the equivalent C++
> code that I can compile myself, or you can compile your C++ code and
> show me the asm of the two functions that are hot spots.

Sure -- attached.  The key functions are the ones in tref.cpp (the
smalltref class) and trefsim.cpp, and the treftest.cpp file that runs a
roughly-equivalent simulation to test.d.

It's a little embarrassing to share because it's fairly badly-designed
code -- my first serious C++ attempt -- it served its purpose some time
ago and was done with.  It started to become vaguely relevant again so I
thought I'd try and redo it with a better design, and rather than do it
in C++ (which I could do much better by now) this seemed like a nice
excuse to build my first D project ... :-)

I already made a couple of tweaks based on your improvements to the D
code, but I think g++ probably already has such optimizations during the
compile process.

I also realised that I wasn't comparing like with like -- the code was
using the RanLux random number generator, and with mt19937 it shaves
quite a bit of time off.  So C++ still has a bit of an edge right now.

There's surely a lot of other stuff that can be done to improve this
C++, but please don't put lots of effort in unless it helps with
improving D (the language and/or frontend, not my code, which is not so
important:-).

Thanks & best wishes,

    -- Joe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tref.hpp
Type: text/x-c++hdr
Size: 2409 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100416/255172b9/attachment-0002.hpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tref.cpp
Type: text/x-c++src
Size: 5854 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100416/255172b9/attachment-0002.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: trefsim.hpp
Type: text/x-c++hdr
Size: 8683 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100416/255172b9/attachment-0003.hpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: treftest.cpp
Type: text/x-c++src
Size: 1249 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100416/255172b9/attachment-0003.cpp>


More information about the Digitalmars-d-learn mailing list