Should binary sharing be done using Mixin Template?

Jonathan M Davis jmdavisProg at gmx.com
Sat May 21 04:03:53 PDT 2011


On 2011-05-21 02:26, Matthew Ong wrote:
> Hi D Developer/Walter Bright,
> 
> Coming from a VM environment.
> 
> Should D be able to do binary sharing when the same template is being
> used for different data type.
> 
> In java using template, the same LinkedList binary is shared for both
> String class type and also Integer class type.
> 
> LinkedList<String> list=new LinkedList<String>();
> LinkedList<Integer> list=new LinkedList<Integer>();
> 
> // Can also apply for Account/Order/PO...
> LinkedList<Account> list=new LinkedList<Account>();
> 
> But there is a single LinkedList Class File(single binary).
> 
> Perhaps that is possible via some sort of binary plumbing internal to
> the compiler? I believe the wrapper is just to ensure the Object type
> handling casting concern. I might be wrong.
> 
> Accoding to Jonathan Davis,
> 
>  >There is no linking involved in mixins. It's not shared.
> 
> This approach I believe allow the final output be smaller even and
> pushes the D to be closer even to the dynamic ability of VM but without
> the extra over head.
> 
> Can someone really say why this is a bad bad idea for memory with some
> automated plumbing being done like in ActiveX/Com/DCOM.

If you want to have containers be shared, then just put Objects in them and 
cast them to the correct type when you get them out. That's what Java does. 
It's just that starting with Java 1.5, the generics do it for you.

Templates are completely different from generics. Generics are compile-time 
artifacts only and have no effect on the final binary. Templates, on the other 
hand, are generated for each type that they're instantiated with. They are 
code generation, pure and simple. And since each and every instantiation could 
be very different depending on what was done with static if and other compile-
time stuff which can affect code generation. So, if you had a 
LinkedList<Account> and a LinkedList<string>, their internal implementation 
could be completely different if the writer of LinkedList thought that it was 
appropriate to do so for whatever reason. In the general case, templates are 
not at all combinable. Even if two template instantiations generate 
effectively the same code, unless the're binary compatible, you can't possibly 
combine them. Now, as I understand it, gcc does do some template combining in 
C++ where it can, but that's an advanced optimization technique that not all 
C++ compilers do, and I don't believe that any D compilers do it at present.

Templates and generics are completely different. They both have their 
advantages and disadvantages, but please don't try and make templates act like 
generics. It's just not going to work. They are inherently different 
regardless of how similar their syntax might be.

- Jonathan M Davis


More information about the Digitalmars-d mailing list