2 ms·
Here is a Lisp compiler where (eq "foo" "foo") turns out false, in spite of there being deduplication: 1> (compile-toplevel '(list "foo" "foo")) #<sys:vm-d
by kazinator 4y ago
Here is a Lisp compiler where (eq "foo" "foo") turns out false, in spite of there being deduplication:
1> (compile-toplevel '(list "foo" "foo"))
#<sys:vm-desc: 923d0b0>
2> [*1]
("foo" "foo")
3> (eq (car *2) (cadr *2))
t
4> (compile-toplevel (eq "foo" "foo"))
#<sys:vm-desc: 9270bc0>
5> [*4]
nil
This is because when (eq "foo" "foo") is read, the objects are not the same.
The compiler does constant folding on that expression before merging the literals.
The expression folds down to nil:
6> (disassemble *4)
data:
syms:
code:
0: 10000000 end nil
instruction count:
1
#<sys:vm-desc: 9270bc0>
and that's that; there are no literals going into the compiled image, and so
no deduplication of anything is possible.
- kazinator 4y agoSupplement: paradoxically, by reducing the optimization level, we can get the string literals to be preserved in that eq expression, and get deduplicated: 1> (let ((*opt-level* 0)) (compile-toplevel '(eq "foo" "foo"))) #<sys:vm-desc: 9e48fd0> 2> [*1] t 3> (disassemble *1) data: 0: "foo" syms: 0: eq code: 0: 20020002 gcall t2 0 d0 d0 1: 04000000 2: 00000400 3: 10000002 end t2 instruction count: 2 #<sys:vm-desc: 9e48fd0> Now there is a call to eq in the compiled code (index 0 in the function symbol table). The string literal is deduplicated and lives in the data register d0, which appears as both arguments to the function. Register t2 receives the result of the call, and the end instruction indicates that register as the result.
- kaba0 4y agoI don’t know CL, is there a separate way to check for object equality vs identity equality (as in java’s == vs equals)?
- tmtvl 4y agoThere is equalp (http://www.lispworks.com/documentation/HyperSpec/Body/f_equalp.htm http://www.lispworks.com/documentation/HyperSpec/Body/f_equa...). It's like Scheme's equal?.