S
Sujeet
If there are long strings (like 1MB or 2MB) is it more performant to pass
those by ref to methods or by value?
those by ref to methods or by value?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
If there are long strings (like 1MB or 2MB) is it more performant to pass
those by ref to methods or by value?
Tom Dacon said:Sujeet, string objects in C# and VB are reference-type objects. They're
allocated on the heap, and your variable contains a reference to the actual
string data on the heap. So when you pass a string of any length to a method
you're always passing a "pointer" (so to speak) to the actual string data,
rather than the actual string data itself, no matter whether you pass it by
value or by reference.
So the performance implications of passing that "pointer" by reference
versus by value are small compared to what would be the cost if the actual
string data were being passed on the stack. When you pass by value, the
compiled code merely sends the "pointer" off to the called method and
forgets about it. When you pass by reference, the compiled code sends the
"pointer" off to the called method, and after the method has completed its
processing the (possibly updated) value is returned to the calling method's
variable. So it's just a matter of one or two IL instructions, probably.
Jon said:That's not quite the way it works. The value isn't "copied back" to
the variable after the method is executed. The variable *itself* ends
up being passed, rather than the variable's value. In other words,
every time the method changes the value of the parameter, that change
is *immediately* visible in the variable, because that's what's being
updated. That's hard to see if it's a local variable, but very visible
if it's an instance/static variable.
Ben Voigt said:Unless you call using BeginInvoke/EndInvoke, or it's a remote object, or
called through COM, or...
In any case where marshalling is involved, the update is propagated back
exactly once, at the end of the call.
Jon Skeet said:True - but at that point it's not really being a language feature and
more, it's to do with the remoting involved. Any number of things
become "odd" when marshalling is involved...
Ben said:At least with standard C++, if something looks like a variable reference, it
is a variable reference. The worst performance hit you could have is
dealing with a VM cache miss.
Sujeet, string objects in C# and VB are reference-type objects. They're
allocated on the heap, and your variable contains a reference to the actual
string data on the heap. So when you pass a string of any length to a method
you're always passing a "pointer" (so to speak) to the actual string data,
rather than the actual string data itself, no matter whether you pass it by
value or by reference.
So the performance implications of passing that "pointer" by reference
versus by value are small compared to what would be the cost if the actual
string data were being passed on the stack. When you pass by value, the
compiled code merely sends the "pointer" off to the called method and
forgets about it. When you pass by reference, the compiled code sends the
"pointer" off to the called method, and after the method has completed its
processing the (possibly updated) value is returned to the calling method's
variable. So it's just a matter of one or two IL instructions, probably.
Instead, you should primarily consider whether or not the called method has
any reason to change the "pointer" to point to a different string during its
processing. If not, pass it by value. If so, pass it by reference, but
document in the calling method that the called method can potentially change
the variable's contents to point to a different string.
For other languages the performance differences can be extreme. But for C#
and VB we get a free pass.
"performant" - jeez, I hate that word. Even the guy who coined it a long
time ago later got defensive about it. Wish I could remember his name. I
used to see him speak at the VBITS conferences back in the early days of
.Net. He worked on the CLR.
HTH,
Tom Dacon
Dacon Software Consulting
- Show quoted text -
...G.S. said:To continue the educational nature of this thread for advanced newbies
like me...
So what happens if the string is passed by value
and the callee changes the string in a way that involvs string's
contents (example - Replace all occurences of ' with space?
Does this create a modified copy of the (entire 2 MB) string on the
heap and re-points the local variable to that new copy (which pointer
is then discarded when the callee ends)?
I guess that is not relevant to initial question of performance of
byref vs. by val, because the same things happen if the string is
passed by reference and modified that way (with the only difference
being that the caller variable (pointer) is updated upon return.)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.