Sequence passing overhead

Discussion of Common Lisp
Post Reply
Pixel_Outlaw
Posts: 43
Joined: Mon Aug 26, 2013 9:24 pm

Sequence passing overhead

Post by Pixel_Outlaw » Wed Nov 13, 2013 12:31 pm

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?

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Sequence passing overhead

Post by edgar-rft » Wed Nov 13, 2013 7:53 pm

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

Pixel_Outlaw
Posts: 43
Joined: Mon Aug 26, 2013 9:24 pm

Re: Sequence passing overhead

Post by Pixel_Outlaw » Wed Nov 13, 2013 10:50 pm

Awesome response, thank you!

Post Reply