|
||||
|
Section 9:
|
[9.2] What's a simple example of procedural integration?
Consider the following call to function g():
void f()
{
int x = /*...*/;
int y = /*...*/;
int z = /*...*/;
...code that uses x, y and z...
g(x, y, z);
...more code that uses x, y and z...
}
Assuming a typical C++ implementation that has registers and a stack, the
registers and parameters get written to the stack just before the call to
g(), then the parameters get read from the stack inside g() and read again
to restore the registers while g() returns to f(). But that's a lot of
unnecessary reading and writing, especially in cases when the compiler is able
to use registers for variables x, y and z: each variable could get
written twice (as a register and also as a parameter) and read twice (when
used within g() and to restore the registers during the return to f()).
void g(int x, int y, int z)
{
...code that uses x, y and z...
}
If the compiler inline-expands the call to g(), all those memory operations
could vanish. The registers wouldn't need to get written or read since there
wouldn't be a function call, and the parameters wouldn't need to get written
or read since the optimizer would know they're already in registers.
Naturally your mileage may vary, and there are a zillion variables that are outside the scope of this particular FAQ, but the above serves as an example of the sorts of things that can happen with procedural integration. |
|||