>From c2ae1a5e19926d26e06d2c6b1c188103c53677b4 Mon Sep 17 00:00:00 2001
From: Ryan Davis <ryan@acceleration.net>
Date: Wed, 21 Dec 2011 13:20:47 -0500
Subject: [PATCH] lazy-evaluate the default value in ENSURE-GETHASH

Converts ENSURE-GETHASH to a macro that only
evaluates the default if we get to the setf branch. Has
a check to create and call a lambda if the default value
looks complicated (using CONSTANTP)
---
 hash-tables.lisp |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/hash-tables.lisp b/hash-tables.lisp
index a574737..0c5506f 100644
--- a/hash-tables.lisp
+++ b/hash-tables.lisp
@@ -89,11 +89,14 @@ PLIST. Hash table is initialized using the HASH-TABLE-INITARGS."
       (setf (gethash (car tail) table) (cadr tail)))
     table))
 
-(defun ensure-gethash (key hash-table &optional default)
+(defmacro ensure-gethash (key hash-table &optional default)
   "Like GETHASH, but if KEY is not found in the HASH-TABLE saves the DEFAULT
 under key before returning it. Secondary return value is true if key was
 already in the table."
-  (multiple-value-bind (value ok) (gethash key hash-table)
+  `(multiple-value-bind (value ok) (gethash ,key ,hash-table)
     (if ok
         (values value ok)
-        (values (setf (gethash key hash-table) default) nil))))
+        (values (setf (gethash ,key ,hash-table)
+                      ,(if (constantp default) default
+                           `(funcall #'(lambda () ,default))))
+                nil))))
-- 
1.7.5.4

