Looking for documentation of D's lower-level aspects.

Dmitry Olshansky dmitry.olsh at gmail.com
Sun Oct 23 01:28:45 PDT 2011


On 23.10.2011 7:25, Sean Silva wrote:
> == Quote from Jonathan M Davis (jmdavisProg at gmx.com)'s article
>> On Sunday, October 23, 2011 00:01:42 Sean Silva wrote:
>> They're all supposed to be reference types.
>
> What prompted the decision for that? Doesn't that incur an extra heap allocation for the
> containers, and an extra level of indirection? I mean, with value-semantics like STL
> containers, you can use it like a value, e.g. as a local in a function, and have no overhead,

AFAIK using e.g. local vector<T> only a small bunch of info is stored on 
stack (ptr, length, capacity, whatever else). It still allocates it's 
elements on heap. Plus there are ways to place class instance on stack 
or anywhere else (e. g. some scratch memory page) if you really need to, 
look for emplace in phobos.

> but if you want to wrap a class around it and have it by reference, then you can, and it is no
> less efficient than if the containers were written that way. But if the containers are already
> by reference, you can't return them to having value semantics without adding even more
> indirection and inefficiency.

Less efficient is a moot point.
When you do iteration and other stuff you'd use range, like you'd use 
iterators in c++. Range gets stack/register allocated pointers directly 
to data (or close to it, that depends on container) so the only extra 
cost in reference type compared to value is the first indirect access to 
construct range and it's negligible.


-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list