From: Nikodemus S. <nik...@ra...> - 2007-07-03 17:27:08
|
Nikodemus Siivola wrote: > Here's a sleazy little trick that speeds things up quite > a bit... > > (defun slot-value (object slot-name) > (let ((reader (get slot-name 'slot-reader-function))) > (unless (functionp reader) > (setf reader (compile nil `(lambda (x) (slot-value x ',slot-name))) > (get slot-name 'slot-reader-function) reader)) > (funcall (the function reader) object))) > > Using the symbol plist is sub-optimal but didn't require any extra > coding -- having a private per-symbol cache would be nicer, but > has a global cost. > > This hack brought to you by the symbol-plist libration front, Just to clarify. The points were: 1. Cacheing the reader function pays off if the cache is fast enough. 2. Hanging stuff directly on the symbols you're going to use for the lookup is fast and good. If I write it like this instead, using the infodb as the cache, (defun slot-value (object slot-name) (let* ((reader-name (slot-reader-name slot-name)) (fdefn (or (info :function :definition reader-name) (progn (ensure-accessor 'reader reader-name slot-name) (info :function :definition reader-name)) (bug "Oops"))) (reader (fdefn-fun fdefn))) (funcall (the function reader) object))) the result is slower then the current implementation. To take advantage of the infodb I need to still use the plist: (defun slot-value (object slot-name) (let ((reader (get slot-name 'slot-reader-function))) (unless (functionp reader) (setf reader (progn (ensure-accessor 'reader reader-name slot-name) (fdefinition reader-name)) (get slot-name 'slot-reader-function) reader)) (funcall (the function reader) object))) The reason I didn't go into this detail in the first place was to avoid cluttering the idea of cacheing the specialized reader in the slot-name symbol. The timings on my microbenchmark are: 5.4 seconds for HEAD 1.4 seconds for version 1 (GET + COMPILE) 14.5 seconds for version 2 (INFODB) 1.3 seconds for version 3 (GET + ENSURE-ACCESSOR + FDEFINITION) A meta-conclusion is that we _really_ want a faster infodb. Cheers, -- Nikodemus |