Added an optimization for vector-length of known-length vectors.

This commit is contained in:
Vincent St-Amour 2010-07-06 14:28:40 -04:00
parent eb7fc7a965
commit e52d63ee68
3 changed files with 16 additions and 0 deletions

View File

@ -0,0 +1,3 @@
(module known-vector-length typed/scheme #:optimize
(require racket/unsafe/ops)
(+ 2 (vector-length (ann (vector 1 2) (Vector Integer Integer)))))

View File

@ -0,0 +1,3 @@
(module known-vector-length typed/scheme #:optimize
(require racket/unsafe/ops)
(+ 2 (begin (vector 1 2) 2)))

View File

@ -231,6 +231,16 @@
(begin (log-optimization "unary pair" #'op)
#'(op.unsafe p.opt)))
;; vector-length of a known-length vector
(pattern (#%plain-app (~and op (~or (~literal vector-length)
(~literal unsafe-vector-length)
(~literal unsafe-vector*-length)))
v:vector-opt-expr)
#:with opt
(begin (log-optimization "known-length vector" #'op)
(match (type-of #'v)
[(tc-result1: (HeterogenousVector: es))
#`(begin v.opt #,(length es))]))) ; v may have side effects
;; we can optimize vector-length on all vectors.
;; since the program typechecked, we know the arg is a vector.
;; we can optimize no matter what.