<div dir="ltr">No one should rely on eq? of lists..... especially not in code that didn't exist in the pre-immutable cons days.</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Feb 11, 2013 at 12:17 PM, Neil Toronto <span dir="ltr"><<a href="mailto:neil.toronto@gmail.com" target="_blank">neil.toronto@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The performance of `sequence->list' came up on this thread:<br>
<br>
<a href="http://thread.gmane.org/gmane.comp.lang.racket.user/16384" target="_blank">http://thread.gmane.org/gmane.<u></u>comp.lang.racket.user/16384</a><br>
<br>
`sequence->list' uses the sequence API to make a copy of its input. This can dominate the running time of functions that look like this:<br>
<br>
(define (f xs)<br>
(let ([xs (sequence->list xs)])<br>
... simple O(n) computation using xs ...))<br>
<br>
Also, it seems wasteful to make a copy if `xs' is already a list.<br>
<br>
I'd like to change the definition of `sequence->list' from this:<br>
<br>
(define (sequence->list s)<br>
(for/list ([v s]) v))<br>
<br>
to this, to handle the two most common sequences specially:<br>
<br>
(define (my-sequence->list s)<br>
(cond [(list? s) s]<br>
[(vector? s) (vector->list s)]<br>
[else (for/list ([v s]) v)]))<br>
<br>
For vectors, I measure a 3x speedup. For lists, it's of course O(1).<br>
<br>
It's a semantic change, but I can't imagine anyone relying on this fact:<br>
<br>
(not (eq? xs (sequence->list xs)))<br>
<br>
If someone does, [(list? s) (map values s)] would preserve that fact, and is 3x faster in my measurements.<br>
<br>
Thoughts?<br>
<br>
Neil ⊥<br>
_________________________<br>
Racket Developers list:<br>
<a href="http://lists.racket-lang.org/dev" target="_blank">http://lists.racket-lang.org/<u></u>dev</a><br>
</blockquote></div><br></div>