4

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?

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Why exactly simple-vector needs to be able to hold elements of any type? Suppose I want to have a one-dimensional, constant size container of elements of same predetermined type, should I stick to simple-array then?
5

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.

1 Comment

What is an expert, why are type annotations meant for them, and how does one become an expert so you can use them? An array of single- or double-floats ends up being a not-really-expert and common use of typed arrays, when supported by the implementation, that really makes a difference. Most implementations actually make a flat array instead of one with boxed values. If type declarations can hurt, at least explain why (e.g. what happens when we also (declare (optimize (safety 0)))), don't just state so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.