Page 1 of 1

Sequence passing overhead

PostPosted: Wed Nov 13, 2013 12:31 pm
by Pixel_Outlaw
I'm working on a function that requires me to pass around an array of 100 or so structs. (simple Point structs with just an x and y slot)

My question is how does CL handle passing arrays by parameter?
I know some languages pass by value and others by pointers.
I really hope that when passing this huge array of structures I'm not passing by copy each function I put it through.

Is there much overhead when passing around large arrays of structures?

Re: Sequence passing overhead

PostPosted: Wed Nov 13, 2013 7:53 pm
by edgar-rft
Quick answer: Arrays are always passed by reference (pointer).

Common Lisp generally tries to pass parameters by reference, except things that are small enough to fit into a single CPU register (FIXNUMs etc). Lisp "tries" means that the details are implementation dependent (and hardware dependent too), but you can be sure that every existing Common Lisp implementation will try to produce as little memory overhead as possible, because otherwise the execution speed would become very slow.

There was a very similar discussion a few years ago under Passing values by reference with some more detailed explanations.

By using COPY-SEQ you can pass arrays by value. But if you COPY-SEQ an array containing other arrays as sub-arrays then you will get a freshly allocated new array containing references (pointers) to the sub-arrays of the original array. Common Lisp only allocates a new top-level sequence, containing references (pointers) to the elements of the original sequence.

There is a paper explaining when, how, and why Common Lisp uses values or references and how to compare them:

- edgar

Re: Sequence passing overhead

PostPosted: Wed Nov 13, 2013 10:50 pm
by Pixel_Outlaw
Awesome response, thank you!