random-state.net

Nikodemus Siivola

<< next | top | previous >>

Inline Caches with LOAD-TIME-VALUE, part 2 #
random, February 21st 2011

So why would you want to use LOAD-TIME-VALUE to implement a cache instead of doing something like this:

(let (cache)
  (defun parse-timestamp (string)
    (let ((cached cache))
      (if (equal string (car cached))
          (cdr cached)
          (let ((stamp (%parse-timestamp string)))
            (setf cache (cons string stamp))
            stamp)))))

...or this?

(defparameter *cache* nil)

(defun parse-timestamp (string)
  (let ((cached *cache*))
    (if (equal (car cached) string)
        (cdr cached)
        (let ((stamp (%parse-timestamp string)))
          (setf *cache* (cons string stamp))
          stamp))))

It's the reason why I'm talking about inline caches. While using toplevel closure or a global variable to implement the cache works fine, consider inlining (and compiler-macros.)

Simply by proclaiming the LOAD-TIME-VALUE-using function INLINE, you will be giving each call site their own cache