tags:

views:

42

answers:

1

I have a question about elisp vectors.

Are integers and characters actually stored (efficiently) inside the vector or via a reference (pointer)?

Vectors can hold arbitrary objects; For example:

(setq v (make-vector 10 nil)) (aset v 0 "Hello World")

In this case it's obvious that the vector cell 0 keeps a reference (pointer) to the string "Hello World".

But what about integers/characters?

(aset v 1 ?X)

Is the character X actually stored inside the vector's cell 1?

+4  A: 

At the C level, an emacs lisp object is a word-sized object that contain tag bits and value bits. The tag bits determine how the value bits are to be interpreted: as a pointer (and to what), or as a direct constant such as an integer or character. This is a fairly common implementation technique for high-level languages.

A (plain) vector is an array of such objects, so there is no indirection in a vector of integers or characters. Strings and bitvectors provide a more compact representation at the expense of only being able to store characters or booleans respectively.

The details of the representation (such as how many tag bits there are, how big integers can go, and so on) depend on the version of Emacs and on compile-time settings. The source file where the magic happens is src/lisp.h.

Gilles