[openbeos] Re: Pointer or reference?

  • From: Ingo Weinhold <bonefish@xxxxxxxxxxxxxxx>
  • To: openbeos@xxxxxxxxxxxxx
  • Date: Sun, 28 Mar 2004 23:23:57 +0200

On 2004-03-28 at 22:47:03 [+0200], anqe wrote:
> > references seem a bit more efficient to me:  pointers require allocation
> of a 4 byte space,
> > initialization to the address to which they point and dereferencing takes
> a small amount of extra
> > time.  i personally like to USE pointers, but when it comes to the design
> of an OS, shouldn't we
> > incorporate everything we know?  if we are going to be in the media_kit
> for example and are trying
> > to push huge amounts of data around with little latency, perhaps using
> references is different
> > enough to improve the speed?
> 
> If anything const char* defenitions probably allow the compiler to generate
> more efficient code and way. I've only had limited experience with writting
> compilers and parsers, but in theory passing byref is telling the compiler
> that your passing it something that could be constant and more importantly a
> code time constant but that can't be guaranteed so the compiler has to
> assume its a normal variable.

I'm not sure, what you mean by `code time constant', but passing something 
per reference means nothing else than that it is not passed by value. The 
`const' concept has nothing to do with this; a reference can refer to both 
constants and mutables.

> Either way the 4B is still gonna be transferred somewhere along the line,
> via stack though the compiler nowadays might be passing it through in
> fastcall.
> 
> That &string is gonna make the compiler generate an "lea esi, string" which
> could possibly be avoided by using a constant pointer to the string. My
> reasoning for this is that the compiler knows where it should be
> initializing the variable and in some cases can make the assumption as to
> where it is, allowing the lea to be avoided.
> 
> I could be off the mark, if so just ignore me (I try my best to ignore me)
> but it seems that if worrying about 4B of memory is an issue then there are
> probably other places to look for speed first. Condition hoisting and
> appropriate usage of case and nested if structures would probably yield more
> performance than chassing all calls.

Passing a reference to a function is not any different from passing a pointer 
to it. In both cases the address of the referenced object must be passed.

The following two functions, for instance, are compiled to exactly the same 
executable code:

extern "C"
void
add_one(int& var)
{
        var++;
}

extern "C"
void
add_one(int* var)
{
        (*var)++;
}

CU, Ingo

Other related posts: