Skip to content

Commit d905189

Browse files
committed
seq: optimize append
There are two changes in this commit. The first change builds the list from a variadic append operation from right to left, which is the natural order for list construction. This speeds up the following program: ``` (time (void (append* (for/list ([i 30000]) (if b '() '(1)))))) ``` from 5 seconds to 1 second. The second change avoids making an assertion when doing unsafe/append on two symbolic unions. This is fine because all calls to unsafe/append is already guarded via type casting to make them symbolic unions of rosette-contract?, so there is no need to add another guard. Thanks to @camoy for raising up the issue about the unnecessary assertions.
1 parent edf682d commit d905189

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

rosette/base/adt/seq.rkt

+6-9
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,19 @@
6969
[((? racket-contract?) (union vs)) (unsafe-merge** vs (proc xs _))]
7070
[((union vs) (? racket-contract?)) (unsafe-merge** vs (proc _ ys))]
7171
[((union vs) (union ws))
72-
(apply unsafe-merge*
73-
(assert-some
74-
(for*/list ([v vs] [w ws] [g (in-value (&& (car v) (car w)))] #:when g)
75-
(cons g (proc (cdr v) (cdr w))))
76-
#:unless (* (length vs) (length ws))
77-
(arguments-error (quote proc) (format "expected ~a ~a" rosette-contract? rosette-contract?)
78-
"first argument" vs "second argument" ws)))]))]
72+
(apply unsafe-merge*
73+
(for*/list ([v vs] [w ws] [g (in-value (&& (car v) (car w)))] #:when g)
74+
(cons g (proc (cdr v) (cdr w)))))]))]
7975
(define #,(lift-id #'proc)
8076
(case-lambda
8177
[() (racket-constructor)]
8278
[(xs) (type-cast rosette-contract? xs (quote proc))]
8379
[(xs ys) (unsafe/append (type-cast rosette-contract? xs (quote proc))
8480
(type-cast rosette-contract? ys (quote proc)))]
8581
[xss (for/fold ([out (racket-constructor)])
86-
([xs (for/list ([ys xss]) (type-cast rosette-contract? ys (quote proc)))])
87-
(unsafe/append out xs))])))]))
82+
([xs (for/list ([ys (in-list (reverse xss))])
83+
(type-cast rosette-contract? ys (quote proc)))])
84+
(unsafe/append xs out))])))]))
8885

8986
(define-syntax (define/lift/split stx)
9087
(syntax-case stx ()

0 commit comments

Comments
 (0)