1. Miguel Leeuwe
  2. PowerBuilder
  3. Tuesday, 17 March 2020 15:43 PM UTC

Hi,

Long time ago, I read somewhere that objects passed to functions or events, are always internally passed by reference, even if your specify "by value" or "readonly" in your function or event declaration.

So until now, I use by reference if I change anything and I use readOnly (only) to indicate that the object is readonly, but really HOPING that internally the object is still passed by reference for speed issues.

Is this really true or should I pass by ref if I want speed improvements?

regards

 

Miguel Leeuwe Accepted Answer Pending Moderation
  1. Tuesday, 17 March 2020 19:30 PM UTC
  2. PowerBuilder
  3. # 1

Maybe I've not been clear, but what I'm most interested in, is to know whether an object would be passed by value, being a copy of the original, when specified. I thought it was NEVER, but Chris - with all respect - says this is a rumour?

See where the rumour comes from (for example):

http://codeverge.com/sybase.powerbuilder.general/pass-by-reference-vs-java-pass-by/1017088

or even from the pb help:

From the help of pb 2019, I find the information confusing and guess I'll just have to do a test app passing huge objects and see if memory is affected when passing an object by value, vs. passing readlonly and by ref.

Thanks you all for answering.

 

Passing objects

When you pass an object to a function or event, the object must exist when you refer to its properties and functions. If you call the function but the object has been destroyed, you get the execution error for a null object reference. This is true whether you pass by reference, by value, or read-only.

To illustrate, suppose you have a window with a SingleLineEdit. If you post a function in the window's Close event and pass the SingleLineEdit, the object does not exist when the function executes. To use information from the SingleLineEdit, you must pass the information itself, such as the object's text, rather than the object. When passing an object, you never get another copy of the object. By reference and by value affect the object reference, not the object itself.

Objects passed by value

When you pass an object by value, you pass a copy of the reference to the object. That reference is still pointing to the original object. If you change properties of the object, you are changing the original object. However, you can change the value of the variable so that it points to another object without affecting the original variable.

Objects passed by reference

When you pass an object by reference, you pass a pointer to the original reference to the object. Again, if you change properties of the object, you are changing the original object. You can change the value of the variable that was passed, but the result is different -- the original reference now points to the new object.

Objects passed as read-only

When you pass an object as read-only, you get a copy of the reference to the object. You cannot change the reference to point to a new object (because read-only is equivalent to a CONSTANT declaration), but you can change properties of the object.

Comment
  1. Miguel Leeuwe
  2. Wednesday, 18 March 2020 09:19 AM UTC
Thank you for this valuable information René.
  1. Helpful
  1. Chris Pollach @Appeon
  2. Wednesday, 18 March 2020 14:13 PM UTC
Hi René ... correct. That aspect was one of the well expanded topics in the old "BOOA" Course - which unfortunately, is no longer. :-(
  1. Helpful
  1. Ricardo Jasso
  2. Wednesday, 18 March 2020 14:41 PM UTC
Well, you always learn something new :) Thanks for the observation René. Just one point, according to the Help manual passing an autoinstantiated object to a function as read-only passes a copy of the object but that copy cannot be modified. So by value and as read-only act the same except that no modifications can be done to the latter.
  1. Helpful
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Tuesday, 17 March 2020 18:00 PM UTC
  2. PowerBuilder
  3. # 2

Hi, Miguel -

Lacking familiarity with your application, about all I can say is: "it depends".

Modern CPU's & graphics cards are so blazing fast anymore that unless your app makes a LOT of function calls in a short period of time and/or passes large amounts of information from the calling code to the called code on a routine basis, I would hesitate to pass all or most function arguments by reference only in the hope of gaining a teeny-tiny boost in performance, but my opinion is based on the history of the types of apps I've worked on over the years...none of these needed to resort to this coding technique. That said - I think you're the only/best one qualified to make this call.

If you go this route, I'd include a notification/disclaimer in each applicable function script that states you're passing arguments by ref on purpose to boost performance by reducing the amount of data passed into the function. Code can outlive us, and self-documenting code is never a bad thing in my book.

Regards, John

Comment
  1. Miguel Leeuwe
  2. Tuesday, 17 March 2020 18:55 PM UTC
Hi John,

Thank you for your answer and yes you are right, but ... it leaves my main question unanswered:

Is any object, being passed to a function or event, internally always passed by reference, even if I specify differently in the parameter declaration?

regards,

MiguelL
  1. Helpful
There are no comments made yet.
Ricardo Jasso Accepted Answer Pending Moderation
  1. Tuesday, 17 March 2020 19:11 PM UTC
  2. PowerBuilder
  3. # 3

Miguel,

Objects are never copied when passing them as arguments either by value, by reference, or by read only. What gets copied or referenced is the reference (pointer) to the object, not the object itself. So any changes to the object inside the function will change the object that was created outside of it.

Passing by value makes a copy of the pointer. If you change it, the original pointer remains unaffected. Passing by reference passes the pointer by reference and if you change it the original pointer will get changed. Passing by read only is similiar to by reference but you cannot change the pointer. I don't see any possible speed improvement of using either of the three.

Simple variables and structures behave differently. Just as you would expect when using either by value, by reference, or by read only as their name implies. Passing by value makes a copy of the variable or structure so if the content of the variable or structure is large there is some speed improvement by using by reference or by read only, although maybe not detectable due to the speed of today's machines.

Regards, Ricardo

 

Comment
  1. Ricardo Jasso
  2. Tuesday, 17 March 2020 20:25 PM UTC
"...would that not mean that in memory a COPY has taken place for the passed pointer by value to be pointing to a different memory segment?"



No, it will be pointing to the same memory segment. Since it is a copy of the original pointer, if you change it to point to a different memory segment (e.g. by using it in a CREATE statement), the original pointer won't get affected and will still be pointing to the original memory segment.
  1. Helpful
  1. Ricardo Jasso
  2. Tuesday, 17 March 2020 20:27 PM UTC
If you pass it by reference and then use it in a CREATE statement then the original pointer will now point to the new memory segment, and the original memory segment will get orphaned.
  1. Helpful
  1. Chris Pollach @Appeon
  2. Tuesday, 17 March 2020 21:16 PM UTC
Hi Ricardo ... Yes (yikes) that would certainly have that negative orphaned memory effect! :-(

BTW: The topic of REF, Read-Only and Value was well covered in the old "PB Performance" course. The course had great examples of memory use and also CPU use when using the Value vs the other two options. Too bad that old course was shelved by Sybase. Some very valuable lessons were in there and most of those are still relevant today in Appeon PB releases. Of course, I am an old (no wise cracks here) programmer who cut his teeth in Assembler - so I am a bit (no pun intended here) of a performance stickler - LOL!.
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 17 March 2020 19:03 PM UTC
  2. PowerBuilder
  3. # 4

Hi Miguel;

  That rumor is false. They are passed as defined.

For performance through-put in an intense processing loop for example, you should always try to pass method arguments by either Read-Only or Reference.   ;-)

Regards ....Chris

Comment
  1. Miguel Leeuwe
  2. Tuesday, 17 March 2020 19:10 PM UTC
Thank you Chris. That answers my question. So ... passing as readOnly uses a pointer and does not copy the value into the parameter?
  1. Helpful
  1. Chris Pollach @Appeon
  2. Tuesday, 17 March 2020 19:40 PM UTC
Correct ... The REF & ReadOnly settings are the same - just a pointer.

Note: The "ReadOnly" setting is only *policed* by the PB Compilers for adherance. Once you get the read-only pointer & "Cast" it, you can do anything with the passed object's storage. ;-)
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.