Const sucks

Sean Kelly sean at f4.ca
Tue Sep 11 07:27:04 PDT 2007


Bruce Adams wrote:
> Sean Kelly Wrote:
> 
>> Walter Bright wrote:
>>> Bruce Adams wrote:
>>>>>> o  So, we still need a method to declare a constant that will not
>>>>>>  consume memory. We'll co-opt the future macro syntax for that:
>>>>>>
>>>>>> macro x = 3; macro s = "hello";
>>>> I'm not clear why this is even needed. Surely "static const" implies
>>>> some level of don't consume any memory by virtue of meaning
>>>> "available at compile time". If you need more than that, rather than
>>>> trusting the compiler then I second "inline" rather than macro, but
>>>> honestly I can't think of an example where this will give you a
>>>> worthwhile performance gain (unlike an inline function).
>>> windows.h in all its terrifying glory has 10,000 macro declarations. If 
>>> each one occupied 4 bytes of static data, there's 40K of bloat in every 
>>> exe.
>> That sounds about right.  When we converted const values to enum values 
>> in the Tango Win32 headers, app size dropped by roughly 46k.  But there 
>> were issues with some of those enum values having the wrong type (-1 set 
>> to int instead of uint), which led to a few strange bugs.
>>
>>
>> Sean
> 
> I seek enlightenmnet for at least two problems with that explanation.
> 
> windows.h is a C binding not a D binding.
> I'm not clear how one translates to the other yet as I've avoided bi-lingual stuff so far. Presumably the D binding is created using htod.

htod works for simple cased.  More complex cases require GregorR's BCD 
or simply a manual effort.

> I thought D followed the you only pay for what you use philosophy.
> So if I import my windows.d module I will only pay the cost of the
> variables I actually use. My friendly neighbourhoold compiler, being clever, mightl also make an effort to minimise the cost of those too.

D currently links at the file level, so if you use anything in a file 
you get the whole thing.  Some linkers are able to link at the section 
level (a piece of a file), but D does not support this yet (see comments 
regarding gc-sections using ld, I believe--there's an issue in 
BugTracker about this).

> While we're on the subject of windows. 40K means nothing. In an embedded system yes. Windows is bloated to the point where 40K in a user application means de nada. (I shudder at the 4Gb games that follow in its wake too). I have know idea where the windows mobile / CE / pocket PC api falls here.

It matters to some people, particularly those coming from C and 
comparing EXE sizes.  It may not be relevant as far as general 
development on Windows is concerned, but it's something library 
developers must take into consideration.


Sean



More information about the Digitalmars-d mailing list