Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#4820 closed Bugs (invalid)

any swaps inefficiently

Reported by: jonathandodd@… Owned by: nasonov
Milestone: To Be Determined Component: any
Version: Boost 1.44.0 Severity: Optimization
Keywords: Cc:

Description

Apologies if this isn't a bug - I am quite new to boost, however I checked in the IRC channel and it doesn't seem like this is a total non-starter.

It seems that if you simply call swap, std::swap is called, resulting in an inefficient swapping operation. For example:

Big b1, b2; any a1=b1, a2=b2;

swap(a1,a2); Calls std::swap, invokes three copy constructors

I've attached a demonstration file, which I compiled with gcc.

Seems like can be easily solved with an overloaded std::swap, but I know little about boost, so I'll leave that for the experts :)

Attachments (1)

test.cc (273 bytes ) - added by jonathandodd@… 12 years ago.
demo of any::swap

Download all attachments as: .zip

Change History (3)

by jonathandodd@…, 12 years ago

Attachment: test.cc added

demo of any::swap

comment:1 by Bryce Adelstein Lelbach, 12 years ago

Resolution: invalid
Status: newclosed

As I explained on IRC, this is not inefficient. Two copy ctor calls happen in the construction of the boost::any objects.

comment:2 by jonathandodd@…, 12 years ago

To clarify, when I run this there are in fact 5 copy constructor calls, 3 of which were generated by swap.

I have been assured that this is just me not turning on compiler optimisations, or not using boost::swap. However, even with the -o3 switch in gcc, and even with boost::swap, I still get 3 copy constructor calls. Since the best thing to do is just to swap over the pointers, I expected none.

Note: See TracTickets for help on using tickets.