ByRef vs Regular Function Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

ByRef vs Regular Function

Post by AHKStudent » 19 Aug 2022, 19:32

I am trying to understand what this means (https://www.autohotkey.com/docs/Functions.htm#param)

"When passing large strings to a function, ByRef enhances performance and conserves memory by avoiding the need to make a copy of the string."

My extremely limited understanding of memory had me thinking that if you don't use ByRef when the data is passed to the function, a copy is made and that copy is stored in a new memory address and when ByRef is used it uses the memory address of the data that was passed.

I did the following test

Code: Select all

x := ["apples","oranges"]
MsgBox, % &x ; show memory location of x
addo(x)
ExitApp
addo(byref h){
	MsgBox, % &h ; show memory location of h
}
; results, regardless if ByRef is used, the memory addresses of x and h are the same. When tested with a simple variable (not an array) the addresses where different when ByRef was omitted 
Results of the test: regardless if ByRef is used, the memory addresses of x and h are the same. When tested with a simple variable (not an array) the addresses where different when ByRef was omitted

What am I missing?

Thank You

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: ByRef vs Regular Function  Topic is solved

Post by teadrinker » 19 Aug 2022, 19:51

x is not a string. Objects are always passed by reference.

iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: ByRef vs Regular Function

Post by iseahound » 19 Aug 2022, 19:59

First off some basics: This is the same for every programming language.
  • Objects are always passed by reference.
  • When not specified, a good assumption is that strings and numbers are passed by value.
Bullet point #1 explains the result you see: Objects are never duplicated. Even the simple assignment of one object to another name also keeps the same address.

Now to explain why ByRef will increase your speed: Strings are passed by value. Therefore, if your string is a hundred megabytes long, it would be faster to not make a copy of the string each time it is passed to a function.

Finally, there is string pooling / string interning. String pooling is where if you have:

Code: Select all

a := "Wolves exit quickly as fanged zoo chimp jabbers."
b := "Wolves exit quickly as fanged zoo chimp jabbers."
MsgBox % &a "`n" &b
then the address of a would be equal to the address of b. This does not happen in AutoHotkey.
String interning is often used interchangeably with string pooling.

Some languages will cache simple values. This is different from string pooling as that is an optimization based on your code, whereas caching affects everyone's code. Python caches small numbers from -5 to 256, meaning every instance of the number 1 is an address to one immutable copy of 1. https://realpython.com/lessons/small-integer-caching/

† If your string or integer is cached / pooled, the created copy will just be an address alias to an immutable cached copy somewhere. So sometimes it ends up being passed ByRef (this does not apply or happen in AutoHotkey)

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: ByRef vs Regular Function

Post by teadrinker » 19 Aug 2022, 20:06

iseahound wrote: When not specified, a good assumption is that strings and numbers are passed by value.
At least in Java strings are objects and passed by reference too.

AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: ByRef vs Regular Function

Post by AHKStudent » 19 Aug 2022, 20:09

iseahound wrote:
19 Aug 2022, 19:59
First off some basics: This is the same for every programming language.
  • Objects are always passed by reference.
This all started when I watched a video about pointers and references in c++, the guy spoke about how it's helpful when dealing with large structures and I assumed he meant an array.

Thank you

Post Reply

Return to “Ask for Help (v1)”