From a193cd9efbef0e59f963483471f7407ea38934b9 Mon Sep 17 00:00:00 2001 From: "J. Ian Johnson" Date: Thu, 29 Nov 2012 10:32:07 -0500 Subject: [PATCH] Peephole optimization for iterating over an empty set (no allocation). --- collects/racket/set.rkt | 2 ++ collects/tests/racket/set.rktl | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/collects/racket/set.rkt b/collects/racket/set.rkt index f79c39c8fe..b0d7cc6a5f 100644 --- a/collects/racket/set.rkt +++ b/collects/racket/set.rkt @@ -309,6 +309,8 @@ (lambda () #'in-set) (lambda (stx) (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) (_ st)] #`[(id) diff --git a/collects/tests/racket/set.rktl b/collects/tests/racket/set.rktl index ad57c67525..4aa4bdd70c 100644 --- a/collects/tests/racket/set.rktl +++ b/collects/tests/racket/set.rktl @@ -105,7 +105,11 @@ (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 (let ([seq (in-set s)]) (for/list ([v seq]) v)) <) + ;; Optimized (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))