help-gplusplus
[Top][All Lists]
Advanced

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

Re: How smart is g++


From: Guy Harrison
Subject: Re: How smart is g++
Date: Wed, 03 Nov 2004 17:59:52 GMT
User-agent: KNode/0.7.7

John Max Skaller wrote:

> 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?

"dunno" (see below)
 
> Or does it treat a reference as a pointer, and have
> to dereference every use?

That's generally how compilers implement references. Typically optimisation
is done at a lower level rather than at a higher level. Thus, instead of
implementing a reference as an alias you get pointers instead.
 
> Or does this depend on context, optimisation switches etc?

Yes. -O0 will yield very dumb code (ideal for human to follow in asm)
whereas -O2 and higher really go to town. Pass -S with -O0 then with, say,
-O2 and look at the asm. Very different.

> 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"?

Possibly.

> 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.

Possibly.
 
> 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?

Mostly. Depends why you're doing it. For instance, under winapi it can be
useful to bind references to a winapi POD and/or implement getter/setter
methods to ease use - obvious example is basic_string<>/TCHAR[] transitions
in addition to hiding all the error checking crud (that often gets
omitted).

In contrast, if one were reading this struct only the once, as you imply,
then the design may be better off dispensing with that struct altogether -
or simply write an "import module" to read it once, thereafter write out
something more suitable. Alternatively one might decide to dispense with
the C++ wrapper and access the struct directly.

There's lots of grey areas in between, depending upon your criteria. Fwiw,
unless you've got/encountered a good reason to do otherwise, stick with
whatever method will be most robust.



reply via email to

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