Skip to content

Commit

Permalink
Do the caching with macros; this is Lisp, after all
Browse files Browse the repository at this point in the history
  • Loading branch information
espenhw committed Feb 27, 2009
1 parent 132825b commit 20130cd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
43 changes: 14 additions & 29 deletions src/main/lisp/malabar-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,11 @@ in the list")
(defun malabar-project-classpath (buffer)
(concat (malabar-project buffer) "." (malabar-classpath-of-buffer buffer)))

(defvar malabar--qualify-class-name-cache nil)

(defun malabar-qualify-class-name (unqualified &optional buffer)
(or (and malabar--qualify-class-name-cache
(gethash unqualified malabar--qualify-class-name-cache))
(let ((classes (malabar-groovy-eval-and-lispeval
(format "%s.getClasses('%s')"
(malabar-project-classpath (or buffer (current-buffer)))
unqualified))))
(when malabar--qualify-class-name-cache
(puthash unqualified classes malabar--qualify-class-name-cache))
classes)
(define-cached-function malabar-qualify-class-name (unqualified &optional buffer)
(or (malabar-groovy-eval-and-lispeval
(format "%s.getClasses('%s')"
(malabar-project-classpath (or buffer (current-buffer)))
unqualified))
(error "Class not found %s" unqualified)))

(defun malabar-classpath-of-buffer (&optional buffer)
Expand Down Expand Up @@ -491,18 +484,11 @@ in the list")
(list malabar-failed-test-re ;; RE
'malabar-find-test-class-from-error)) ;; FILE

(defvar malabar--class-info-cache nil)

(defun malabar-get-class-info (classname &optional buffer)
(or (and malabar--class-info-cache
(gethash classname malabar--class-info-cache))
(let ((info (malabar-groovy-eval-and-lispeval
(format "%s.getClassInfo('%s')"
(malabar-project-classpath (or buffer (current-buffer)))
classname))))
(when malabar--class-info-cache
(puthash classname info malabar--class-info-cache))
info)))
(define-cached-function malabar-get-class-info (classname &optional buffer)
(malabar-groovy-eval-and-lispeval
(format "%s.getClassInfo('%s')"
(malabar-project-classpath (or buffer (current-buffer)))
classname)))

(defsubst malabar--get-property (spec prop)
(getf (cdr spec) prop))
Expand Down Expand Up @@ -798,14 +784,13 @@ in the list")
(defun malabar--override-all (methods &optional suppress-annotation)
(let ((method-count (length methods))
(counter 0)
(malabar--qualify-class-name-cache (make-hash-table :test 'equal))
(malabar--class-info-cache (make-hash-table :test 'equal))
(overridable-methods (malabar-overridable-methods)))
(message nil)
(working-status-forms "Overriding methods...%s" nil
(dolist (method methods)
(working-status (/ (* (incf counter) 100) method-count) (malabar--get-name method))
(malabar--override-method method overridable-methods suppress-annotation t))
(with-caches
(dolist (method methods)
(working-status (/ (* (incf counter) 100) method-count) (malabar--get-name method))
(malabar--override-method method overridable-methods suppress-annotation t)))
(working-status t "done"))
(let ((class-tag (malabar-get-class-tag-at-point)))
(indent-region (semantic-tag-start class-tag) (semantic-tag-end class-tag)))))
Expand Down
37 changes: 37 additions & 0 deletions src/main/lisp/malabar-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,41 @@
(defun string-ends-with (string end)
(string= (substring string (- (length string) (length end))) end))

(defvar malabar--caches nil)

(def-edebug-spec with-caches t)
(defmacro with-caches (&rest forms)
"Executes FORMS with all defined caches bound to new
hash-tables with `equal' as test."
`(let ,(mapcar (lambda (cache-name)
(list cache-name '(make-hash-table :test 'equal)))
malabar--caches)
,@forms))

(def-edebug-spec define-cached-function defun)
(defmacro define-cached-function (name lambda-list &optional doc &rest body)
"Defines NAME as a function which, when invoked within the
scope of `with-caches', memoizes its return in a unique cache
keyed by the function's first parameter."
(declare (indent defun)
(doc-string 3))
(let ((gensym (gensym))
(cache-name (gensym))
(key (first lambda-list)))
`(progn
(add-to-list 'malabar--caches ',cache-name)
(defun ,name ,lambda-list
,@(when (stringp doc)
(list doc))
(or (and (boundp ',cache-name)
,cache-name
(gethash ,key ,cache-name))
(let ((,gensym (progn ,@(if (stringp doc)
body
(cons doc body)))))
(when (and (boundp ',cache-name)
,cache-name)
(puthash ,key ,gensym ,cache-name))
,gensym))))))

(provide 'malabar-util)

0 comments on commit 20130cd

Please sign in to comment.