Should binary sharing be done using Mixin Template?

Dmitry Olshansky dmitry.olsh at gmail.com
Sun May 22 03:25:49 PDT 2011


On 21.05.2011 23:03, bearophile wrote:
> so:
>
>> What kind of applications are we talking about?
> Let's say all applications longer than few thousands lines of code.
>
>
>> I can't imagine a situation where you sacrifice the performance (that you
>> gain with templates) for a trivial issue like bloat,
> Experience with profilers and with dynamic languages shows well that in all programs longer than few thousand lines a certain (sometimes good) percentage of the code doesn't influence much the performance of the whole program. So for such code other factors like code succinctness, safety, maintainability, and even binary compactness become more important than performance.
>
>
>> when we finally get shared libraries, it will be even more trivial.
> We will see. But I don't believe a single solution will solve this problem.
>

I think one problem is mentality e.g. people do template things on wrong 
criteria.
As a small example, some containers (like lists and unlike sets) do not 
care about "what's in the box". It's only size in bytes + alignment that 
really matters (and if there are indirections in data). Then it's just 
simple wrapper on top of it that does interpretation of bytes.
So imagine that something like this:
struct List(T)
{
     Node!T head;
//    ... methods on List
}
struct Node(T)
{
     T data;
     Node* next;
}

should really be more like:

struct ListBase(T.sizeof)
{
     NodeBase!(T.sizeof) head;
//    ... methods on ListBase
}
struct NodeBase(size_t size)
{
     void[size] data=void;
     NodeBase* next;
}

List!T forwards to ListBase!(T.sizeof) almost no new code, 'front' and 
'back' aside. Also we have our type checking back.
All of the wrappers are doing very inlineable things like e.g.
struct ListRange(T)
{
     NodeBase!(T.sizeof)* current;
     @property ref T front(){ return *cast(T*)(&current.data[0]); }
     @property void popFront(){ current = current.next; }
     @property bool empty(){ return !current; }
}

> Bye,
> bearophile


-- 
Dmitry Olshansky



More information about the Digitalmars-d mailing list