Skip to content

Commit e7be251

Browse files
committed
use closures for tiny speedup
1 parent f59df64 commit e7be251

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

typed-racket-lib/typed-racket/static-contracts/optimize.rkt

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110

111111

112112
;; Reduce a static contract assuming that we trusted the current side
113-
(define (trusted-side-reduce sc flat-sc?)
113+
(define ((make-trusted-side-reduce flat-sc?) sc)
114114
(match sc
115115
[(->/sc: mand-args opt-args mand-kw-args opt-kw-args rest-arg (list (any/sc:) ...))
116116
(function/sc #t mand-args opt-args mand-kw-args opt-kw-args rest-arg #f)]
@@ -173,7 +173,7 @@
173173
;; update-side : sc? weak-side? -> weak-side?
174174
;; Change the current side to something safe & strong-as-possible
175175
;; for optimizing the sub-contracts of the given `sc`.
176-
(define (update-side sc side flat-sc?)
176+
(define ((make-update-side flat-sc?) sc side)
177177
(match sc
178178
[(or/sc: scs ...)
179179
#:when (not (andmap flat-sc? scs))
@@ -289,24 +289,27 @@
289289
(sc-map sc trim)]))
290290
(trim sc 'covariant))
291291

292+
(define (make-sc->kind recursive-kinds)
293+
(if recursive-kinds
294+
(λ (sc)
295+
(let loop ([sc sc])
296+
(match sc
297+
[(recursive-sc _ _ body)
298+
(loop body)]
299+
[(or (recursive-sc-use id)
300+
(name/sc: id))
301+
(hash-ref recursive-kinds id #f)]
302+
[_
303+
(sc-terminal-kind sc)])))
304+
sc-terminal-kind))
292305

293306
;; If we trust a specific side then we drop all contracts protecting that side.
294307
(define (optimize sc #:trusted-positive [trusted-positive #f] #:trusted-negative [trusted-negative #f] #:recursive-kinds [recursive-kinds #f])
295-
(define sc->kind
296-
(if (not recursive-kinds)
297-
sc-terminal-kind
298-
(λ (sc)
299-
(let loop ([sc sc])
300-
(match sc
301-
[(recursive-sc _ _ body)
302-
(loop body)]
303-
[(or (recursive-sc-use id)
304-
(name/sc: id))
305-
(hash-ref recursive-kinds id #f)]
306-
[_
307-
(sc-terminal-kind sc)])))))
308-
(define (flat-sc? sc)
309-
(eq? 'flat (sc->kind sc)))
308+
(define flat-sc?
309+
(let ([sc->kind (make-sc->kind recursive-kinds)])
310+
(λ (sc) (eq? 'flat (sc->kind sc)))))
311+
(define trusted-side-reduce (make-trusted-side-reduce flat-sc?))
312+
(define update-side (make-update-side flat-sc?))
310313

311314
;; single-step: reduce and trusted-side-reduce if appropriate
312315
(define (single-step sc maybe-weak-side)
@@ -320,14 +323,14 @@
320323

321324
(reduce
322325
(if trusted
323-
(trusted-side-reduce sc flat-sc?)
326+
(trusted-side-reduce sc)
324327
sc)))
325328

326329
;; full-pass: single-step at every static contract subpart
327330
(define (full-pass sc)
328331
(define ((recur side) sc variance)
329332
(define curr-side (combine-variance side variance))
330-
(define next-side (update-side sc curr-side flat-sc?))
333+
(define next-side (update-side sc curr-side))
331334
(single-step (sc-map sc (recur next-side)) curr-side))
332335
((recur 'positive) sc 'covariant))
333336

0 commit comments

Comments
 (0)