help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How smart is g++


From: John Max Skaller
Subject: How smart is g++
Date: Sun, 31 Oct 2004 15:37:48 +1100
User-agent: Pan/0.13.3 (That cat's something I can't explain)

How smart is g++ handling references? Consider:

struct X { int j; };

void f(X x)
{
  int j_copy = x.j;
  int & j_ref = x.j;
  ...
}

Is g++ smart enough to make uses of j_copy and j_ref
the same speed? Meaning to use the same instructions?

Or does it treat a reference as a pointer, and have
to dereference every use?

Or does this depend on context, optimisation switches etc?

I'm currently unpacking a structure to individual variables.
The structure isn't otherwise used, other than to be
initialised, similar to the shown example. (Acually it is
passed to a class constructor which does the unpacking
so methods of the class can access the components).

I was thinking to store the actual struct instead,
and change the variables to be references, this saves
unpacking. In my context assume the result will be semantically
equivalent -- this question is about performance.

"Are references to a subcomponent of a struct as fast to
use as the literal expression for that subcomponent"?

In particular, initialisation of 'j_ref' above could take
zero time and storage -- it's a constant offset into the stack just like
the expression x.j.

However, in a class, this may be tricky if the reference
is bound with a constructor, and then used later in a method.
[I might fix that if the overhead for functions is zero by
binding the reference in each method]

Hmm .. did the question make sense?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]