Consing is another name for allocation of storage, as done by the
cons function (hence its name.)
cons is by no means the
only function which conses—so does
make-array and many
other functions. Arithmetic and function call can also have hidden
consing overheads. Consing hurts performance in the following ways:
Consing is not undiluted evil, since programs do things other than consing, and appropriate consing can speed up the real work. It would certainly save time to allocate a vector of intermediate results that are reused hundreds of times. Also, if it is necessary to copy a large data structure many times, it may be more efficient to update the data structure non-destructively; this somewhat increases update overhead, but makes copying trivial.
Note that the remarks in section efficiency-overview about the importance of separating tuning from coding also apply to consing overhead. The majority of consing will be done by a small portion of the program. The consing hot spots are even less predictable than the CPU hot spots, so don’t waste time and create bugs by doing unnecessary consing optimization. During initial coding, avoid unnecessary side-effects and cons where it is convenient. If profiling reveals a consing problem, then go back and fix the hot spots.
See non-descriptor for a discussion of how to avoid number consing in Python.