473,379 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Passing long Strings as parameters (by ref and by value)

If there are long strings (like 1MB or 2MB) is it more performant to pass
those by ref to methods or by value?
Jun 27 '08 #1
11 6096
On Jun 19, 5:30*pm, Sujeet <Suj...@discussions.microsoft.comwrote:
If there are long strings (like 1MB or 2MB) is it more performant to pass
those by ref to methods or by value?
String is a reference type, so a string parameter passed by value is
still only going to require the size of a reference (i.e. 4 or 8
bytes).

See http://pobox.com/~skeet/csharp/parameters.html

Jon
Jun 27 '08 #2
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
"Sujeet" <Su****@discussions.microsoft.comwrote in message
news:AA**********************************@microsof t.com...
If there are long strings (like 1MB or 2MB) is it more performant to pass
those by ref to methods or by value?

Jun 27 '08 #3
The other posters have answered this fine - but just to add that this
only applies within a single AppDomain; all bets are off when talking
between different AppDomains (remoting / IPC) or processes (IPC) or
COM-interop or over the network. Maybe obvious, but worth noting.

Marc
Jun 27 '08 #4
Tom Dacon <td****@community.nospamwrote:
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.
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.

Here's an example:

using System;

class Test
{
static string text;

static void Main()
{
Method(ref text);
}

static void Method(ref string foo)
{
Console.WriteLine ("text = " + text);
foo = "hi";
Console.WriteLine ("text = " + text);
}
}

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #5
Sorry, Jon - that's what I get for not checking the IL before I reply off
the top of my head.

I've seen so much prolog/epilog code in so many languages - and written
some, for that matter - that they all sort of merge into one another after
awhile. Basically, they all have to solve the same problem but the details
tend to differ.

Tom

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Tom Dacon <td****@community.nospamwrote:
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.

Jun 27 '08 #6
Jon Skeet [C# MVP] wrote:
Tom Dacon <td****@community.nospamwrote:
>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.

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.
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.
Jul 2 '08 #7
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:
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.

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

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 2 '08 #8


"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:
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.

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.

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...
And that's one of the worst features of new languages... you can address
remote objects and "odd" behavior and the syntax never even hints you're
doing so.

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. Course that only helps a little, since
functions still vary in complexity from "almost free" to "wait for a
response from the moon", but I guess you can't have everything.

VB6 was the worst, because of the whole Let/Set syntax. So:

I = I + 1

could invoke two remote calls (one to I's get, one to I's let). Ugh!

Best thing to do is consider that anytime you pass something by reference,
you've transferred ownership of that variable to the called function for the
duration.
>
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 6 '08 #9
Ben Voigt [C++ MVP] wrote:
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.
Strongly disagree. operator= could be doing anything.

-- Barry

--
http://barrkel.blogspot.com/
Jul 8 '08 #10
On Jun 19, 1:02*pm, "Tom Dacon" <tda...@community.nospamwrote:
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

"Sujeet" <Suj...@discussions.microsoft.comwrote in message

news:AA**********************************@microsof t.com...
If there are long strings (like 1MB or 2MB) is it more performant to pass
those by ref to methods or by value?- Hide quoted text -

- Show quoted text -
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.)
Jul 9 '08 #11
G.S. <gs******@gmail.comwrote:
To continue the educational nature of this thread for advanced newbies
like me :)...
So what happens if the string is passed by value
The *reference* to the string is passed by value, just to keep it
clear. The object isn't passed at all.
and the callee changes the string in a way that involvs string's
contents (example - Replace all occurences of ' with space?
You can't do that - strings are immutable. That's why methods like
Replace return a new string.
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)?
If you do:

parameter = parameter.Replace("'", " ");

then that would indeed create a new string, and change the value of the
parameter to refer to that new string.
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.)
Indeed.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 9 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
5
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the...
13
by: Matthias Kaeppler | last post by:
Hi, I was wondering why library implementors often make getter functions return strings by value (copies). For example, in boost::filesystem the leaf() function returns an std::string by value....
1
by: Dave Parry | last post by:
Hi, I would like to hear peoples thoughts on passing a value type by reference and then trying to store this ref in a member so it can be updated elsewhere, whereby I want the referenced value...
3
by: Tor Inge Rislaa | last post by:
Passing and using parameters How to get hold of the parameter value provided by the URL? When I open a web form with a DataSource I often include a parameter in the URL as below: ...
4
by: xkeops | last post by:
I have forgotten how vb.net deals with long strings, I know I used this once but I don't remember. Or was it C# ??? dim s as string = @"string1 string2 string3" instead of
5
by: CyberSoftHari | last post by:
I am trying to get an output Parameters value from a stored procedure using sqlDataSource in asp.net 2.0. But I only get a null value for the output Parameters. Can someone Point me to get value? ...
2
by: jalil.feghhi | last post by:
I have an access database that has a field (type memo) that keeps very long strings. When the value of this field is very long, my Visual Basic 6.0 code does not return the whole value and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.