473,792 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

R-value references and performance

Hey everyone.

First of all let me say that I know that the C++ standard never makes
any guarantees about compiler implementation and thus about the
performance of any language feature. That said, r-value references
are clearly being included because they make certain optimization
analysis cases possible to identify, and every compiler will try to
include them.

Anyway, I've been playing around with some code in ConceptGCC using r-
value references and lots of inlined code to write something very
general that should simplify down to the speed of the old C version.
Needless to say, the generated assembly code is about twice as long so
it doesn't work. I'm not sure if thats because the optimizations
aren't in yet, or because I am asking for an unreasonable amount of
intelligence from the compiler (the kind that I can never count on
showing up, even years from now). While this isn't really a C++
language issue per se, many of the advantages of template/generic
programming is predicated on the idea the compiler can and will make
certain optimizations after it does the inline expansions.

Here is a simple C program which adds the first vector from a list in
interleaved format (x0, y0, z0, x1, y1, z1, ...) to the second vector
from a list in planar format (x0, x1, y0, y1, z0, z1). It adds
interleaved vector 0 to planar vector 1 and stores it in interleaved
vector 2:

int main (void)
{
double interleavedStor age[] = {
rand(), rand(), rand(),
rand(), rand(), rand(),
0, 0, 0
};

double planarX[] = {rand(), rand()};
double planarY[] = {rand(), rand()};
double planarZ[] = {rand(), rand()};

interleavedStor age[6] = interleavedStor age[0] + planarX[1];
interleavedStor age[7] = interleavedStor age[1] + planarY[1];
interleavedStor age[8] = interleavedStor age[2] + planarZ[1];

// This is here to make sure that the values are read, so that it
// actually does something
return (int)(interleav edStorage[0] + interleavedStor age[1] +
interleavedStor age[2]);
}

Here is my C++ program, which allows you to store a vector in any
format. I add some comments to explain what I think is supposed to
happen:

#include <utility>
#include <cstdlib>

namespace std
{
// I couldn't find what header std::move was in, so I
// remade it myself. Where is it?
template <typename T>
inline T&& move (T&& a)
{
return a;
}
}

using namespace std;

// A raw_vec holds (x, y, z) coordinates. With any luck, in this
// program a raw_vec will never actually exist and always be
// optimized away

struct raw_vec
{
inline raw_vec (double _x, double _y, double _z)
: x(_x), y(_y), z(_z)
{
}

double x, y, z;
};

// A ref_vec is a structure that references the x, y, and z
// coordinates of a vector managed by some other class.
// With any luck, a ref_vec will never actually exist. After all
// the inline expansion, the compiler will realize that it has
// references to variables but it already knew where those
// variables were in the first place.

struct ref_vec
{
inline ref_vec (double& _x, double& _y, double& _z)
: x(_x), y(_y), z(_z)
{
}

inline ref_vec (ref_vec&& temporary)
: x(std::move(tem porary.x)),
y(std::move(tem porary.y)),
z(std::move(tem porary.z))
{
}

// Whenever a raw_vec is constructed, we hope the
// compiler realizes that it is never needed except
// to assign to the references held by this class,
// thus it should never construct a raw_vec and just
// assign to references directly
inline ref_vec& operator= (raw_vec&& raw)
{
x = std::move(raw.x );
y = std::move(raw.y );
z = std::move(raw.z );

return *this;
}

double& x;
double& y;
double& z;
};

// An interleaved vector container
class InterleavedVect ors
{
public:
inline InterleavedVect ors (double* p)
: basePtr(p)
{
}

// Return the references to x, y, z coordinates of the
// ith vector.
inline ref_vec operator[] (int i) const
{
return ref_vec(basePtr[3*i], basePtr[3*i+1], basePtr[3*i+2]);
}

private:
double* basePtr;
};

// A planar vector container
class PlanarVectors
{
public:
inline PlanarVectors (double* _x, double* _y, double* _z)
: x(_x), y(_y), z(_z)
{
}

// Return the references to x, y, z coordinates of the
// ith vector.
inline ref_vec operator[] (int i) const
{
return ref_vec(*(x + i), *(y + i), *(z + i));
}

private:
double* x;
double* y;
double* z;
};

// Adding two ref_vecs...The result is a raw_vec because operator+
// doesn't have access to the l-value it is assigning to. However, we
// hope that the raw_vec will never exist, because it is assigned
// immediately to a ref_vec (which has the storage we want to use)
// whose operator= (raw_vec&&) function should expand and write
// the values directly into the ref_vec. See the operator= function
// comment in ref_vec, and note the way it used below

