Debian Codegraph @

src/clos/dispatch.lsp

raw
1 ;;;;
2 ;;;; Copyright (c) 2021, Daniel Kochmański
3 ;;;; See file 'LICENSE' for the copyright details.
4 ;;;;
5
6 ;;;
7 ;;; This file contains implementations of various attempts at the generic
8 ;;; function dispatch.
9 ;;;
10 (in-package #:clos)
11
12 ;;; This function is similar to `generic_compute_applicable_method' in C.
13 (defun generic-compute-applicable-method (gf args)
14 (declare (si::c-local))
15 (multiple-value-bind (method-list ok)
16 (compute-applicable-methods-using-classes gf (mapcar #'class-of args))
17 (unless ok
18 (setf method-list (compute-applicable-methods gf args)))
19 (and method-list
20 (let ((combin (generic-function-method-combination gf)))
21 (compute-effective-method-function gf combin method-list)))))
22
23 ;;; This function is similar to `restricted_compute_applicable_method' in C.
24 (defun restricted-compute-applicable-method (gf args)
25 (declare (si::c-local))
26 (with-early-accessors (+standard-generic-function-slots+)
27 (let ((method-list (std-compute-applicable-methods gf args)))
28 (and method-list
29 (let ((combin (generic-function-method-combination gf)))
30 (compute-effective-method-function gf combin method-list))))))
31
32 ;;; This function is similar to `compute_applicable_method' in C.
33 (defun compute-applicable-method (gf args)
34 (declare (si::c-local))
35 (if (eq (slot-value (class-of gf) 'name) 'standard-generic-function)
36 (restricted-compute-applicable-method gf args)
37 (generic-compute-applicable-method gf args)))
38
39 ;;; This is an unoptimized discriminator function that doesn't cache results.
40 (defun unoptimized-discriminator (gf)
41 (lambda (&rest args)
42 (if-let ((fn (compute-applicable-method gf args)))
43 (funcall fn args nil)
44 (apply #'no-applicable-method gf args))))
45
46 ;;; This discriminator is similar to the discriminator implemented in C. It is
47 ;;; slower, incomplete and defined only for the purpose of benchmarking against
48 ;;; other dispatch prototypes written in Lisp and to better understand C code.
49 ;;;
50 ;;; Dispatch is around 1x-4x slower than the "native" implementation and this
51 ;;; code is reasonably simplified. FIXME accessor optimization is missing.
52 #+ (or)
53 (defun soft-legacy-discriminator (gf)
54 (let ((method-cache (make-hash-table :test #'equal :synchronized t)))
55 (lambda (&rest args)
56 (let ((hash-key
57 ;; GENERIC-FUNCTION-SPEC-LIST is maintained by the function
58 ;; COMPUTE-G-F-SPEC-LIST called on discriminator invalidation.
59 (loop for arg in args
60 for (spec-class . spec-eql) in (generic-function-spec-list gf)
61 if (member arg spec-eql)
62 collect `(eql ,arg) into hash-key
63 else
64 collect (class-of arg) into hash-key
65 finally (return (list* gf hash-key)))))
66 (if-let ((fn (gethash hash-key method-cache)))
67 (funcall fn args nil)
68 (if-let ((fn (compute-applicable-method gf args)))
69 (progn
70 (setf (gethash hash-key method-cache) fn)
71 (funcall fn args nil))
72 (apply #'no-applicable-method gf args)))))))