www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Am I do something wrong

reply "Eric Suen" <eric.suen.tech gmail.com> writes:
Hi,

  I write a very simple program, two version,
one is write in D, and others is using C, the D version
is extreme slow, the D version using 450 ms, but C version
using 0 ms, can anybody tell me is there anything wrong
in the code?

Here is the code:
http://files-upload.com/files/499789/test.zip

Thanks

Eric
Sep 13 2007
parent reply Christopher Wright <dhasenan gmail.com> writes:
Eric Suen wrote:
 Hi,
 
   I write a very simple program, two version,
 one is write in D, and others is using C, the D version
 is extreme slow, the D version using 450 ms, but C version
 using 0 ms, can anybody tell me is there anything wrong
 in the code?
 
 Here is the code:
 http://files-upload.com/files/499789/test.zip
The compile time is also insanely long. If you just remove 'const' from those giant arrays (you'll have to assign base_action in your static constructor), you get a giant speed boost, both in compilation and in run time. Looking at the assembly, constant expansion means that your tAction function maps to 28000 lines of assembly if you have const arrays, or about fifteen if you have non-const arrays. Constant expansion of arrays is an optimization for small arrays, but this is slightly ridiculous. But the speed improvement from omitting 'const' puts the D version on par with the C. And if you want to get both the speed and the constness, I think you can have the giant array literals be private and mutable, and then expose some public const arrays that your static constructor sets as references to the private ones. (You can certainly do it in D2. Not sure about D1.)
Sep 13 2007
parent reply "Eric Suen" <eric.suen.tech gmail.com> writes:
D not even a dynamic language, why const will affect runtime
performance? besides, isn't all the LALR parser will using
giant const arrays? for examples: seatd(http://seatd.mainia.de/)
has a huge file called parser.d...

"Christopher Wright"
 Eric Suen wrote:
 Hi,

   I write a very simple program, two version,
 one is write in D, and others is using C, the D version
 is extreme slow, the D version using 450 ms, but C version
 using 0 ms, can anybody tell me is there anything wrong
 in the code?

 Here is the code:
 http://files-upload.com/files/499789/test.zip
The compile time is also insanely long. If you just remove 'const' from those giant arrays (you'll have to assign base_action in your static constructor), you get a giant speed boost, both in compilation and in run time. Looking at the assembly, constant expansion means that your tAction function maps to 28000 lines of assembly if you have const arrays, or about fifteen if you have non-const arrays. Constant expansion of arrays is an optimization for small arrays, but this is slightly ridiculous. But the speed improvement from omitting 'const' puts the D version on par with the C. And if you want to get both the speed and the constness, I think you can have the giant array literals be private and mutable, and then expose some public const arrays that your static constructor sets as references to the private ones. (You can certainly do it in D2. Not sure about D1.)
Sep 13 2007
parent Christopher Wright <dhasenan gmail.com> writes:
Eric Suen wrote:
 D not even a dynamic language, why const will affect runtime
 performance? besides, isn't all the LALR parser will using
 giant const arrays? for examples: seatd(http://seatd.mainia.de/)
 has a huge file called parser.d...
Look at the assembly yourself. It's quite obvious that dmd is doing constant expansion in a situation where any human would tell you that it's silly, and that this is creating a very bloated function that probably takes longer to load than the pagefault that reference semantics might cost. I don't know how seatd works, or how efficient it is. I just looked at that example. It might be that, due to some strange quirks of dmd, seatd has large const arrays that work efficiently and your example has large const arrays that blow up. More likely, seatd uses much smaller arrays, or does something to avoid making them const. Also, perhaps seatd uses one array at a time rather than doing array1[array2[array3[input]]]] sort of things. As to why there's a performance difference (D not being dynamic is irrelevant to this), dmd expands compile-time constants. There's no pointer to take an offset of; you have to have a giant switch statement that returns a value depending on your input.
Sep 13 2007