Re: [OFFish] Performance difference between passing smallish structs by reference or value
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: Robert Goldsmith <email@hidden>
- Date: Mon, 24 May 2004 03:42:19 +0100
>
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.
>
This would be true if the cpu did not play a part in helping things
along. As it is, although the compiler does not know if element and res
are the same, the cpu's cache management certainly will. As a result,
any changes to one or the other will be written back to the cache (as
they will both have been brought into the cache well ahead of time by a
compiler signalled pre-fetch) and the speed hit will be the same
whether the parameters were by-reference or by-value. With the G4 and
G5, with their 'triggered' sequential prefetch (where they notice
trends and go off fetching memory ahead of required, repeating the
pattern until the cpu notices the pattern was broken), even larger
parameters which don't fit in the L1 cache have a limited performance
hit.
As a result, the only difference really is the extra copy, push, pull
stages for a by-value pass. Therefore, unless it is not possible to
de-reference the by-reference pointer (e.g. DO or graphics card stuff),
or you are dealing with primitive types such as ints and chars (most of
which are 4 bytes or less anyhow), by reference is the best option in
all cases I can think of, assuming you don't want to create a duplicate
object - and, tbh, using the pass-by-value system to do that is just
bad programming, imho.
Robert
---
GnuPG public key:
http://www.Far-Blue.co.uk
[demime 0.98b removed an attachment of type application/pgp-signature which had a name of PGP.sig]
_______________________________________________
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.