dispatch.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. ;;;; Copyright (C) 1999, 2000, 2001, 2003, 2006, 2009 Free Software Foundation, Inc.
  2. ;;;;
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;;;
  17. ;; There are circularities here; you can't import (oop goops compile)
  18. ;; before (oop goops). So when compiling, make sure that things are
  19. ;; kosher.
  20. (eval-when (compile) (resolve-module '(oop goops)))
  21. (define-module (oop goops dispatch)
  22. #:use-module (oop goops)
  23. #:use-module (oop goops util)
  24. #:use-module (oop goops compile)
  25. #:export (memoize-method!)
  26. #:no-backtrace)
  27. (define *dispatch-module* (current-module))
  28. ;;;
  29. ;;; Generic functions have an applicable-methods cache associated with
  30. ;;; them. Every distinct set of types that is dispatched through a
  31. ;;; generic adds an entry to the cache. This cache gets compiled out to
  32. ;;; a dispatch procedure. In steady-state, this dispatch procedure is
  33. ;;; never recompiled; but during warm-up there is some churn, both to
  34. ;;; the cache and to the dispatch procedure.
  35. ;;;
  36. ;;; So what is the deal if warm-up happens in a multithreaded context?
  37. ;;; There is indeed a window between missing the cache for a certain set
  38. ;;; of arguments, and then updating the cache with the newly computed
  39. ;;; applicable methods. One of the updaters is liable to lose their new
  40. ;;; entry.
  41. ;;;
  42. ;;; This is actually OK though, because a subsequent cache miss for the
  43. ;;; race loser will just cause memoization to try again. The cache will
  44. ;;; eventually be consistent. We're not mutating the old part of the
  45. ;;; cache, just consing on the new entry.
  46. ;;;
  47. ;;; It doesn't even matter if the dispatch procedure and the cache are
  48. ;;; inconsistent -- most likely the type-set that lost the dispatch
  49. ;;; procedure race will simply re-trigger a memoization, but since the
  50. ;;; winner isn't in the effective-methods cache, it will likely also
  51. ;;; re-trigger a memoization, and the cache will finally be consistent.
  52. ;;; As you can see there is a possibility for ping-pong effects, but
  53. ;;; it's unlikely given the shortness of the window between slot-set!
  54. ;;; invocations. We could add a mutex, but it is strictly unnecessary,
  55. ;;; and would add runtime cost and complexity.
  56. ;;;
  57. (define (emit-linear-dispatch gf-sym nargs methods free rest?)
  58. (define (gen-syms n stem)
  59. (let lp ((n (1- n)) (syms '()))
  60. (if (< n 0)
  61. syms
  62. (lp (1- n) (cons (gensym stem) syms)))))
  63. (let* ((args (gen-syms nargs "a"))
  64. (types (gen-syms nargs "t")))
  65. (let lp ((methods methods)
  66. (free free)
  67. (exp `(cache-miss ,gf-sym
  68. ,(if rest?
  69. `(cons* ,@args rest)
  70. `(list ,@args)))))
  71. (cond
  72. ((null? methods)
  73. (values `(,(if rest? `(,@args . rest) args)
  74. (let ,(map (lambda (t a)
  75. `(,t (class-of ,a)))
  76. types args)
  77. ,exp))
  78. free))
  79. (else
  80. ;; jeez
  81. (let preddy ((free free)
  82. (types types)
  83. (specs (vector-ref (car methods) 1))
  84. (checks '()))
  85. (if (null? types)
  86. (let ((m-sym (gensym "p")))
  87. (lp (cdr methods)
  88. (acons (vector-ref (car methods) 3)
  89. m-sym
  90. free)
  91. `(if (and . ,checks)
  92. ,(if rest?
  93. `(apply ,m-sym ,@args rest)
  94. `(,m-sym . ,args))
  95. ,exp)))
  96. (let ((var (assq-ref free (car specs))))
  97. (if var
  98. (preddy free
  99. (cdr types)
  100. (cdr specs)
  101. (cons `(eq? ,(car types) ,var)
  102. checks))
  103. (let ((var (gensym "c")))
  104. (preddy (acons (car specs) var free)
  105. (cdr types)
  106. (cdr specs)
  107. (cons `(eq? ,(car types) ,var)
  108. checks))))))))))))
  109. (define (compute-dispatch-procedure gf cache)
  110. (define (scan)
  111. (let lp ((ls cache) (nreq -1) (nrest -1))
  112. (cond
  113. ((null? ls)
  114. (collate (make-vector (1+ nreq) '())
  115. (make-vector (1+ nrest) '())))
  116. ((vector-ref (car ls) 2) ; rest
  117. (lp (cdr ls) nreq (max nrest (vector-ref (car ls) 0))))
  118. (else ; req
  119. (lp (cdr ls) (max nreq (vector-ref (car ls) 0)) nrest)))))
  120. (define (collate req rest)
  121. (let lp ((ls cache))
  122. (cond
  123. ((null? ls)
  124. (emit req rest))
  125. ((vector-ref (car ls) 2) ; rest
  126. (let ((n (vector-ref (car ls) 0)))
  127. (vector-set! rest n (cons (car ls) (vector-ref rest n)))
  128. (lp (cdr ls))))
  129. (else ; req
  130. (let ((n (vector-ref (car ls) 0)))
  131. (vector-set! req n (cons (car ls) (vector-ref req n)))
  132. (lp (cdr ls)))))))
  133. (define (emit req rest)
  134. (let ((gf-sym (gensym "g")))
  135. (define (emit-rest n clauses free)
  136. (if (< n (vector-length rest))
  137. (let ((methods (vector-ref rest n)))
  138. (cond
  139. ((null? methods)
  140. (emit-rest (1+ n) clauses free))
  141. ;; FIXME: hash dispatch
  142. (else
  143. (call-with-values
  144. (lambda ()
  145. (emit-linear-dispatch gf-sym n methods free #t))
  146. (lambda (clause free)
  147. (emit-rest (1+ n) (cons clause clauses) free))))))
  148. (emit-req (1- (vector-length req)) clauses free)))
  149. (define (emit-req n clauses free)
  150. (if (< n 0)
  151. (comp `(lambda ,(map cdr free)
  152. (case-lambda ,@clauses))
  153. (map car free))
  154. (let ((methods (vector-ref req n)))
  155. (cond
  156. ((null? methods)
  157. (emit-req (1- n) clauses free))
  158. ;; FIXME: hash dispatch
  159. (else
  160. (call-with-values
  161. (lambda ()
  162. (emit-linear-dispatch gf-sym n methods free #f))
  163. (lambda (clause free)
  164. (emit-req (1- n) (cons clause clauses) free))))))))
  165. (emit-rest 0
  166. (if (or (zero? (vector-length rest))
  167. (null? (vector-ref rest 0)))
  168. (list `(args (cache-miss ,gf-sym args)))
  169. '())
  170. (acons gf gf-sym '()))))
  171. (define (comp exp vals)
  172. (let ((p ((@ (system base compile) compile) exp #:env *dispatch-module*)))
  173. (apply p vals)))
  174. ;; kick it.
  175. (scan))
  176. ;; o/~ ten, nine, eight
  177. ;; sometimes that's just how it goes
  178. ;; three, two, one
  179. ;;
  180. ;; get out before it blows o/~
  181. ;;
  182. (define timer-init 30)
  183. (define (delayed-compile gf)
  184. (let ((timer timer-init))
  185. (lambda args
  186. (set! timer (1- timer))
  187. (cond
  188. ((zero? timer)
  189. (let ((dispatch (compute-dispatch-procedure
  190. gf (slot-ref gf 'effective-methods))))
  191. (slot-set! gf 'procedure dispatch)
  192. (apply dispatch args)))
  193. (else
  194. ;; interestingly, this catches recursive compilation attempts as
  195. ;; well; in that case, timer is negative
  196. (cache-dispatch gf args))))))
  197. (define (cache-dispatch gf args)
  198. (define (map-until n f ls)
  199. (if (or (zero? n) (null? ls))
  200. '()
  201. (cons (f (car ls)) (map-until (1- n) f (cdr ls)))))
  202. (define (equal? x y) ; can't use the stock equal? because it's a generic...
  203. (cond ((pair? x) (and (pair? y)
  204. (eq? (car x) (car y))
  205. (equal? (cdr x) (cdr y))))
  206. ((null? x) (null? y))
  207. (else #f)))
  208. (if (slot-ref gf 'n-specialized)
  209. (let ((types (map-until (slot-ref gf 'n-specialized) class-of args)))
  210. (let lp ((cache (slot-ref gf 'effective-methods)))
  211. (cond ((null? cache)
  212. (cache-miss gf args))
  213. ((equal? (vector-ref (car cache) 1) types)
  214. (apply (vector-ref (car cache) 3) args))
  215. (else (lp (cdr cache))))))
  216. (cache-miss gf args)))
  217. (define (cache-miss gf args)
  218. (apply (memoize-method! gf args) args))
  219. (define (memoize-effective-method! gf args applicable)
  220. (define (first-n ls n)
  221. (if (or (zero? n) (null? ls))
  222. '()
  223. (cons (car ls) (first-n (cdr ls) (- n 1)))))
  224. (define (parse n ls)
  225. (cond ((null? ls)
  226. (memoize n #f (map class-of args)))
  227. ((= n (slot-ref gf 'n-specialized))
  228. (memoize n #t (map class-of (first-n args n))))
  229. (else
  230. (parse (1+ n) (cdr ls)))))
  231. (define (memoize len rest? types)
  232. (let* ((cmethod (compute-cmethod applicable types))
  233. (cache (cons (vector len types rest? cmethod)
  234. (slot-ref gf 'effective-methods))))
  235. (slot-set! gf 'effective-methods cache)
  236. (slot-set! gf 'procedure (delayed-compile gf))
  237. cmethod))
  238. (parse 0 args))
  239. ;;;
  240. ;;; Memoization
  241. ;;;
  242. (define (memoize-method! gf args)
  243. (let ((applicable ((if (eq? gf compute-applicable-methods)
  244. %compute-applicable-methods
  245. compute-applicable-methods)
  246. gf args)))
  247. (cond (applicable
  248. (memoize-effective-method! gf args applicable))
  249. (else
  250. (no-applicable-method gf args)))))
  251. (set-procedure-property! memoize-method! 'system-procedure #t)