• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [OFFish] Performance difference between passing smallish structs by reference or value
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [OFFish] Performance difference between passing smallish structs by reference or value


  • Subject: Re: [OFFish] Performance difference between passing smallish structs by reference or value
  • From: Allan Odgaard <email@hidden>
  • Date: Mon, 24 May 2004 04:27:59 +0200

On 23. May 2004, at 14:28, Wade Tregaskis wrote:

Anyway, I thought I'd ask because even given my observations, nearly every performance-critical sample of code I've seen still passes by value (e.g. OpenGL, as a prime example). I assume I'm missing something important... if so, I'd like to know before I go and rejig 400 function declarations. :)

I generally use const reference and I see a lot of by-value which I honestly think is because the original coder was sloppy.

Another penalty for by-value is when the type is non-POD, i.e. have a copy-constructor/destructor which does some work, cause then this will be executed when passing the value.

But in performance-critical code there might actually be a reason to use by-value because it allows for better optimizations in the function body. When passing by-value all data is copied onto the local stack-frame and can be read from there w/o problems. When passing by-reference you are actually giving a pointer to the data, so each time a member variable is needed, the pointer has to be dereferenced -- the compiler might be able to cache the member variables in registers (as it most likely did in your small tests), but this can only be done when it has enough information to do the proper alias-analysis to prove that local assignments do not change the pointed-to data.

E.g. take this (atypical) function:

void count (T* first, T* last, T const& element, size_t& res)
{
for(; first != last; ++first)
res += element == *first ? 1 : 0;
}

Unless inline, the compiler cannot know if 'element' and 'res' point to the same storage, and thus adding to 'res' *may* change 'element', which require the pointer (to 'element') to be de-referenced on each iteration. Had we instead given 'element' by-value, we would not have this problem.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: [OFFish] Performance difference between passing smallish structs by reference or value
      • From: Robert Goldsmith <email@hidden>
References: 
 >[OFFish] Performance difference between passing smallish structs by reference or value (From: Wade Tregaskis <email@hidden>)

  • Prev by Date: Re: NSPoint and automatic constructor
  • Next by Date: Re: tableView:sortDescriptorsDidChange: question
  • Previous by thread: Re: [OFFish] Performance difference between passing smallish structs by reference or value
  • Next by thread: Re: [OFFish] Performance difference between passing smallish structs by reference or value
  • Index(es):
    • Date
    • Thread