www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Should binary sharing be done using Mixin Template?

reply Matthew Ong <ongbp yahoo.com> writes:
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. -- Matthew Ong email: ongbp yahoo.com
May 21 2011
next sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Sat, 21 May 2011 12:26:35 +0300, Matthew Ong <ongbp yahoo.com> wrote:

 Should D be able to do binary sharing when the same template is being  
 used for different data type.

A linker should be able to merge binary-identical objects in read-only segments, but this will cause ambiguity in stack traces (and possibly have other consequences I can't think of). -- Best regards, Vladimir mailto:vladimir thecybershadow.net
May 21 2011
prev sibling next sibling parent Daniel Gibson <metalcaedes gmail.com> writes:
Am 21.05.2011 11:26, schrieb Matthew Ong:
 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).

This is because Javas generics are gone when the code is compiled, i.e. List<Integer> und List<String> is the same type. I find them rather useless, you can't overload from Generics parameters: void foo(List<Integer> l) { ... } void foo(List<String> l) { ... } won't compile. Totally different from D or C++ where a new type is created that is actually specific to the template parameter.
 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.

In D you can have non class types as template parameters. And even with class types your code could handle different classes in a different way via "static if( is(T : MyType) ) { ... }" etc
 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.

May 21 2011
prev sibling next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
May 21 2011
next sibling parent "JimBob" <jim bob.com> writes:
"Jonathan M Davis" <jmdavisProg gmx.com> wrote in message 
news:mailman.306.1305975846.14074.digitalmars-d puremagic.com...
 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.

Cant the compiler simply do a bitwise compare of all versions of a given template function/method, and remove any that have duplicate code? I mean actually compare the generated asm code.
May 22 2011
prev sibling parent Paulo Pinto <pjmlp progtools.org> writes:
On 21.05.2011 13:03, Jonathan M Davis wrote:
 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.

Only if you are speaking about Java. In Eiffel, Ada, Modula-3 Ocaml, Haskell, and .Net generics are also handled like C++ templates. They just don't allow the same type of metaprogramming tricks that C++ does, although Template Haskell does allow neat tricks as well. -- Paulo
May 24 2011
prev sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Sun, 22 May 2011 17:12:17 +0300, JimBob <jim bob.com> wrote:

 Cant the compiler simply do a bitwise compare of all versions of a given
 template function/method, and remove any that have duplicate code? I mean
 actually compare the generated asm code.

That's what I suggested earlier, but I think this should be done by a linker, as it'll allow merging duplicate code objects in all modules. -- Best regards, Vladimir mailto:vladimir thecybershadow.net
May 22 2011