version=D_16

via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 10 15:39:22 PDT 2017


On Monday, 10 July 2017 at 21:53:16 UTC, Luís Marques wrote:
> On Monday, 10 July 2017 at 21:30:44 UTC, Walter Bright wrote:
>> You can't use RTTI or Exceptions, for example. Those generate 
>> bloat even if they are not used - a compiler switch is typical 
>> to disable them. It's not true that C++ is "pay only for what 
>> you use".
>>
>> If the C++ usage is "C with member functions", then yes, it'll 
>> work and be useful.
>
> Sure, people don't use exceptions and RTTI and other expensive 
> things on microcontrollers. But, as you say, they use it for 
> less expensive or zero cost things like member functions, 
> operator overloading, templates (not very common, but I've seen 
> some interesting 0-cost wrappers that increased the safety of 
> setting registers), etc. But I'm not here to advocate C++, I've 
> only used it incidentally for microcontrollers (AVR, the 
> Arduino libraries are C++, urg).
>
>> For example, ints in C are 16 bits. In D they are 32. This 
>> means that integer operations are expensive.
>
> I don't understand this point of view. You are literally saying 
> that writing "int x" is more expensive in D than in 16-bit C 
> because they mean something different. Uhh, so just write 
> "short x" in D? That's the equivalent code. Why would you write 
> code that's character-by-character the same but means a 
> different machine type?
>
> If that's because short is more inconvenient in D than in C, I 
> can understand that! But C also has a lot of annoying 
> limitations, I feel that overall it's still a win.
>
> If you give me more concrete examples I can try to address 
> them. Just keep in mind that it's still fairly low level D 
> code. I'm not throwing new Exceptions! But you can use modules, 
> static ifs, templates... Modules alone sometimes feel like 
> enough benefit...

The problem Walter pointed to is that due to integer promotion, 
arithmetic operands of types smaller than int are converted to 
int, hence even if you use bytes and shorts you would end up 
using ints, which are expensive on CPUs with no native 32-bit 
registers. In theory, you could write your code so that it's easy 
for the optimizer to prove that you're only using 8 or 16 bits 
and variables would fit in single registers, so you would be able 
to get away without a performance penalty for using a language 
where ints are 32-bit.


More information about the Digitalmars-d mailing list