What is the potential impact on compiler optimization to defining a Common Lisp structure (or class) like (defstruct person (age nil :type (or null integer)))
vs (defstruct person (age -1 :type integer))
? I often find it convenient, using the first form, to have a null value signifying a special condition; for example, if a person's age is not known. But in the second form, you could also use an ad-hoc designated integer like -1 to indicate this condition. Is there an advantage/disadvantage to one representation over the other? (ps: In this case the difference is likely negligible, but seems like it might not be for nested structures involving complex objects like hash-tables, etc.)

- 1,847
- 11
- 21
-
1Impact on compiler optimization? Who knows? This depends on the runtime, the compiler, compiler settings, your program, ... If you would use CLOS, a slot can be unbound, which could indicate that the age is not known. – Rainer Joswig Apr 13 '17 at 17:59
1 Answers
I think you are asking the wrong question.
"... a computer language is not just a way of getting a computer to perform operations, but rather ... it is a novel formal medium for expressing ideas about methodology"
Abelson/Sussman "Structure and Interpretation of Computer Programs".
You are writing code for people to read, not just for computer to execute. The primary driver for your design should be code readability rather than performance (especially when, as is in your case, the performance impact is most likely to be negligible):
Specifically in your case, if you have only one special value, and that special value means "missing", you might want to use defclass
where you can have unbound slots instead of nil
slot values in defstruct
.
PS. Don't get me wrong - performance DOES matter. However, it matters in scalability, not micro-optimizations. IOW, the priorities should be
- Scalability (i.e., choosing a linearithmic instead of a quadratic algorithm)
- Readability/maintainability (code structure, symbol names, comments, and such)
- Micro-optimizations (e.g., type declarations in Lisp code)
-
It seems like there are a lot of informed opinions on optimization & representation here: your references and (https://groups.google.com/forum/#!topic/comp.lang.lisp/fpEC_WubwGE) (https://groups.google.com/forum/#!topic/comp.lang.lisp/ZOgTMwm3gD4)], for example. Lots of food for thought, thanks. – davypough Apr 14 '17 at 04:40