Are function parameters copy on write?

28 views
Skip to first unread message

David Storrs

unread,
Jun 6, 2019, 11:59:59 AM6/6/19
to Racket Users
My understanding is that Racket is call by value, not call by reference.  My application will often be passing around large-ish byte strings; will they be copied every time I pass them, or will the interpreter use copy-on-write?

Jon Zeppieri

unread,
Jun 6, 2019, 12:13:47 PM6/6/19
to David Storrs, Racket Users
On Thu, Jun 6, 2019 at 12:00 PM David Storrs <david....@gmail.com> wrote:
My understanding is that Racket is call by value, not call by reference.  My application will often be passing around large-ish byte strings; will they be copied every time I pass them, or will the interpreter use copy-on-write?


"call-by-value" is used in two very different ways. Racket is call-by-value in the sense that all arguments to functions need to be evaluated before the function body is evaluated. This kind of "call-by-value" is distinguished from other evaluation strategies, like call-by-name or call-by-need.

The other sense of the term -- and the one you're asking about -- has to do with whether arguments are copied from caller to callee. I find the terms "call-by-value" and "call-by-reference" in this context to be pretty confusing. (If the function argument in question is a mutable byte-string, why wouldn't passing it "by-value" imply that you pass the very same mutable byte-string, rather than making a copy of it?) At any rate, Racket does not copy arguments on function calls (except insofar as the copy is indistinguishable from the original -- as with a fixnum, for example).


Jay McCarthy

unread,
Jun 6, 2019, 12:14:42 PM6/6/19
to David Storrs, Racket Users
Your code is passing bytes by value, but bytes are themselves
pointers, so you are passing copies of the pointer, not copies of the
bytes. When you modify it, with `bytes-set!` you are modifying the
underlying structure. When you copy it with `subbytes` or
`bytes-copy`, you are making a new object with a new pointer.

--
Jay McCarthy
Associate Professor @ CS @ UMass Lowell
http://jeapostrophe.github.io
Vincit qui se vincit.

On Thu, Jun 6, 2019 at 12:00 PM David Storrs <david....@gmail.com> wrote:
>
> My understanding is that Racket is call by value, not call by reference. My application will often be passing around large-ish byte strings; will they be copied every time I pass them, or will the interpreter use copy-on-write?
>
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAE8gKod0Z%2Bv-3Oew4mPhsT4mwzOLdhc7Q-nF4xf_yH3qAgq_Hg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

David Storrs

unread,
Jun 6, 2019, 12:16:58 PM6/6/19
to Racket Users
On Thu, Jun 6, 2019 at 12:14 PM Jay McCarthy <jay.mc...@gmail.com> wrote:
Your code is passing bytes by value, but bytes are themselves
pointers, so you are passing copies of the pointer, not copies of the
bytes. When you modify it, with `bytes-set!` you are modifying the
underlying structure. When you copy it with `subbytes` or
`bytes-copy`, you are making a new object with a new pointer.

Sweet, that's what I needed to know.  Thank you.

What about other things, like arbitrary structs, (im)mutable strings, hashes, etc? 

Jay McCarthy

unread,
Jun 6, 2019, 12:25:06 PM6/6/19
to David Storrs, Racket Users
On Thu, Jun 6, 2019 at 12:16 PM David Storrs <david....@gmail.com> wrote:


On Thu, Jun 6, 2019 at 12:14 PM Jay McCarthy <jay.mc...@gmail.com> wrote:
Your code is passing bytes by value, but bytes are themselves
pointers, so you are passing copies of the pointer, not copies of the
bytes. When you modify it, with `bytes-set!` you are modifying the
underlying structure. When you copy it with `subbytes` or
`bytes-copy`, you are making a new object with a new pointer.

Sweet, that's what I needed to know.  Thank you.

What about other things, like arbitrary structs, (im)mutable strings, hashes, etc? 

Everything is like this except word sized objects like numbers but those are not mutable so you’d never know the difference (performance would be same too because the pointer would be word sized anyways.)




--
Jay McCarthy
Associate Professor @ CS @ UMass Lowell
http://jeapostrophe.github.io
Vincit qui se vincit.

On Thu, Jun 6, 2019 at 12:00 PM David Storrs <david....@gmail.com> wrote:
>
> My understanding is that Racket is call by value, not call by reference.  My application will often be passing around large-ish byte strings; will they be copied every time I pass them, or will the interpreter use copy-on-write?
>
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAE8gKod0Z%2Bv-3Oew4mPhsT4mwzOLdhc7Q-nF4xf_yH3qAgq_Hg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

David Storrs

unread,
Jun 6, 2019, 12:27:32 PM6/6/19
to Jay McCarthy, Racket Users
Cool.  Thanks, Jay.
Reply all
Reply to author
Forward
0 new messages