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

bearophile bearophileHUGS at lycos.com
Mon Oct 24 04:32:46 PDT 2011


Sean Silva:

> in some
> cases, std::vector causes too much heap traffic so they have
> SmallVector which preallocates a certain amount of storage *inside* of
> the object itself in order to avoid heap traffic if the number of
> elements doesn't exceed some predetermined amount.

I expect Phobos to eventually gain such data structure too, if it's so useful. It doesn't look hard to implement.


> Even still, LLVM
> uses std::vector all over the place, and it I've never seen a
> std::vector embedded in a class by reference; it is always held by
> value precisely because then you don't do an unnecessary heap
> allocation.

If the vector needs to grow you can't store it all in the class instance. If it can't grow, in D you use a fixed-sixed array inside the class instance. So the situation seems the same as C++, or better.


> I admit, I'm very biased because my use case for D is low-level
> systems programming a la C/C++, so naturally I want a standard library
> that will not compromise on aspects that are important for the kinds
> of programs that I write. Nonetheless, if the goal is to have "good
> enough" containers, then it doesn't matter, but if the goal is to have
> "truly optimal" containers (as I think it should be; D is certainly
> powerful enough to pull it off elegantly), then it does matter.

Phobos data structures are meant to be "very good" in their implementation. But every data structure is the result of a balance of several trade-offs. Phobos and D try to lead to correct code, because a fast but wrong program is useless. Even C++ STL is not the fastest possible, so people write other libraries:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list