www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can nice D code get a bit slow?

reply Markus <marm24 proton.me> writes:
Hi, sorry for the broad and vague question. I have read in some 
reddit post about benchmarks, that some code didn't use the final 
keyword on methods in a sense that final would make it faster, I 
believe.

I thought, without any D knowledge, it could be that with shorter 
code I might create virtual calls by accident. It might in some 
rare case have an impact on performance, but it might - on the 
good side - be the optimizer of one of the three compilers that 
puts that handling of maybe-virtual calls away or whatever might 
happen.

So, having no clue about D (just bought some books), I wanted to 
ask if nice looking code can become slow, in general. In the 
mentioned case it's just that I like the packaging of functions 
into some sort of scope (OOP) versus the flat C and Go stuff. I 
like OOP for this reason, but now I'm unsure whether I might stay 
out of creating classes at all.

I do like to write 'in' and 'ref' keywords at their places, for 
maybe creating some little speed benefits and safety, but the 
'final' keyword I then might write because I opted in into OOP 
looks, for just the visual reason.

Uh, hope you understand my vague question, sorry about that. I 
found D to be the right place because it's not missing any 
essential feature I know of.

Kind regards
Mar 08 2023
next sibling parent reply Hipreme <msnmancini hotmail.com> writes:
On Wednesday, 8 March 2023 at 10:49:32 UTC, Markus wrote:
 Hi, sorry for the broad and vague question. I have read in some 
 reddit post about benchmarks, that some code didn't use the 
 final keyword on methods in a sense that final would make it 
 faster, I believe.

 [...]
Don't bother with it. This kind of optimization is done when compiling with -O, and I really doubt about your bottleneck being that you're calling a virtual call. Wait when you actually need to optimize before making your code ugly.
Mar 08 2023
parent user1234 <user1234 12.de> writes:
On Wednesday, 8 March 2023 at 12:46:53 UTC, Hipreme wrote:
 On Wednesday, 8 March 2023 at 10:49:32 UTC, Markus wrote:
 Hi, sorry for the broad and vague question. I have read in 
 some reddit post about benchmarks, that some code didn't use 
 the final keyword on methods in a sense that final would make 
 it faster, I believe.

 [...]
Don't bother with it. This kind of optimization is done when compiling with -O,
No they are not, simple example : https://godbolt.org/z/xfPqnWrrv Obviously in a library `test` might be called with a derived so `v1()` target address must be read in the vtbl of the argument.
 and I really doubt about your bottleneck being that you're 
 calling a virtual call.
That is more true. vcalls dont add complexity.
 Wait when you actually need to optimize before making your code 
 ugly.
Mar 09 2023
prev sibling next sibling parent FeepingCreature <feepingcreature gmail.com> writes:
On Wednesday, 8 March 2023 at 10:49:32 UTC, Markus wrote:
 So, having no clue about D (just bought some books), I wanted 
 to ask if nice looking code can become slow, in general. In the 
 mentioned case it's just that I like the packaging of functions 
 into some sort of scope (OOP) versus the flat C and Go stuff. I 
 like OOP for this reason, but now I'm unsure whether I might 
 stay out of creating classes at all.

 Uh, hope you understand my vague question, sorry about that. I 
 found D to be the right place because it's not missing any 
 essential feature I know of.

 Kind regards
If you write D like Java, it will be slow like Java - actually slower, because D's GC isn't as good. If you remember to use structs for value types (no `class Vector3f`, `class Point` etc.), you should be fine. Though as a general rule, don't worry about it too much. Even slow D is still fast.
Mar 08 2023
prev sibling next sibling parent Guillaume Piolat <first.last spam.org> writes:
On Wednesday, 8 March 2023 at 10:49:32 UTC, Markus wrote:
 Uh, hope you understand my vague question, sorry about that. I 
 found D to be the right place because it's not missing any 
 essential feature I know of.
