memoization: Micro-optimize code produced by 'define-cache-procedure'.

* guix/memoization.scm (%nothing): Remove.
(define-cache-procedure): Make '%nothing' a local variable, with a
literal list.
master
Ludovic Courtès 2017-03-16 13:41:51 +01:00
parent 34d60c49cb
commit 146db52a18
No known key found for this signature in database
GPG Key ID: 090B11993D9AEBB5
1 changed files with 10 additions and 11 deletions

View File

@ -31,9 +31,6 @@
(define-syntax-rule (return/1 value) (define-syntax-rule (return/1 value)
value) value)
(define %nothing ;nothingness
(list 'this 'is 'nothing))
(define-syntax define-cache-procedure (define-syntax define-cache-procedure
(syntax-rules () (syntax-rules ()
"Define a procedure NAME that implements a cache using HASH-REF and "Define a procedure NAME that implements a cache using HASH-REF and
@ -41,7 +38,9 @@ HASH-SET!. Use CALL to invoke the thunk and RETURN to return its value; CALL
and RETURN are used to distinguish between multiple-value and single-value and RETURN are used to distinguish between multiple-value and single-value
returns." returns."
((_ name hash-ref hash-set! call return) ((_ name hash-ref hash-set! call return)
(define (name cache key thunk) (define name
(let ((%nothing '(this is nothing)))
(lambda (cache key thunk)
"Cache the result of THUNK under KEY in CACHE, or return the "Cache the result of THUNK under KEY in CACHE, or return the
already-cached result." already-cached result."
(let ((results (hash-ref cache key %nothing))) (let ((results (hash-ref cache key %nothing)))
@ -49,7 +48,7 @@ already-cached result."
(let ((results (call thunk))) (let ((results (call thunk)))
(hash-set! cache key results) (hash-set! cache key results)
(return results)) (return results))
(return results))))) (return results)))))))
((_ name hash-ref hash-set!) ((_ name hash-ref hash-set!)
(define-cache-procedure name hash-ref hash-set! (define-cache-procedure name hash-ref hash-set!
call/mv return/mv)))) call/mv return/mv))))