www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D generates large assembly for simple function

reply Matt <vodkahaze hotmail.com> writes:
Playing around with Godbolt, D seems to generate an embarassing 
amount of assembly for a simple function (50ish for squaring an 
int vs 4 for C++ and 7 for Rust). Even Go compiles to less 
assembly.

Is there something I'm missing?
Jan 27 2018
next sibling parent reply Matt <vodkahaze hotmail.com> writes:
Godbolt link: https://godbolt.org/g/t5S976
Jan 27 2018
next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 27 January 2018 at 19:42:01 UTC, Matt wrote:
 Godbolt link: https://godbolt.org/g/t5S976
The actual code is : imul edi, edi mov eax, edi ret The rest is runtime initialization. which you can remove using an undocumented -betterC switch.
Jan 27 2018
next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 27 January 2018 at 19:43:50 UTC, Stefan Koch wrote:
 On Saturday, 27 January 2018 at 19:42:01 UTC, Matt wrote:
 Godbolt link: https://godbolt.org/g/t5S976
The actual code is : imul edi, edi mov eax, edi ret The rest is runtime initialization. which you can remove using an undocumented -betterC switch.
ah ... -betterC is only for dmd. try using the gdc compiler instead of ldc. it does not emit runtime stuff if it's not used.
Jan 27 2018
parent Johan Engelen <j j.nl> writes:
On Saturday, 27 January 2018 at 19:45:35 UTC, Stefan Koch wrote:
 ah ... -betterC is only for dmd.
`-betterC` works from LDC 1.1.0. - Johan
Jan 27 2018
prev sibling next sibling parent kdevel <kdevel vogtner.de> writes:
On Saturday, 27 January 2018 at 19:43:50 UTC, Stefan Koch wrote:
 On Saturday, 27 January 2018 at 19:42:01 UTC, Matt wrote:
 Godbolt link: https://godbolt.org/g/t5S976
The actual code is : imul edi, edi mov eax, edi ret
Could you please paste the source code? I mean in say 5 years when there will be no more godbolt.org someone reading this thread will not know what it was about.
Jan 27 2018
prev sibling next sibling parent Seb <seb wilzba.ch> writes:
On Saturday, 27 January 2018 at 19:43:50 UTC, Stefan Koch wrote:
 On Saturday, 27 January 2018 at 19:42:01 UTC, Matt wrote:
 Godbolt link: https://godbolt.org/g/t5S976
The actual code is : imul edi, edi mov eax, edi ret The rest is runtime initialization. which you can remove using an undocumented -betterC switch.
It's not undocumented: https://dlang.org/spec/betterc.html
Jan 27 2018
prev sibling parent Seb <seb wilzba.ch> writes:
On Saturday, 27 January 2018 at 19:43:50 UTC, Stefan Koch wrote:
 On Saturday, 27 January 2018 at 19:42:01 UTC, Matt wrote:
 Godbolt link: https://godbolt.org/g/t5S976
The actual code is : imul edi, edi mov eax, edi ret The rest is runtime initialization. which you can remove using an undocumented -betterC switch.
BTW as asm.dlang.org is dead, you can use run.dlang.io for these things, e.g. DMD: https://run.dlang.io/is/lLL1aJ LDC: https://run.dlang.io/is/sVn5tu (-output-s / -asm are only added for extra convenience) Since a couple of days, it even does demangling of the symbols. Though, of course, if you want to look only at LDC's output, godbolt is still the better choice.
Jan 27 2018
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 01/27/2018 11:42 AM, Matt wrote:
 Godbolt link: https://godbolt.org/g/t5S976
According to that link D and C++ both produce 4 lines of assembly, Rust 7, and Go 38 (for that function). Ali
Jan 27 2018
parent reply Johan Engelen <j j.nl> writes:
On 01/27/2018 11:42 AM, Matt wrote:
 Godbolt link: https://godbolt.org/g/t5S976
Careful with these comparisons guys. Know what you are looking at. Rust does not eliminate setting the framepointer register, and so it looks "bad" [1]. Clang also sets the framepointer for macOS ABI regardless of optimization level. https://godbolt.org/g/eeo81n [1] See https://github.com/rust-lang/rust/pull/47152 -Johan
Jan 28 2018
parent welkam <wwwelkam gmail.com> writes:
On Sunday, 28 January 2018 at 14:33:04 UTC, Johan Engelen wrote:
 Careful with these comparisons guys. Know what you are looking 
 at.
Wise words
Jan 28 2018
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Jan 27, 2018 at 07:41:21PM +0000, Matt via Digitalmars-d-learn wrote:
 Playing around with Godbolt, D seems to generate an embarassing amount
 of assembly for a simple function (50ish for squaring an int vs 4 for
 C++ and 7 for Rust). Even Go compiles to less assembly.
 
 Is there something I'm missing?
If you're looking for efficiency of generated code, use gdc or ldc. While dmd is the reference compiler with the latest and greatest bleeding-edge features, it's not known to be the best at generating optimized code, even if you run it with -O. If code size / efficiency is important to you, I highly recommend using gdc or ldc instead. T -- Век живи - век учись. А дураком помрёшь.
Jan 27 2018