Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between array/vector types in Common Lisp?

What is the difference between the types vector, simple-vector, array and simple-array? I know that vectors are a subtype of arrays and cannot be multidimensional. The "simple-" versions seem to be types, and the rest classes. So the types might be more complex, to specify the type.

Which type-specifiers should I use? What is the best way to specify a vector?

like image 506
cl-porky11 Avatar asked Dec 14 '22 16:12

cl-porky11


2 Answers

A vector is an one-dimensional array.

They are simple if they are not displaced, have no fill pointer, and are not expressly adjustable. A simple-vector also needs to be able to hold elements of any type.

Which type-specifiers should I use?

I guess that depends on what you want to do.

like image 187
Rainer Joswig Avatar answered Jan 02 '23 08:01

Rainer Joswig


Rainer answered the positive part of your question.

Here is the answer to the normative part:

Which type-specifiers should I use?

You should not use any, unless you know very well what you are doing.

Type annotations in Common Lisp are optional; they are intended for the experts for the performance tuning. It is highly unlikely that you will ever gain any meaningful benefit from type declarations and you are highly likely to be hurt by them. E.g., if your type annotation is wrong you might get a segfault instead of an error message (when combined with unsafe code).

Additionally, the effect may be the opposite of what one might expect. E.g., using specialized arrays (e.g., (array double-float)) to reduce memory footprint may result in an increased garbage collection because every array access may have to box a new double-float.

Essentially, type annotations are usually micro optimizations which should be accompanied by careful profiling.

like image 44
sds Avatar answered Jan 02 '23 06:01

sds