25 ms·
CL implementations offer the indexed access, but include the data in the struct only for few data types. Any good Common Lisp implementation will use fixed ind
by lispm 9y ago
CL implementations offer the indexed access, but include the data in the struct only for few data types.
Any good Common Lisp implementation will use fixed indexes. The Common Lisp compiler can inline the accessor and will be using fixed indexes.
What Lisp compiles usually not do is inline the data in the struct. Thus the struct slots will contain a pointer to the slot value.
Exceptions: small integers, characters, ... Those will fit into the slot and will be stored directly in the struct.
That's the point of structures in Common Lisp.
Below is an example in Clozure Common Lisp on a ARM processor. As you can see, the compiler can inline the accessor and uses fixed indexes to access the slots...
CL-USER> (defstruct person name age)
PERSON
CL-USER> (defun test (person)
(declare (person person)
(inline person-name person-age)
(optimize (speed 3) (safety 0) (debug 0)))
(person-name person))
TEST
CL-USER> (disassemble #'test)
(mov imm0 (:$ 19))
(stmdb (:! sp) (imm0 vsp fn lr))
(mov fn temp2)
(vpush1 arg_z) ;[12]
(ldr arg_z (:@ arg_z (:$ 2))) ; name at pos 2
(ldmia (:! sp) (imm0 vsp fn pc))
NIL
CL-USER> (defun test (person)
(declare (person person)
(inline person-name person-age)
(optimize (speed 3) (safety 0) (debug 0)))
(person-age person))
TEST
CL-USER> (disassemble #'test)
(mov imm0 (:$ 19))
(stmdb (:! sp) (imm0 vsp fn lr))
(mov fn temp2)
(vpush1 arg_z) ;[12]
(ldr arg_z (:@ arg_z (:$ 6))) ; age at pos 6
(ldmia (:! sp) (imm0 vsp fn pc))
NIL