X-Git-Url: http://christophe.rhodes.io/gitweb/?p=specializable.git;a=blobdiff_plain;f=specializable.lisp;h=8d01319b1267e28a5da465263fbb8b4df27163c5;hp=b74d73fdd506347aa61dd14de4b71f94ecbfe70e;hb=d55ebbbcbd77023c799d8d95dce5d3772aec5841;hpb=3ad29ae97f2b75c730b7df5af55c73e316b17606 diff --git a/specializable.lisp b/specializable.lisp index b74d73f..8d01319 100644 --- a/specializable.lisp +++ b/specializable.lisp @@ -20,6 +20,7 @@ (in-package "SPECIALIZABLE") (defclass extended-specializer (sb-mop:specializer) + ;; FIXME: this doesn't actually do quite what I wanted. ((direct-methods-table :allocation :class :initform nil :accessor direct-methods-table))) @@ -43,21 +44,28 @@ (remove-duplicates (mapcar #'sb-mop:method-generic-function (sb-mop:specializer-direct-methods specializer)))) (defclass specializable-generic-function (standard-generic-function) - ((extended-specializers :initform (make-hash-table :test 'equal) - :reader generic-function-extended-specializers) - (emf-table :initform (make-hash-table :test 'equal) :reader emf-table)) + ((emf-table :initform (make-hash-table :test 'equal) :reader emf-table) + (cacheingp :initform t :initarg :cacheingp) + (single-arg-cacheing-p :initform t :initarg :single-arg-cacheing-p)) (:metaclass sb-mop:funcallable-standard-class) (:default-initargs :method-class (find-class 'specializable-method))) -(defclass specializable-method (standard-method) - ((lambda-expression :initarg :lambda-expression - :accessor specializable-method-lambda-expression))) +;;; TODO: we don't use this class yet, but we might do later +(defclass specializable-method (standard-method) ()) + +;;; TODO use info? +(defun extended-specializer-name-p (name) + (and (symbolp name) + (get name 'extended-specializer-parser))) + +(deftype extended-specializer-name () + `(satisfies extended-specializer-name-p)) (defmacro define-extended-specializer (name (gf-var &rest args) &body body) ;; FIXME: unparser `(setf (get ',name 'extended-specializer-parser) - (lambda (,gf-var ,@args) - ,@body))) + (lambda (,gf-var ,@args) + ,@body))) ;; doesn't work, because we'd have to dump GF into the fasl for the macro ;; expansion @@ -81,15 +89,10 @@ ;;; from SBCL: (defmethod sb-pcl:parse-specializer-using-class - ((gf specializable-generic-function) name) - (cond - ((typep name 'sb-mop:specializer) name) - ((symbolp name) (find-class name)) - ((consp name) - (case (car name) - (eql (sb-mop:intern-eql-specializer (cadr name))) - (t (make-extended-specializer name)))) - (t (error "unexpected specializer name")))) + ((gf specializable-generic-function) (specializer-name t)) + (if (typep specializer-name '(cons extended-specializer-name)) + (make-extended-specializer specializer-name) + (call-next-method))) (defmethod sb-pcl:make-method-specializers-form ((gf specializable-generic-function) method snames env) @@ -120,43 +123,76 @@ ((gf specializable-generic-function) (g class)) (sb-pcl::class-wrapper g)) +(defun first-arg-only-special-case (gf) + (let ((arg-info (sb-pcl::gf-arg-info gf))) + (and (slot-value gf 'single-arg-cacheing-p) + (>= (sb-pcl::arg-info-number-required arg-info) 1) + (every (lambda (x) (eql x t)) + (cdr (sb-pcl::arg-info-metatypes arg-info)))))) + ;;; FIXME: in some kind of order, the discriminating function needs to handle: ;;; - argument count checking; -;;; - keyword argument validity; +;;; - DONE (in effective method) keyword argument validity; ;;; - DONE flushing the emf cache on method addition/removal ;;; - DONE (sort of, using wrappers/g-e-h-k) flushing the cache on class redefinition; ;;; - cache thread-safety. +;;; - speed +;;; - DONE (in SBCL itself) interaction with TRACE et al. (defmethod sb-mop:compute-discriminating-function ((gf specializable-generic-function)) - (lambda (&rest args) - (let* ((generalizers (mapcar (lambda (x) (generalizer-of-using-class gf x)) - (required-portion gf args))) - (keys (mapcar (lambda (x) (generalizer-equal-hash-key gf x)) generalizers)) - (emfun (gethash keys (emf-table gf) nil))) - (if emfun - (sb-pcl::invoke-emf emfun args) - (slow-method-lookup gf args generalizers))))) + (cond + ((not (slot-value gf 'cacheingp)) + (lambda (&rest args) + (let ((generalizers (mapcar (lambda (x) (generalizer-of-using-class gf x)) + args))) + (slow-method-lookup-and-call gf args generalizers)))) + ((first-arg-only-special-case gf) + (lambda (&rest args) + (let* ((g (generalizer-of-using-class gf (car args))) + (k (generalizer-equal-hash-key gf g)) + (emfun (gethash k (emf-table gf) nil))) + (if emfun + (sb-pcl::invoke-emf emfun args) + (slow-method-lookup-and-call + gf args (cons g (mapcar (lambda (x) (generalizer-of-using-class gf x)) + (cdr (required-portion gf args))))))))) + (t + (lambda (&rest args) + (let* ((generalizers (mapcar (lambda (x) (generalizer-of-using-class gf x)) + (required-portion gf args))) + (keys (mapcar (lambda (x) (generalizer-equal-hash-key gf x)) generalizers)) + (emfun (gethash keys (emf-table gf) nil))) + (if emfun + (sb-pcl::invoke-emf emfun args) + (slow-method-lookup-and-call gf args generalizers))))))) (defmethod reinitialize-instance :after ((gf specializable-generic-function) &key) (clrhash (emf-table gf))) (defun slow-method-lookup (gf args generalizers) - ;; differs from closette (multiple-value-bind (applicable-methods definitivep) (compute-applicable-methods-using-generalizers gf generalizers) - (if definitivep - (let* ((emfun - (compute-effective-method-function gf applicable-methods)) - (keys (mapcar (lambda (x) (generalizer-equal-hash-key gf x)) generalizers))) - (setf (gethash keys (emf-table gf)) emfun) - (sb-pcl::invoke-emf emfun args)) - (sb-pcl::invoke-emf (compute-effective-method-function - gf (sb-mop:compute-applicable-methods gf args)) - args)))) + (unless definitivep + (setf applicable-methods (compute-applicable-methods gf args))) + (values (compute-effective-method-function gf applicable-methods) + definitivep))) + +(defun slow-method-lookup-and-call (gf args generalizers) + (multiple-value-bind (emf cacheablep) + (slow-method-lookup gf args generalizers) + (when cacheablep + (let ((keys (mapcar (lambda (x) (generalizer-equal-hash-key gf x)) generalizers))) + (if (first-arg-only-special-case gf) + (setf (gethash (car keys) (emf-table gf)) emf) + (setf (gethash keys (emf-table gf)) emf)))) + (sb-pcl::invoke-emf emf args))) (defun compute-effective-method-function (gf methods) - (let* ((mc (sb-mop:generic-function-method-combination gf)) - (em (sb-mop:compute-effective-method gf mc methods))) - (sb-pcl::make-effective-method-function gf em))) + (if (null methods) + (lambda (&rest args) (apply #'no-applicable-method gf args)) + (let* ((mc (sb-mop:generic-function-method-combination gf)) + (sb-pcl::*applicable-methods* methods) + (em (sb-mop:compute-effective-method gf mc methods))) + (sb-pcl::make-effective-method-function gf em)))) ;; new, not in closette (defgeneric generalizer-of-using-class (generic-function object))