Pass-by-value will result in a copy operation, since the function use a copy of the original input parameter. This can be a very expensive operation, especially for user-defined classes with a lot of members (and base classes).
In a function, which use pass-by-const-reference, no copies are used, the function use the original input parameter (but can not modify it, since it declared const).
This post is actually the first post of two, where I'm comparing the disassembly of pass-by-value code vs. pass-by-const-reference code. In this post, the comparison is done with built-in types.
In the next post, I'm doing the same comparison, but for user-defined classes.
I'm using Visual Studio 2010 Express and building in Debug mode. Why? Because I'm demonstrating very simple examples, which means the compiler will do a lot of optimizations in Release mode. Further, I'm using WinDbg to view the disassembly.
Are you familiar with the calling convention cdecl? This calling convention is used by default for each function you are writing in Visual Studio. What you need to know in this context, is that cdecl will put the arguments on the stack in reversed order. It will be clearer when we are stepping through the examples below.
Okay, let's checkout the example. Below is a very simple program which adds two integers. The addition is done in the function add.
int main() { int a = 3; int b = 7; int c = add(a, b); return 0; }
As mentioned above, I'm going to compare the pass-by-value case with the pass-by-const-reference.
First up is the pass-by-value case. Below is the C++ code and the disassembly of the main function with its stack.
int add(int a, int b) { return a+b; }
WinDbg - disassembly of main (about to execute the call instruction) and its stack |
Then we are ready to execute the add function, i.e. the call instruction. But before making the call, we must put the arguments on stack. Remember that the add function use the cdecl calling convention, which means we pass the arguments to the stack in reversed order, i.e. first push 7 (b), and then push 3 (a).
Also note what's occurs on the stack. The red and green frames is the original values of a and b. The blue and purple frames is the result of the copying, i.e. the push instructions.
Now let's continue with the disassembly of the add function.
WinDbg - disassembly of add function (about to execute the move eax instruction) and its stack |
Next up is the pass-by-const-reference case. Below is the C++ code and the disassembly of the main function with its stack.
int add(int &a, int &b) { return a+b; }
WinDbg - Disassembly of main function (about to execute the call instruction) and its stack |
Above, as in the pass-by-value case, the red and green frame sets the variables a=3 and b=7. But after the initiate of the variables, there is a difference. We push two values to the stack, but not copies of the values, but addresses to the values instead.
The first lea instruction stores the address of 7 (b) in eax, which is then pushed on the stack. The second lea instruction stores the address of 3 (a) in ecx, which is then pushed on the stack.
Then we are ready to execute the add function, i.e. the call instruction.
Also check out the stack. The red and green frames is the original values of a and b (exactly as in the pass-by-value case). The blue and purple frames contains the address of b and a (instead of copies as in the pass-by-value vase).
Now let's continue with the disassembly of the add function.
WinDbg - Disassembly of add function (about to execute the move eax instruction) and its stack |
The move eax, dword ptr [ebp+8] instruction, sets eax to the address of a. Next instruction sets eax to the contents of the address which eax points to, i.e. value 3.
Same procedure is done for b, which is then added to 3 (a) in the instruction add eax, dword ptr [ecx].
To summarize, in the pass-by-value case, copies of the values are pushed on the stack. In the pass-by-const-reference case, the addresses of the values are pushed on the stack.
In the next part of comparing pass-by-value and pass-by-const-reference, I will have a look at user-defined classes.
You are welcome to leave comments, complaints or questions!
No comments:
Post a Comment