Metafunctions now respect caching-enabled? and set-cache-size!.

svn: r17271
This commit is contained in:
Casey Klein 2009-12-11 20:54:32 +00:00
parent 0c03246daa
commit 4f140eed03
3 changed files with 24 additions and 24 deletions

View File

@ -1592,6 +1592,7 @@ before the pattern compiler is invoked.
compiled-pattern?)) compiled-pattern?))
(set-cache-size! (-> (and/c integer? positive?) void?)) (set-cache-size! (-> (and/c integer? positive?) void?))
(cache-size (and/c integer? positive?))
(make-bindings ((listof bind?) . -> . bindings?)) (make-bindings ((listof bind?) . -> . bindings?))
(bindings-table (bindings? . -> . (listof bind?))) (bindings-table (bindings? . -> . (listof bind?)))

View File

@ -1425,7 +1425,15 @@
(values (values
(wrap (wrap
(letrec ([cache (make-hash)] (letrec ([cache (make-hash)]
[cache-entries 0]
[not-in-cache (gensym)] [not-in-cache (gensym)]
[cache-result (λ (arg res case)
(when (caching-enabled?)
(when (>= cache-entries cache-size)
(set! cache (make-hash))
(set! cache-entries 0))
(hash-set! cache arg (cons res case))
(set! cache-entries (add1 cache-entries))))]
[log-coverage (λ (id) [log-coverage (λ (id)
(when id (when id
(for-each (for-each
@ -1452,7 +1460,7 @@
[(null? cases) [(null? cases)
(if relation? (if relation?
(begin (begin
(hash-set! cache exp (cons #f #f)) (cache-result exp #f #f)
#f) #f)
(redex-error name "no clauses matched for ~s" `(,name . ,exp)))] (redex-error name "no clauses matched for ~s" `(,name . ,exp)))]
[else [else
@ -1470,7 +1478,7 @@
(redex-error name "codomain test failed for ~s, call was ~s" ans `(,name ,@exp))) (redex-error name "codomain test failed for ~s, call was ~s" ans `(,name ,@exp)))
(cond (cond
[ans [ans
(hash-set! cache exp (cons #t id)) (cache-result exp #t id)
(log-coverage id) (log-coverage id)
#t] #t]
[else [else
@ -1499,7 +1507,7 @@
"codomain test failed for ~s, call was ~s" "codomain test failed for ~s, call was ~s"
ans ans
`(,name ,@exp))) `(,name ,@exp)))
(hash-set! cache exp (cons ans id)) (cache-result exp ans id)
(log-coverage id) (log-coverage id)
ans)]))])))]))] ans)]))])))]))]
[else [else

View File

@ -369,28 +369,19 @@ clause is followed by an ellipsis. Nested ellipses produce
nested lists. nested lists.
} }
@defproc[(set-cache-size! [size positive-integer?]) void?]{ @defparam[caching-enabled? on? boolean?]{
When this parameter is @scheme[#t] (the default), Redex caches the results of
pattern matching and metafunction evaluation. There is a separate cache for
each pattern and metafunction; when one fills (see @scheme[set-cache-size!]),
Redex evicts all of the entries in that cache.
Changes the cache size; the default size is @scheme[350]. Caching should be disabled when matching a pattern that depends on values
other than the in-scope pattern variables or evaluating a metafunction
The cache is per-pattern (ie, each pattern has a cache of size at most that reads or writes mutable external state.
350 (by default)) and is a simple table that maps expressions to how
they matched the pattern (ie, the bindings for the pattern
variables). When the cache gets full, it is thrown away and a new
cache is started.
} }
@defparam[caching-enabled? on? boolean?]{ @defproc[(set-cache-size! [size positive-integer?]) void?]{
This is a parameter that controls whether or not a cache Changes the size of the per-pattern and per-metafunction caches. The default size is @scheme[350].
is consulted (and updated) while matching and while evaluating
metafunctions.
If it is @scheme[#t], then side-conditions and the right-hand sides
of metafunctions are assumed to only depend on the values of the
pattern variables in scope (and thus not on any other external
state).
Defaults to @scheme[#t].
} }
@section{Terms} @section{Terms}
@ -921,7 +912,7 @@ or if the contract is violated.
Note that metafunctions are assumed to always return the same results Note that metafunctions are assumed to always return the same results
for the same inputs, and their results are cached, unless for the same inputs, and their results are cached, unless
@scheme[caching-enable?] is set to @scheme[#f]. Accordingly, if a @scheme[caching-enabled?] is set to @scheme[#f]. Accordingly, if a
metafunction is called with the same inputs twice, then its body is metafunction is called with the same inputs twice, then its body is
only evaluated a single time. only evaluated a single time.