473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

why visual studio does not optimize constructor in this case

200 100+
Hello everyone,


Why visual studio does not optimize constructor in this case? I do not understand what the MSDN mentioned,

if use different named object, compiler can not optimize. Why?

http://msdn2.microsoft.com/en-us/library/ms364057(vs.80).aspx

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. class RVO
  3. {
  4. public:
  5.  
  6.             RVO(){printf("I am in constructor\n");}
  7.             RVO (const RVO& c_RVO) {printf ("I am in copy constructor\n");}
  8.             int mem_var;       
  9. };
  10. RVO MyMethod (int i)
  11. {
  12.             RVO rvo;
  13.             rvo.mem_var = i;
  14.       if (rvo.mem_var == 10)
  15.          return (RVO());
  16.             return (rvo); 
  17. }
  18. int main()
  19. {
  20.             RVO rvo;
  21.             rvo=MyMethod(5);
  22. }
  23.  
Output is,

I am in constructor
I am in constructor
I am in copy constructor

My expected output is,

I am in constructor
I am in constructor


thanks in advance,
George
Dec 27 '07 #1
14 2149
chroot
13
Maybe the compiler would optimize if you wrote (in main)

Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2.  
With two lines, the compiler has no other choice then to use the default constructor, then replace with the copy constructor.
Dec 27 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
This is the output I would expect:

I am in constructor
I am in constructor
I am in copy constructor

because in main():
Expand|Select|Wrap|Line Numbers
  1. RVO rvo;
  2.  
has to create an object. Threrefore: I am in constructor
Then in this call:
Expand|Select|Wrap|Line Numbers
  1. RVO MyMethod (int i)
  2. {
  3.             RVO rvo;
  4. }
  5.  
has to create an object. Threrefore: I am in constrcutor

Then the object has to be returned:
Expand|Select|Wrap|Line Numbers
  1. RVO MyMethod (int i)
  2. {     //etc...
  3.       return (rvo); 
  4. }
  5.  
That means a copy. Therefore: I am in copy constrcutor.

Finally, in main():
Expand|Select|Wrap|Line Numbers
  1. rvo=MyMethod(5);
  2.  
Normally, the copy constructor would be called to create an initialize rvo. However, the optimization (NRVO) allows the compiler to keep the returned object and call it rvo, thereby saving a constrcutor call.

Beware that the temporary object returned from MyMethod() does not contain any local pointers or references.
Dec 27 '07 #3
George2
200 100+
Thanks chroot,


Maybe the compiler would optimize if you wrote (in main)

Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2.  
With two lines, the compiler has no other choice then to use the default constructor, then replace with the copy constructor.
I have tried that when using the alternative method you mentioned, the output is,

--------------------
I am in constructor
I am in copy constructor
--------------------

which means it is optimized, cool!

What do you mean *the compiler has no other choice then to use the default constructor, then replace with the copy constructor*? Could you provide more information please? I am interested to learn from you why using your alternative method will trigger compiler to optimize the code to overcome MSDN sample. :-)


regards,
George
Dec 28 '07 #4
George2
200 100+
Thanks weaknessforcats,


I do not quite understand why compiler could not optimize the case. The object returned from MyMethod() is temporary, and we could invoke the assignment operator the outer rvo, which could save one time to call copy constructor.

Why compiler can not optimize in this case, could you provide more description please?

Normally, the copy constructor would be called to create an initialize rvo. However, the optimization (NRVO) allows the compiler to keep the returned object and call it rvo, thereby saving a constrcutor call.

Beware that the temporary object returned from MyMethod() does not contain any local pointers or references.

regards,
George
Dec 28 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
do not quite understand why compiler could not optimize the case. The object returned from MyMethod() is temporary, and we could invoke the assignment operator the outer rvo, which could save one time to call copy constructor.
You can't use the assignment operator on a object that's not built yet.

This is not an assignment:
Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2.  
I know, I know, you see the assignment operator but this is not an assignment. It's an initialization. Initialization requires a constructor. NRVO just lets you use the temporary object retured from MyMethod as rvo.

BTW: This is in every interview I have ever had:
Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2. rvo=MyMethod(5);
  3.  
The question is: How many assignments are there. When you answer 2, you have failed the interview.
Dec 28 '07 #6
George2
200 100+
I agree with you, weaknessforcats. Let us come back to the original question. :-)


http://msdn2.microsoft.com/en-us/library/ms364057(vs.80).aspx

You can compare sample 1 and sample 4. The only differences as I stated in my original question is, sample 4 uses different return paths and this is the root cause why compiler can not use NRVO to optimize. I do not know and understand why different return paths matters whether or not compiler will do the optimization. Any ideas?

You can't use the assignment operator on a object that's not built yet.

This is not an assignment:
Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2.  
I know, I know, you see the assignment operator but this is not an assignment. It's an initialization. Initialization requires a constructor. NRVO just lets you use the temporary object retured from MyMethod as rvo.

BTW: This is in every interview I have ever had:
Expand|Select|Wrap|Line Numbers
  1. RVO rvo=MyMethod(5);
  2. rvo=MyMethod(5);
  3.  
The question is: How many assignments are there. When you answer 2, you have failed the interview.

