[Issue 9088] static static

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 29 10:09:08 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=9088



--- Comment #2 from bearophile_hugs at eml.cc 2012-11-29 10:08:58 PST ---
(In reply to comment #1)

Thank you for the answers Andrej.

> For functions: I think this might be the job of the compiler/linker. If there
> are multiple static function definitions that are identical they should be
> merged, there's no need for the user to do the job of the compiler/linker.

Notes:
- Currently DMD is not doing this, and I think it's not going to do it soon (I
don't know about GDC2, possibly GCC is able to do this, but see below);
- I think LDC2 is able to do this, but you have to add a compilation switch
that is not included in "-O4" switch and that no one uses or knows about
(something avout merging functions);
- I think in C if you have many different functions that contain the same code,
and you take their address, the C language requires them to be different. I
assume D shares the same C rule. So LDC is forced to leave stubs that contain a
jump, it can't delete them fully;
- Avoiding to create bloat in the first place is faster for the compiler+linker
than compiling them and then not linking them (or recognizing them as equal and
replacing them with a jump to just one implementation). I agree that in theory
the compiler can avoid creating those copies in the first place, because it
knows they come from a template, but I think currently LDC2 is not this smart;
- A "static static" annotation is a contract between compiler and programmer,
like "pure". This means that if you try to use the template arguments (that are
types and compile-time values) in a "static static" template struct method, the
compiler complaints at compile-time (something similar happens if you try to
use a locally defined variable in a static nested function). This enforcing
can't happen if such feature is left to compiler/linker optimizations.
- Generally I think D needs somethiong to help fight template bloat. "static
static" is not very good for this, but I think it helps.


> As for template-shared variables, they are possible without bloating the global
> (module) scope by using templates, for example:
> ...
> You could probably implement some kind of helper mixin that could implement
> this automatically.

"static static" is not essential, because there's always the possibility to
move things to module scope.

On the other hand if you take a look at topswops(), a "static static"
annotation allows to minimize the amount of changes in the code when you want
to turn a function into a template (and avoid moving static variabiles to
globals). So it's not essential, but I think it's handy, in some cases.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list