inline raw_vec operator+ (const ref_vec& v1, const ref_vec& v2)
{
return raw_vec(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}

int main (void)
{
double interleavedStor age[] = {
rand(), rand(), rand(),
rand(), rand(), rand(),
0, 0, 0
};

double planarX[] = {rand(), rand()};
double planarY[] = {rand(), rand()};
double planarZ[] = {rand(), rand()};

// Our vectors use stack based storage
InterleavedVect ors interleaved(int erleavedStorage );
PlanarVectors planar(planarX, planarY, planarZ);

// We hope this line turns back into the C version. First the
// rhs constructs two ref_vecs (the result of the operator[]).
// But the operator[] is inlined to show the compiler that each
ref_vec
// actually corresponds directly to the storage pointer held by
// storage class, thus we hope it does not really make copies
// of anything. Then operator+ constructs a temporary raw_vec on
// on the rhs. However, it is assigned to another ref_vec, so we
// hope that the inlined ref_vec::operat or= (raw_vec&&) function
// will expand and eliminate the need to actually make the
// temporary, and just store the values directly in the referenced
// coordinates held by the lhs ref_vec. And as before, this
// ref_vec is doing nothing more than serving as a temporary
// alias for InterleavedVect or::basePtr and will be optimized out
interleaved[2] = interleaved[0] + planar[1];

return (int)(interleav edStorage[0] + interleavedStor age[1] +
interleavedStor age[2]);
}
Oct 11 '08 #1
2 2608
On Oct 11, 5:36*am, Ken Camann <kjcam...@gmail .comwrote:

<deleted>
Anyway, I've been playing around with some code in ConceptGCC using r-
value references and lots of inlined code to write something very
general that should simplify down to the speed of the old C version.
Needless to say, the generated assembly code is about twice as long so
it doesn't work.
<deleted>
namespace std
{
* // I couldn't find what header std::move was in, so I
* // remade it myself. *Where is it?
* template <typename T>
* inline T&& move (T&& a)
* {
* * return a;
* }

}
I'm wondering about your definition of move().

In Howard Hinnant's original proposal (1377):

http://www.open-std.org/jtc1/sc22/wg...%20to%20rvalue

he defined it like this:

template <class T>
inline
T&&
move(T&& x)
{
return static_cast<T&& >(x);
}
In an article from earlier this year (A Brief Introduction to Rvalue
References):

http://www.artima.com/cppsource/rvalue.html

He defined it like this:

template <class T>
typename remove_referenc e<T>::type&&
move(T&& a)
{
return a;
}

Perhaps one of these definitions will work better for you.

BTW, I think move() is to be defined in <utility>.
Oct 11 '08 #2
On Oct 11, 2:36*pm, Ken Camann <kjcam...@gmail .comwrote:
>
First of all let me say that I know that the C++ standard
never makes any guarantees about compiler implementation and
thus about the performance of any language feature. *That
said, r-value references are clearly being included because
they make certain optimization analysis cases possible to
identify, and every compiler will try to include them.
I'm not sure that that's the main reason. The offer a semantic
which is very useful, and fairly hard to implement otherwise.
Although the performance aspects probably enter into
consideration, at least for some members of the committee, I
think the real motivation is to make it simple to define
something like auto_ptr in a way that allows it to be used in a
standard container.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 12 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1385
by: raj | last post by:
I need explanation of the behavior of the following code. When run as it is, it gives error. //-------------------------<start>------------------ #include <iostream.h> #include <stdlib.h> class ABC { private: int x;
11
2080
by: JKop | last post by:
The following compiles: // syrup.cpp struct DoubleInDisguise { double data; };
8
2275
by: john townsley | last post by:
are there any differences when using pointers or passing by reference when using 1) basic variables 2)complex variable types like arrays,structures it seems easier to simply pass by reference and simply forget about pointers but thats seems to easy, there must still be a need for pointers
1
2290
by: Hemant Mohan | last post by:
Consider the following program snipet: <snip> typedef struct { unsigned char a ; unsigned char b ; unsigned char c ;
9
3162
by: Kavya | last post by:
These were the questions asked to me by my friend 1. Operator which may require an lvalue operand, yet yield an rvalue. 2. Operator which may require an rvalue operand, yet yield an lvalue. My answer to these questions are & and * respectively. Is/Are there any other operator(s) satisfying these criteria?
25
3501
by: SRR | last post by:
Consider the following code: #include <stdio.h> #include <string.h> struct test{ char a; } funTest( void ); int main( void ) {
1
1726
by: Chris Fairles | last post by:
Take the following snip (pretend A its a std::pair if you wish ;) ) template <class T1,class T2> struct A { A(A const&){cout<<"A(const&) ";} A(A&&){cout<<"A(A&&) ";} A(T1 const&, T2 const&){cout<<"A(T1,T2) ";} template<class U1, class U2> A(A<U1,U2&&) {cout<<"A(A<U1,U2>&&) ";}
0
1345
by: Frank Bergemann | last post by:
Using gcc-4.3.0 it silenty(!) invokes the Test::operator=(Test const& t) for Test a; Test b = std::move(a); Test c = std::moved(T()); - if i disable the Test::operator=(Test&& t) in source file. (But uses Test::operator=(Test&& t) if it is availabled.)
0
1101
by: Jerry Coffin | last post by:
In article <9f60e411-a5b1-4571-9d3d-005432378cd4@ 56g2000hsm.googlegroups.com>, aitorf666@gmail.com says... That's not the real reason for rvalue references. There are two primary reasons for rvalue references: providing move semantics, and providing forwarding semantics. The ability to modify the original object with impunity is the _mechanism_ behind the move semantics. Looking at this as 'mutable', however is looking at how you do...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10211
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.