2 ms·
I don't doubt that CL could do it -- if there's any dynamic language that can support efficient C-style attribute lookup for structs, it's Lisp. (Although I ho
by chubot 9y ago
I don't doubt that CL could do it -- if there's any dynamic language that can support efficient C-style attribute lookup for structs, it's Lisp.
(Although I honestly doubt that idiomatic Lisp does this. Idiomatic Lisp might be even slower than idiomatic Python, because I've seen people use alists as structs rather than dictionaries.)
Either way, your example doesn't really address what I'm talking about. Python has a struct module too, and it has namedtuple, etc. My claim is that Python doesn't have C-style structs, where attribute names are resolved to indexes AT COMPILE TIME, because it breaks the model of the bytecode compiler and dynamic module system.
Lisp is flexible about compile time vs. runtime, so I won't be surprised if it can do that. But again there's no evidence in your example that it does that.
- lispm 9y agoCL 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