Changeset 7488 for branches/working-0710


Ignore:
Timestamp:
Oct 22, 2007, 2:29:37 AM (12 years ago)
Author:
wws
Message:

Support for some user optimization of generic function dispatch.

(ccl:optimize-generic-function-dispatching)

Calls ccl:override-one-method-one-arg-dcode on all generic functions
that do standard method dispatch and have a single method specialized
on the first argument. There is no way to undo the optimizations
globally, but redefining a method will undo the optimization for its
generic function.

(ccl:override-one-method-one-arg-dcode generic-function method)

Specialize this method to optimize generic function dispatch for a
generic function that does standard method dispatch on a single method
specialized on the first argument.

You'll have to be able to recognize methods for which your
optimization will work, and you'll have to understand how to override
(ccl::%gf-dcode generic-function) for generic functions specialized on
only the first argument.

Example (does very little actual optimization, but illustrates the idea):

(defclass my-reader-method (standard-method)

((slot :initform nil :accessor my-reader-method-slot)))

(defclass my-class ()

((my-slot :initform :uninitialized)))

(defun initialize-slot (instance slot)

(setf (slot-value instance slot) slot))

;; This will likely be done by a macro
(defgeneric read-my-slot (instance) (:method-class my-reader-method))
(setf (my-reader-method-slot

(defmethod read-my-slot ((instance my-class))

(let ((value (slot-value instance 'my-slot)))

(if (eq value :uninitialized)

(initialize-slot instance 'my-slot)

value))))

'my-slot)

(defmethod ccl:override-one-method-one-arg-dcode (gf (method my-reader-method))

(let* ((slot (my-reader-method-slot method))

(slot-id (ccl::ensure-slot-id slot)))

(when slot

(flet ((optimized-read-method (dt arg)

(let ((value (ccl::slot-id-value arg slot-id)))

(if (eq value :uninitialized)

(ccl::%%one-arg-dcode dt arg) ;do normal dispatching

value))))

(setf (ccl::%gf-dcode gf) #'optimized-read-method)))))

;; To enable the optimizations
(ccl:optimize-generic-function-dispatching)

Location:
branches/working-0710/ccl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/working-0710/ccl/level-1/l1-clos.lisp

    r5944 r7488  
    17381738                             newval))))))))))
    17391739  instance)
     1740
     1741;;; Sometimes you can do a lot better at generic function dispatch than the
     1742;;; default. This supports that for the one-arg-dcode case.
     1743(defmethod override-one-method-one-arg-dcode ((generic-function t) (method t))
     1744  nil)
     1745
     1746(defun optimize-generic-function-dispatching ()
     1747  (dolist (gf (population.data %all-gfs%))
     1748    (when (eq #'%%one-arg-dcode (%gf-dcode gf))
     1749      (let ((methods (generic-function-methods gf)))
     1750        (when (eql 1 (length methods))
     1751          (override-one-method-one-arg-dcode gf (car methods)))))))
  • branches/working-0710/ccl/lib/ccl-export-syms.lisp

    r7410 r7488  
    222222     specializer-direct-generic-functions
    223223     copy-instance
     224
     225     override-one-method-one-arg-dcode
     226     optimize-generic-function-dispatching
    224227
    225228     ;; Not MOP
Note: See TracChangeset for help on using the changeset viewer.