|
You can have VC++ generate an ASM listing file.
Maxwell Chen
|
|
|
|
|
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
class RVO
{
public:
RVO(){printf("I am in constructor\n");}
RVO (const RVO& c_RVO) {printf ("I am in copy constructor\n");}
int mem_var;
};
RVO MyMethod (int i)
{
RVO rvo;
rvo.mem_var = i;
if (rvo.mem_var == 10)
return (RVO());
return (rvo);
}
int main()
{
RVO rvo;
rvo=MyMethod(5);
}
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
|
|
|
|
|
does this limitation applies in your case ?
Multiple return paths (even if the same named object is returned on all paths) with EH states introduced.
There are 2 return paths in your MyMethod .
|
|
|
|
|
Thanks Maximilien,
What means EH states?
I do not know why diferent execution path will disable compiler from optimization. In the sample, there are two execution paths to return,
1. return (RVO());
In case (1), compiler could optimize by saving the creating of the temporary object and assign to the extern rvo object directly.
2. return (rvo);
In this case (2), compiler could optimize by not creating the temporary object for the return value, and assign the inner rvo to outter rvo directly.
Why compiler can not optimize it?
regards,
George
|
|
|
|
|
George_George wrote: What means EH states?
The exception handling state.
Maxwell Chen
|
|
|
|
|
Thanks Maxwell,
I have provided my perpective below why the compiler should be able to optimize the code, and it is appreciated if you could review and comment.
regards,
George
|
|
|
|
|
"Named Return Value Optimization" is mainly Stan Lippman's work. As you have read that there are three exceptions which NRVO do not take place.
Different paths returning different named objects.
Multiple return paths (even if the same named object is returned on all paths) with EH states introduced.
The named object returned is referenced in an inline asm block.
Let's see your example (example 7 in MSDN). In function RVO MyMethod(int i) , there are two return paths (see below). They are different intermediate object names. The compiler can not simplify these two return path into one.
RVO MyMethod (int i){
RVO rvo;
rvo.mem_var = i;
if (rvo.mem_var == 10)
return (RVO());
return (rvo);
}
Maxwell Chen
|
|
|
|
|
Thanks Maxwell,
I can understand the real path for execution could only be decided at runtime.
But I do not know why which named variable/return path matters the optimization -- the optimization only happens when the function returns and compiler only needs to insert optimization code at the place where there is a return statement -- if there are multiple returns, the compiler could put multiple optimization into multiple return statement.
I think no matter which one will return, either rvo or RVO(), the return value is a temporary obejct, and compiler could optimize it by using assignment operator on the outside rvo object instance directly without creating the temporary object, right?
Why the named variable/return path matters? Could you provide more information about your analysis please?
regards,
George
|
|
|
|
|
George_George wrote: But I do not know why which named variable/return path matters the optimization
The term "optimization" refers to two aspects: (1) efficiency , and (2) code size .
(1) The efficiency concept: Just like finding the shortest path.
(2) The code size issue: If many return paths can be simplified, concluded, and compacted into one single return path, the code size will be smaller. And maybe it would be more efficient.
Maxwell Chen
|
|
|
|
|
Thanks Maxwell,
I think you mean there is room for compiler to optimize the speed of the code, but considering the binary size will be larger, the code can not be optimized, right?
But I am confused that I assign the /O2 (optimize for max speed) and it should work and does not consider the resulting binary size -- which is the purpose of /O1 (optimize for small size). Any comments?
regards,
George
|
|
|
|
|
George_George wrote: RVO rvo;
rvo.mem_var = i;
if (rvo.mem_var == 10)
return (RVO());
return (rvo);
IMHO, In this case the optimization will be difficult and sometimes useless. That may be the reason the code goes untouched.
Suppose the return value pass by hidden reference,as per the article,
1. If value of i == 10, it have to construct/copy the return value once again after initialization as the newly cosntructed object depends on another location of memory. which gives no advantage on optimization
2.As per the article, the NRVO is basically on workes with single return objects.
In the following case, the optimization dint work.
RVO rvo;<br />
<br />
if ( 11 == 10)<br />
return (RVO());<br />
rvo.mem_var = i;
return (rvo);
In the following case compiler damn sure that
if ( 11 == 10)<br />
return (RVO());
The above case will never works, so NRVO can be applied but still it's missing I believe.
Anyway I'm not so expert in optimization related things, please share you view points.
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
Thanks for sharing your perpective, Sarath!
Two more comments,
1. I do not quite understand why you think below in the case there is no room to optimize? Could you provide more description please?
Sarath. wrote: 1. If value of i == 10, it have to construct/copy the return value once again after initialization as the newly cosntructed object depends on another location of memory. which gives no advantage on optimization
2. here is my perpective mentioned before, please feel free to comment and correct.
--------------------
I do not know why which rvo instance will return matters. I think no matter which one will return, either rvo or RVO(), the return value is a temporary obejct, and compiler could optimize it by using assignment operator on the outside rvo object instance directly without creating the temporary object, right?
Why the path matters? Could you provide more information about your analysis please?
--------------------
regards,
George
|
|
|
|
|
George_George wrote: 1. I do not quite understand why you think below in the case there is no room to optimize? Could you provide more description please?
Sarath. wrote:
1. If value of i == 10, it have to construct/copy the return value once again after initialization as the newly cosntructed object depends on another location of memory. which gives no advantage on optimization
I was talking based on the article.
Please check Hidden argument with/without NRVO[^]
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
i have a for loop like this
for(int i=0;i=3;i++)
{
cout<<"hello";
}
Ans :it works infinite times
can anybody explain me the reason for this for loop
|
|
|
|
|
Hint:
the expression
i=3;
evaluates to 3 , i.e. non-zero.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Deepu Antony wrote: i=3;
try i==3; , or better yet 3==i;
You may be right
I may be crazy
-- Billy Joel --
Within you lies the power for good - Use it!
|
|
|
|
|
It loops infinitely because i=3 (the conditional part of the for() loop) always evaluates to non-zero.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
|
Hi All,
I want to change some edit line to be read only but when i change the control properties i see that the control is in gray color -
I want that the control will stay in withe color - how can i do it ?
|
|
|
|
|
A read-only control and a disabled (gray) control are two different things. Which do you want?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I did not said anything about disable the control.
I mean readOnly.
|
|
|
|
|
You will have to color the control yourself.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
You may handle the WM_CTLCOLORSTATIC message in the Edit Box parent window.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Hello every1.
I need to read the log file which is being updated every second and it is growing.
How do i have to approach that if i want to receive only 'newly' updated entries in the logfile rather than parsing it from the beggining?
Is there any good example how to do that?
|
|
|
|
|
Just store the end-of-file position, for instance (oversimplified standard C snippet)
last_pos = _lseek( fp, 0L, SEEK_END );
then, later on, start reading from recorded position
_lseek( fp, last_pos, SEEK_SET );
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|