Peephole optimization for iterating over an empty set (no allocation).

This commit is contained in:
J. Ian Johnson 2012-11-29 10:32:07 -05:00 committed by Matthew Flatt
parent 4721a79c8f
commit a193cd9efb
2 changed files with 6 additions and 0 deletions

View File

@ -309,6 +309,8 @@
(lambda () #'in-set) (lambda () #'in-set)
(lambda (stx) (lambda (stx)
(syntax-case stx (set) (syntax-case stx (set)
;; Set construction is costly, so specialize empty/singleton cases
[[(id) (_ (set))] #'[(id) (:do-in ([(id) #f]) #t () #f () #f #f ())]]
[[(id) (_ (set expr))] #'[(id) (:do-in ([(id) expr]) #t () #t () #t #f ())]] [[(id) (_ (set expr))] #'[(id) (:do-in ([(id) expr]) #t () #t () #t #f ())]]
[[(id) (_ st)] [[(id) (_ st)]
#`[(id) #`[(id)

View File

@ -105,7 +105,11 @@
(test '(1 2 3) sort (for/list ([v s]) v) <) (test '(1 2 3) sort (for/list ([v s]) v) <)
(test '(1 2 3) sort (for/list ([v (in-set s)]) v) <) (test '(1 2 3) sort (for/list ([v (in-set s)]) v) <)
(test '(1 2 3) sort (let ([seq (in-set s)]) (for/list ([v seq]) v)) <) (test '(1 2 3) sort (let ([seq (in-set s)]) (for/list ([v seq]) v)) <)
;; Optimized
(test '(1) sort (for/list ([v (in-set (set 1))]) v) <) (test '(1) sort (for/list ([v (in-set (set 1))]) v) <)
(test #t values (let ([noset #t])
(for ([v (in-set (set))]) (set! noset #f))
noset))
(void)) (void))