Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Effect of passing arguments ByVal and ByRef on Performance

Status
Not open for further replies.

jashy

Programmer
May 18, 2000
14
0
0
US
Hi,<br>I am developing business tier objects in VB6 which are to be deployed using DCOM. I am using ActiveX Exe's for my business components. I have a lot of methods and their arguments need to be passed from the user interface tier to these business objects.<br><br>These arguments are not changed in the business tier. So I need not pass them ByRef. I want to know if there is any performance hit in passing arguments ByVal instead of ByRef?<br>Which option is better to achieve optimal performance???<br><br>Thank you<br>jatin<br>
 
We *always* use ByVal.&nbsp;&nbsp;I understand that it avoids more than one set of exchanges between tiers for each time used.<br><br>Another time saver:&nbsp;&nbsp;place all properties in an array, then pass the array as a variant.&nbsp;&nbsp;Performance improvement is significant. <p>John Kisner<br><a href=mailto:jlkisner@jlkisner.com>jlkisner@jlkisner.com</a><br><a href= > </a><br>
 
I think that the performance conserning Time is improved if you use ByRef because its only pointers and not a copy of the parameter that is received! The thing that takes time is the copying of the parameter!!<br><br>But I think that is has to be a lot of function calls before the improvemnet is significant!<br><br><br>Nikolaj
 
I'd agree with John - pass by val where possible. <br><br>When programming in a distributed environment you need to reduce trips over the wire to a minimum. If you pass a reference to an object then you are going back and forth over the network each time you set/get a variable.<br><br>HTH <p>Cal<br><a href=mailto: > </a><br><a href= > </a><br>
 
Sure!!<br><br>Of course!! I didnt have that in mind! Was only thinking local objects!! Sure! Use ByVal when Distribudted apps!<br><br>Nikoalj
 
This thread is another example of how the deployed environment affects COM & DCOM issues.&nbsp;&nbsp;Here the issue is response performance, no issue except in distributed (DCOM) deployment.<br><br>There are related thoughts discussed in the Learning OOP forum. <p>John Kisner<br><a href=mailto:jlkisner@jlkisner.com>jlkisner@jlkisner.com</a><br><a href= > </a><br>
 
Thank you all for your suggestions.<br><br>What Im doing is something similar. I've defined User Defined Types (UDT) in the middle tier. Now I want to accept these UDTs from the UI tier. However VB doesnt allow to accept the UDTs ByVal. They must be accepted ByRef. So a workaround is that in the method declaration in the middle tier, declare the argument as ByVal Variant. However while calling these methods from the UI take care of passing only the appropriate UDT. That way I am able to use UDTs instead of large no. of separate arguments and also able to accept them ByVal.<br><br>However the catch is that the user can pass any variant when a particular UDT is expected as the argument is defined as ByVal Variant.<br><br>Any one has a better suggestion?<br><br>Thanks<br>Jatin
 
If you create the UDT in the Object in the middle tier too and uses this type as the parameter to the method visual basic, as I remeber, makes a Runtime Error if the parameter is of the wrong type. If you make a reference to the object in VB you actually can see that the paramtere has to be of the UDT!<br><br>Nikolaj
 
Nikolaj,<br>I've created the Public UDT in the middle tier. Now when I add a reference to the ActivX exe(MT) in the UI, the UDT is available in the UI.<br><br>Now, In the Middle tier the method accepts a Variant eg:<br>Public Function Test(ByVal arg1 as Variant) as Variant.<br><br>However, in the UI, I do something like this:<br><br>Dim udt1 as UDT<br>dim varRet as Varinat<br><br>&nbsp;&lt;populate the elements of the UDT&gt;<br><br>varRet = MT.Test(udt1)<br><br><br>So Im passing the UDT in a Variant argument ByVal. This is possible. I tested with an ActiveX Exe and a Standard Exe.<br><br><br>
 
Nikolaj,<br>Yes, sort of. Now I can pass the UDTs ByVal to the middle tier which accept them as a Variant.<br>So I achieved both: <br>1&gt; Pass the arguments ByVal to improve the performance and<br>2&gt; Use UDTs in the UI tier to avoid the clumsy method definitions involving large number of arguments.<br><br>Looks like I'll go with this only. The good part is that in the middle tier, after accepting the UDT as a Variant, you can store it back in a variable of type UDT.<br>
 
Hi<br>Passing ByVal has the advantage over ByRef in the sense that it avoids network round trips.<br><br>However, in our application, we need to pass back the result of a SELECT statement on the table which might return more than 25,000 rows at once. We are thinking of passing the query result back to the UI tier in a Variant. (A 2 dimensional array containing the rows and columns of the SELECT statement).<br>While passing such large values, is it advisable to pass them ByVal? Will it not hog up the network when such large data is passed?<br><br>Im sure this is a question that will have to be solved in each DCOM application. I want to know how do you tackle this?<br><br>Thanks<br>Jatin
 
Jatin,<br><br>You are confronting one of the significant performance issues with distributed applications.&nbsp;&nbsp;Our architecture was designed 3 years ago and I expect there are better techniques now available, so maybe others will respond.<br><br>We try to keep our recordsets residing in the 2nd tier (or lower), then passing only selected records up to the UI tier.&nbsp;&nbsp;When necessary for the UI logic to work directly with large data volumes (passing a whole customer table for display & selection) we pass only the required columns (maybe cust name & Number).&nbsp;&nbsp;We use an array as a variant for the actual transfer.<br><br>Of course the use of browser architecture would eliminate the need for large data volumes to be transported to the UI tier. <p>John Kisner<br><a href=mailto:jlkisner@jlkisner.com>jlkisner@jlkisner.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top