optimized preference lookup (one hash lookup in all cases except for the first.)

svn: r14727

original commit: 7d93d3b539342184886f7f0433fc8ae1f71cb3a9
This commit is contained in:
Eli Barzilay 2009-05-06 04:07:23 +00:00
parent f5f5cf90aa
commit bc668b4652

View File

@ -80,23 +80,25 @@ the state transitions / contracts are:
;; return the current value of the preference `p' ;; return the current value of the preference `p'
;; exported ;; exported
(define (preferences:get p) (define (preferences:get p)
(define v (hash-ref preferences p none))
(cond (cond
;; if this is found, we can just return it immediately
[(not (eq? v none))
v]
;; first time reading this, check the file & unmarshall value, if
;; it's not there, use the default
[(pref-default-set? p) [(pref-default-set? p)
(let* (;; try to read the preferece from the preferences file
(unless (hash-has-key? preferences p) [v ((preferences:low-level-get-preference)
;; first time reading this, check the file, unmarshall if required (add-pref-prefix p) (λ () none))]
(let/ec k [v (if (eq? v none)
;; if there is no preference saved, we just don't do anything. ;; no value read, take the default value
;; the code below notices this case. (default-value (hash-ref defaults p))
(let ([marshalled ((preferences:low-level-get-preference) ;; found a saved value, unmarshall it
(add-pref-prefix p) (λ () (k (void))))]) (unmarshall-pref p marshalled))])
(hash-set! preferences p (unmarshall-pref p marshalled))))) ;; set the value for future reference and return it
(hash-set! preferences p v)
;; if it still isn't set, take the default value v)]
(unless (hash-has-key? preferences p)
(hash-set! preferences p (default-value (hash-ref defaults p))))
(hash-ref preferences p)]
[(not (pref-default-set? p)) [(not (pref-default-set? p))
(raise-unknown-preference-error (raise-unknown-preference-error
'preferences:get 'preferences:get