regards,
George
Dec 29 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
I do not know and understand why different return paths matters whether or not compiler will do the optimization. Any ideas?
Code has been generated to return objects from each of the return paths. Which one should be the Named Returned Value?? Path #1, Path #2 or Path #3 ??? In any ambiguous situation, the compiler backs off optimization as unsafe.

Now I suppose we could philosophize on this but the fact is that's how it works. Other things prevent NRVO and they are in that MSDN topic as well.
Dec 29 '07 #8
George2
200 100+
Hi weaknessforcats,


I do not understand why different return paths will block compiler from optimization, for each path #1, #2 or #3, compiler could optimize separately by using hidden argument technique, because for each return path, without optimization they all need to create temporary object on return stack.

Any ideas why compiler could not optimize?

Code has been generated to return objects from each of the return paths. Which one should be the Named Returned Value?? Path #1, Path #2 or Path #3 ??? In any ambiguous situation, the compiler backs off optimization as unsafe.

Now I suppose we could philosophize on this but the fact is that's how it works. Other things prevent NRVO and they are in that MSDN topic as well.

regards,
George
Dec 30 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
NRVO does not optimize if there are multiple return paths because it's not the same object every time.
Dec 30 '07 #10
George2
200 100+
Hi weaknessforcats,


I agree it is not the same object instance, but each different object is doing similar things, making a temporary object on the function return stack and assign it to the return value, then destruct the temporary object on function stack. This is the area which NRVO covers, why it could not optimize, I do not know.

It is appreciated if you could share some more perpectives. :-)

NRVO does not optimize if there are multiple return paths because it's not the same object every time.

regards,
George
Dec 31 '07 #11
weaknessforcats
9,208 Expert Mod 8TB
You said it yourself: each different object.

NRVO is Named Returned Value Object. Not Objects.

You have to have only one object be the named return value.
Jan 1 '08 #12
George2
200 100+
Hi weaknessforcats,


I am confused. Named Return Value Optimization, is not the same as only one named return value optimization.

You can test that if you have two different variable instances, different names for different return paths, NRVO still does not work.

I am not sure why there is such limitation. I am not sure whether I have missed or mis-understood some important aspects of NRVO, which blocks my understanding of why multiple return names with multiple return paths blocks compiler from optimization. Any ideas?

You said it yourself: each different object.

NRVO is Named Returned Value Object. Not Objects.

You have to have only one object be the named return value.

regards,
George
Jan 2 '08 #13
weaknessforcats
9,208 Expert Mod 8TB
Any ideas?
You can research the C++ language spec. Once you find that multiple return pathes prevent NRVO, then you can try to find the ANSI committee C++ design jnotes where that conclusion was reached.

You will never get to the bottom of this and what you think about it doesn't count. You need to find hard factual documentation to support your position. Nothing I can say is going to help.
Jan 2 '08 #14
George2
200 100+
Thanks all the same, I appreciate your help all the way.

You can research the C++ language spec. Once you find that multiple return pathes prevent NRVO, then you can try to find the ANSI committee C++ design jnotes where that conclusion was reached.

You will never get to the bottom of this and what you think about it doesn't count. You need to find hard factual documentation to support your position. Nothing I can say is going to help.

regards,
George
Jan 3 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Martin Bless | last post by:
The good news: Along with Python-2.4 comes really good news to Windows users. Yes, you now CAN build extension modules yourself using the SAME C++ compiler and linker Python is built with...
19
by: Alf P. Steinbach | last post by:
// As usual the error message directs one to the report the bug. // // And as usual there is absolutely no way to do so without paying for // the privilege... // // Or using three or four hours...
1
by: jmd | last post by:
Hello. I have downloaded and installed Visual Studio 2005 February CTP -- Professional Edition (English) from msdn. All works fine (with a few bugs) except the Quickstarts. The Quickstarts install...
8
by: Will Chamberlain | last post by:
I came across a rather interesting article this morning and thought I'd share. We all know that Visual Studio is a great IDE, but I think we can all agree that it is adds a dramatic change to how...
4
by: pascalroca | last post by:
class myClass { int lacement ; public : myClass(){}; myClass(const myClass& ref){} virtual std::vector< std::auto_ptr< myClass getValeur() = 0; }; typedef std::auto_ptr< myClassmyClassPtr;
3
by: karlag92 | last post by:
We have a very large C# winforms client application that is constructed as a single solution currently with 75 projects. We're currently using VS 2003 but will upgrade to 2005 some time next year....
4
by: George2 | last post by:
Hello everyone, Why Visual Studio compiler can not optimize in this case? I think this case is almost the same as sample 1, why compiler can optimize sample 1 but can not optimze sample 2? ...
4
by: George2 | last post by:
Hello everyone, Why visual studio does not optimize constructor in this case? I do not understand what the MSDN mentioned, if use different named object, compiler can not optimize. Why? ...
0
jwwicks
by: jwwicks | last post by:
Introduction This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the...
2
Markus
by: Markus | last post by:
I recently installed Visual Studio 2008 Professional. On the initial install it gave me a list of options, something to do with optimizing Visual Studio. It gave you the option to optimize it for C#,...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.