car/cdr chain optimization
Stavros Macrakis <[email protected]> Mon, 3 Feb 2025 17:37:50 -0500
| Newsgroups | gmane.lisp.steel-bank.general |
|---|---|
| Message-ID | <CACLVabWpf1EhX0vB_WwNOMJvRDJT+TD7OMFNEtD5rqyXVCMNPw@mail.gmail.com> |
*(eq (cadr x) (cddr x)) *calculates* (cdr x)* twice. With *(safety 0)*,
that means an extra memory reference; with higher safety, that means an
extra type check as well. I haven't found a combination of
*optimization* settings which
just copies the register value instead of recalculating. Creating an
explicit temp variable makes the code less clear for not much gain.(*)
Is there some way to have SBCL cache *(cdr x)*? Clearly if *(cdr x) *is
modified between the first and the second, there's a problem. That could be
inline code; it could be code in some function called between the two
calls; it could be asynchronous or parallel code. But what declare that
that's not going to happen? With *(safety 3)*, it could *check* that that
didn't happen; with *(safety 2)*, it could believe the declaration. (Maybe
I missed some option?)
The Complr compiler (1960s-1970s) on the PDP-10 was very good at this. But
L1 cache, etc. make it a less valuable optimization. Is it ever worth doing?
-s
(*) Except for some utility routines in, e.g., Maxima, where
micro-optimizations like that can result in a measurable difference in the
speed of the system as a whole.
_______________________________________________
Sbcl-help mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sbcl-help