Well, bounds check often cost a few percent, and you can disable it or use .ptr safe will push you to use slices instead of raw pointers, and those can occupy the stack a bit more, but it's not a huge effect. Lack of dangerous "fast math" "optimizations" is a real boon for correctness. We don't have integers overflows, those would also cost a few percent. Your D programs by being easy to modify will have very few performance bottlenecks that are problematic, often the CPU-bound or memory-bound stuff that would be slow in any language. For example I like to merge array allocations, this necessitates the same code in C, C++, or any native language. I think it's important not to go overboard with lazy chains and templates though. Honestly the increased productivity will leave you ample time to make things fast, and you can use the native profilers, for example Intel Vtune. This year I released a free VST compressor named Lens, it does more than the closest manybands competitor while being in the ballpark CPU-wise, and they are using ICC AVX2 which does dynamic instruction set dispatch, while we use SSSE3. Optimization is all about having the time to do it.
Mar 08 2023
prev sibling next sibling parent reply FozzieBear <FozzieBear gmail.com> writes:
On Wednesday, 8 March 2023 at 10:49:32 UTC, Markus wrote:
 Hi, sorry for the broad and vague question. I have read in some 
 reddit post about benchmarks, that some code didn't use the 
 final keyword on methods in a sense that final would make it 
 faster, I believe.

 I thought, without any D knowledge, it could be that with 
 shorter code I might create virtual calls by accident. It might 
 in some rare case have an impact on performance, but it might - 
 on the good side - be the optimizer of one of the three 
 compilers that puts that handling of maybe-virtual calls away 
 or whatever might happen.

 So, having no clue about D (just bought some books), I wanted 
 to ask if nice looking code can become slow, in general. In the 
 mentioned case it's just that I like the packaging of functions 
 into some sort of scope (OOP) versus the flat C and Go stuff. I 
 like OOP for this reason, but now I'm unsure whether I might 
 stay out of creating classes at all.

 I do like to write 'in' and 'ref' keywords at their places, for 
 maybe creating some little speed benefits and safety, but the 
 'final' keyword I then might write because I opted in into OOP 
 looks, for just the visual reason.

 Uh, hope you understand my vague question, sorry about that. I 
 found D to be the right place because it's not missing any 
 essential feature I know of.

 Kind regards
The whole point of OO programming, made convenient by the class type, is to enable you to more easily work with abstractions that relate to your problem domain, rather than abstractions related to the processing of your code at the machine level (cpu, memory, cache, registers... etc.). This is, in essence, the difference between C like programming, and OOP. Now if 7 nanoseconds (more or less) pose a signficant performance issue for the solution you're developing, then OOP probably isn't what you're looking for anyway. Having said that though, any info you can provide to the compiler so that it can (hopefully) use that information at compile time and insert direct calls instead of virtual calls, may be worthwhile. But that assumes you even know how long 7 nanoseconds are .. cause I don't.
Mar 09 2023
parent FozzieBear <FozzieBear gmail.com> writes:
On Thursday, 9 March 2023 at 10:05:35 UTC, FozzieBear wrote:
 ..
 But that assumes you even know how long 7 nanoseconds are .. 
 cause I don't.
Well, it turns out, that light takes about a nanosecond to travel 1 foot. So that means, the performance impact from using a virutal call, would be equivalent to the amount of time light takes to travel 7 feet. I'm sure there are applications out there that might need to take this into account. But for most, its trivial and irrelvant, and the programmer would benefit from focusing on more important things.
Mar 09 2023
prev sibling parent FozzieBear <FozzieBear gmail.com> writes:
On Wednesday, 8 March 2023 at 10:49:32 UTC, Markus wrote:

btw. As far as I can tell (using the example in the link below), 
compiled D (using the ldc compiler with -O3) will optimise the 
call just fine.

However, not including the 'final' keyword would not mean the end 
of the world ;-)

https://godbolt.org/z/nW5s3z1Tc
Mar 09 2023