performance hit using byref or byval?  
Author Message
Lobmeyer





PostPosted: Tue Oct 26 12:04:13 CDT 2004 Top

Winforms >> performance hit using byref or byval?

New to winforms, I'm trying to break up my code to not have so much in the
main form, so I'm moving code outside into reusable code. My concern is that
I'm passing huge objects like entire datagrids BYREF to modify and molest in
my functions. Are there any performance issues I need to worry about? I want
my app to use as little memory and cpu resources as possible.

-Max

DotNet173  
 
 
Tom





PostPosted: Tue Oct 26 12:04:13 CDT 2004 Top

Winforms >> performance hit using byref or byval? You're not actually passing the huge object around when you use byref. All
you're passing is the reference to the object, which resides on the heap.
This is a bit subtle, but this is how it works:

When you pass a reference to an object by value, the receiving method
receives a copy of the reference, guaranteeing that when the method returns
to the calling code that the reference will be unchanged (i.e., will not
point to a different object on the heap).

When you pass a reference to an object by reference, the receiving method is
in essence operating directly on the caller's variable, and could in
principle change it so that it points to some new object of the same type.

In either case, the cost of passing the object reference is very slight, so
you should make your choice about passing convention on other
considerations, such as whether you want to protect the variable's value
from the called method, or whether the called method is authorized to change
it.

These rules apply to objects created on the heap. Objects created on the
stack (structs/Types), I believe, have other considerations.

HTH,
Tom Dacon
Dacon Software Consulting



> New to winforms, I'm trying to break up my code to not have so much in the
> main form, so I'm moving code outside into reusable code. My concern is
> that I'm passing huge objects like entire datagrids BYREF to modify and
> molest in my functions. Are there any performance issues I need to worry
> about? I want my app to use as little memory and cpu resources as
> possible.
>
> -Max
>


 
 
Max





PostPosted: Tue Oct 26 14:41:58 CDT 2004 Top

Winforms >> performance hit using byref or byval? Ok that puts my mind at ease. I guess what I was reading is that byref
causes more roundtrips in terms of speed rather than the memory usage a
byval might cause. Byval might be faster if I don't mind the memory
overhead. I suppose the issue is insignificant as long as I'm not byrefing a
datagrid a million times in a huge loop or something and expecting fast
results.

-Max





> You're not actually passing the huge object around when you use byref. All
> you're passing is the reference to the object, which resides on the heap.
> This is a bit subtle, but this is how it works:
>
> When you pass a reference to an object by value, the receiving method
> receives a copy of the reference, guaranteeing that when the method
> returns to the calling code that the reference will be unchanged (i.e.,
> will not point to a different object on the heap).
>
> When you pass a reference to an object by reference, the receiving method
> is in essence operating directly on the caller's variable, and could in
> principle change it so that it points to some new object of the same type.
>
> In either case, the cost of passing the object reference is very slight,
> so you should make your choice about passing convention on other
> considerations, such as whether you want to protect the variable's value
> from the called method, or whether the called method is authorized to
> change it.
>
> These rules apply to objects created on the heap. Objects created on the
> stack (structs/Types), I believe, have other considerations.
>
> HTH,
> Tom Dacon
> Dacon Software Consulting
>


>> New to winforms, I'm trying to break up my code to not have so much in
>> the main form, so I'm moving code outside into reusable code. My concern
>> is that I'm passing huge objects like entire datagrids BYREF to modify
>> and molest in my functions. Are there any performance issues I need to
>> worry about? I want my app to use as little memory and cpu resources as
>> possible.
>>
>> -Max
>>
>
>


 
 
Herfried





PostPosted: Tue Oct 26 15:42:26 CDT 2004 Top

Winforms >> performance hit using byref or byval?
> Ok that puts my mind at ease. I guess what I was reading is that byref
> causes more roundtrips in terms of speed rather than the memory usage a
> byval might cause. Byval might be faster if I don't mind the memory
> overhead. I suppose the issue is insignificant as long as I'm not byrefing
> a datagrid a million times in a huge loop or something and expecting fast
> results.

I would change the 'ByRef' to 'ByVal' in your particular case.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

 
 
Tom





PostPosted: Tue Oct 26 17:53:54 CDT 2004 Top

Winforms >> performance hit using byref or byval? The main point is that the object itself sits quietly on the heap and
doesn't get copied or moved in either case. Just the references get passed
around.

An argument passed by reference probably causes a couple of extra IL
instructions per call, to copy the value of the reference back into the
caller's variable. Compile a small example and look at the IL code in ILDASM
to see the details.

Making a million calls to anything in a huge loop would cause a noticeable
effect, I suppose, even if you didn't pass any arguments at all. The
difference between byval and byref would probably not be noticeable even in
that case. My preference is to always pass input arguments by value except
in extraordinary circumstances, and I can't even remember the last time I
had to do it.

Here are some good rules:
Use BYVAL if you're supplying an input value to the method that the
called method will either only look at, or change some property of
Use BYREF if you're supplying an initial value to the method, and the
method is allowed to change the reference to point to a new object
Use OUT if you don't want to supply an initial value to the method, but
the method will return a reference to a new object in the argument variable

For VB.Net, the keywords are ByVal and ByRef; I don't think VB.Net has the
concept of an output-only parameter but I may be wrong;
For C#, the keywords are 'ref' for passing by reference, and 'out' for
passing for output; passing by value is the default and there's no keyword
for it.

Tom Dacon
Dacon Software Consulting



> Ok that puts my mind at ease. I guess what I was reading is that byref
> causes more roundtrips in terms of speed rather than the memory usage a
> byval might cause. Byval might be faster if I don't mind the memory
> overhead. I suppose the issue is insignificant as long as I'm not byrefing
> a datagrid a million times in a huge loop or something and expecting fast
> results.
>
> -Max
>
>
>


>> You're not actually passing the huge object around when you use byref.
>> All you're passing is the reference to the object, which resides on the
>> heap. This is a bit subtle, but this is how it works:
>>
>> When you pass a reference to an object by value, the receiving method
>> receives a copy of the reference, guaranteeing that when the method
>> returns to the calling code that the reference will be unchanged (i.e.,
>> will not point to a different object on the heap).
>>
>> When you pass a reference to an object by reference, the receiving method
>> is in essence operating directly on the caller's variable, and could in
>> principle change it so that it points to some new object of the same
>> type.
>>
>> In either case, the cost of passing the object reference is very slight,
>> so you should make your choice about passing convention on other
>> considerations, such as whether you want to protect the variable's value
>> from the called method, or whether the called method is authorized to
>> change it.
>>
>> These rules apply to objects created on the heap. Objects created on the
>> stack (structs/Types), I believe, have other considerations.
>>
>> HTH,
>> Tom Dacon
>> Dacon Software Consulting
>>


>>> New to winforms, I'm trying to break up my code to not have so much in
>>> the main form, so I'm moving code outside into reusable code. My concern
>>> is that I'm passing huge objects like entire datagrids BYREF to modify
>>> and molest in my functions. Are there any performance issues I need to
>>> worry about? I want my app to use as little memory and cpu resources as
>>> possible.
>>>
>>> -Max
>>>
>>
>>
>
>