std.container.RedBlackTree versus C++ std::set
Rob T
alanb at ucora.com
Thu Feb 14 00:01:07 PST 2013
On Thursday, 14 February 2013 at 06:56:38 UTC, monarch_dodra
wrote:
> On Wednesday, 13 February 2013 at 23:22:03 UTC, Ivan Kazmenko
> wrote:
>> Hi!
>> -----
>> Ivan Kazmenko.
>
> Keep in mind that C++ and D have very different philosophies
> regarding copy construction.
>
> C++ has "strong ownership", so for example, whenever you copy a
> string/vector, or pass it by value, the whole damn thing has to
> be completely duplicated. It's so expansive that C++ has gone
> out of its way to use pass-by-reference semantics, as well
> (now) RValue references.
>
> D, on the other hand (being GC), has a "shallow ownership"
> philosopy: Basically, when you make a copy, nothing happens,
> appart from the binary copy of the object. You want to pass a
> string by value? that's 8 bytes of data. Period. The *most*
> complex CC I have *ever* seen in D merely increments a
> reference counter. That's it. 90% (99%?) of the time, there
> isn't even a postblit implemented. Because of this, D has
> embraced pass by value.
>
> Not to mention, if you are using classes, those are already
> pointer types. Why pass them by ref?
>
> --------
>
> The conclusion is that the comparison is not fair: D's pass by
> value is not *very* different from C++'s pass by ref (in amount
> of data copied).
>
> If you *do* want a fair-er comparison, then I'd suggest you
> implement a ".dup" on your object, and see how many times THAT
> gets called. Guess what: 0.
>
> I'm not saying D's approach is perfect, just that D's library
> is optimized for D-like types.
>
> Just the same, a lot the stl is not properlly optimized for
> little POD types, as they get passed around by ref, when their
> actual size is the same as the pointer referencing them...
I agree. There are cases where structs make a lot of sense,
usually when they are very simple simple and contain no pointers
or references, otherwise structs should be avoided in favor of
classes to avoid doing copy/move constructors and to avoid
concerns over performance optimizations. With classes, only
certain points in your code require that a duplicate copy be made
of the class instance, the majority of code need only to pass
around a reference which is the default behavior - easy and fast!
--rt
More information about the Digitalmars-d-learn
mailing list