• 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
[OFFish] Performance difference between passing smallish structs by reference or value
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Subject: [OFFish] Performance difference between passing smallish structs by reference or value
  • From: Wade Tregaskis <email@hidden>
  • Date: Sun, 23 May 2004 22:28:31 +1000

I know this has nothing to do with Cocoa, but in the absence of any alternative, I figure this is the best "general purpose" list.

Essentially, I'm trying to get a feel for the trade off's between passing by const reference (C++'s "&") and by value, for smallish structs (<50 bytes). From looking at the assembly output from gcc, both without optimisation and with -O2 & -O3, the by value case (surprisingly?) always generates longer code... gcc doesn't output with readability in mind, but so far as I can tell there's extra work required to setup the function call, and then extra work to load parameters within the function itself; i.e. lots of memory movement on and off the stack. By reference doesn't seem to have such issues - I imagine and seem to see that everything's loaded directly from memory to registers as needed in the function, and consequently there's less memory movement (half, I guess) and less code for the function setup.

Is this a valid observation? Is it a good conclusion that even for small structs (16 bytes or so), e.g. NSRect, it's worth passing by reference rather than value? This is going on the assumption or knowledge that all fields of the struct are used in the function, of course.

At what point does gcc start passing parameters in registers, if -O3 doesn't do it? I imagine the results would be quite different, as in my common use I'll have the parameters in registers already by the time I make the function call. I can't inline my functions, despite their simple inputs, as they invariably perform tight loops with many additional function calls.

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

Wade Tregaskis (aim: wadetregaskis)
-- Sed quis custodiet ipsos custodes?
_______________________________________________
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.


  • Prev by Date: Re: Avoiding == and = mixup in if statements
  • Next by Date: Any cocoa password applications, or which NS do i use???
  • Previous by thread: WebKit & Caching Data/Stylesheets
  • Next by thread: Re: [OFFish] Performance difference between passing smallish structs by reference or value
  • Index(es):
    • Date
    • Thread