What could have been the reason for this?
Nathan Myers
ncm-nospam@cantrip.org
Mon Jun 3 16:31:00 GMT 2002
On Mon, Jun 03, 2002 at 05:40:48PM -0400, Phil Edwards wrote:
> Some of the helper routines for insertions in vector et al go like:
>
> an_insert_helper (......., value_type const& x)
> {
> value_type x_copy = x;
> find the right position in the sequence
> set up memory
> *position = x_copy;
> }
>
> None of the pseudocode in the middle two "lines" uses x nor x_copy.
>
> For the life of me I cannot figure out why it's done this way. 'position'
> is either of iterator type, and its dereference operator returns a reference,
> or of pointer type. Assigning "const reference x" to such a beast should
> have the exact same semantics as assigning plain local x_copy, unless I've
> forgotten a serious chunk of C++.
>
> What would SGI have gained by using this extra copy? I don't see any
> difficulties that would require the proverbial extra level of indirection.
> Am I overlooking some design decision?
No doubt there was some case where somebody was inserting a value that
was already in the array, but either (1) it appears at a higher index
than the insertion point, so that shifting elements down blows away the
argument instance before there is a place to put the copy, or (2) the
insertion needs a reallocation, and the old copy gets destroyed before
they get around to copying it into place. Rather than detect those
cases and handle them, they just made another copy first.
It's decidedly suboptimal for objects that have nontrivial copy
semantics. You can optimize this by using less<T*> to detect
whether the argument reference is actually in the array. You
take a pointer to the argument, and then adjust the pointer if
the target actually moves out from under it.
> One of my favorite hard science fiction novels involves a society of
> spacefaring traders. Their ships' libraries have accumulated thousands
> of years worth of software. As a result, the most needed job on any
> trading ship is "Programmer-Archaeologist," someone who can dig through
> the centuries of code. I'm beginning to sympathize. :-)
That's Vernor Vinge. If you haven't read "The Peace War" yet, you are
missing out on fundamental juju.
Nathan Myers
ncm at cantrip dot org
More information about the Libstdc++
mailing list