1 ;;;; Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3 ;;;; This program is free software; you can redistribute it and/or modify
4 ;;;; it under the terms of the GNU General Public License as published by
5 ;;;; the Free Software Foundation; either version 2, or (at your option)
6 ;;;; any later version.
8 ;;;; This program 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
11 ;;;; GNU General Public License for more details.
13 ;;;; You should have received a copy of the GNU General Public License
14 ;;;; along with this software; see the file COPYING. If not, write to
15 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 ;;;; Boston, MA 02111-1307 USA
18 ;;;; As a special exception, the Free Software Foundation gives permission
19 ;;;; for additional uses of the text contained in its release of GUILE.
21 ;;;; The exception is that, if you link the GUILE library with other files
22 ;;;; to produce an executable, this does not by itself cause the
23 ;;;; resulting executable to be covered by the GNU General Public License.
24 ;;;; Your use of that executable is in no way restricted on account of
25 ;;;; linking the GUILE library code into it.
27 ;;;; This exception does not however invalidate any other reasons why
28 ;;;; the executable file might be covered by the GNU General Public License.
30 ;;;; This exception applies only to the code released by the
31 ;;;; Free Software Foundation under the name GUILE. If you copy
32 ;;;; code from other Free Software Foundation releases into a copy of
33 ;;;; GUILE, as the General Public License permits, the exception does
34 ;;;; not apply to the code that you add in this way. To avoid misleading
35 ;;;; anyone as to the status of such modified files, you must delete
36 ;;;; this exception notice from them.
38 ;;;; If you write modifications of your own for GUILE, it is your choice
39 ;;;; whether to permit this exception to apply to your modifications.
40 ;;;; If you do not wish that, delete this exception notice.
44 (define-module (oop goops dispatch)
45 :use-module (oop goops)
46 :use-module (oop goops util)
47 :use-module (oop goops compile)
48 :export (memoize-method!)
53 ;;; This file implements method memoization. It will finally be
54 ;;; implemented on C level in order to obtain fast generic function
55 ;;; application also during the first pass through the code.
63 (define hashset-index 6)
65 (define hash-threshold 3)
66 (define initial-hash-size 4) ;must be a power of 2 and >= hash-threshold
68 (define initial-hash-size-1 (- initial-hash-size 1))
70 (define the-list-of-no-method '(no-method))
76 ;; (#@dispatch args N-SPECIALIZED #((TYPE1 ... ENV FORMALS FORM1 ...) ...) GF)
77 ;; (#@dispatch args N-SPECIALIZED HASHSET MASK
78 ;; #((TYPE1 ... ENV FORMALS FORM1 ...) ...)
85 (define method-cache-entries cadddr)
87 (define (set-method-cache-entries! mcache entries)
88 (set-car! (cdddr mcache) entries))
90 (define (method-cache-n-methods exp)
91 (n-cache-methods (method-cache-entries exp)))
93 (define (method-cache-methods exp)
94 (cache-methods (method-cache-entries exp)))
98 (define (set-hashed-method-cache-hashset! exp hashset)
99 (set-car! (cdddr exp) hashset))
101 (define (set-hashed-method-cache-mask! exp mask)
102 (set-car! (cddddr exp) mask))
104 (define (hashed-method-cache-entries exp)
107 (define (set-hashed-method-cache-entries! exp entries)
108 (set-car! (list-cdr-ref exp 5) entries))
112 (define (method-cache-generic-function exp)
113 (list-ref exp (if (method-cache-hashed? exp) 6 4)))
117 (define (method-cache-hashed? x)
118 (integer? (cadddr x)))
120 (define max-non-hashed-index (- hash-threshold 2))
122 (define (passed-hash-threshold? exp)
123 (and (> (vector-length (method-cache-entries exp)) max-non-hashed-index)
124 (struct? (car (vector-ref (method-cache-entries exp)
125 max-non-hashed-index)))))
127 ;;; Converting a method cache to hashed form
129 (define (method-cache->hashed! exp)
130 (set-cdr! (cddr exp) (cons 0 (cons initial-hash-size-1 (cdddr exp))))
137 (define (n-cache-methods entries)
138 (do ((i (- (vector-length entries) 1) (- i 1)))
139 ((or (< i 0) (struct? (car (vector-ref entries i))))
142 (define (cache-methods entries)
143 (do ((i (- (vector-length entries) 1) (- i 1))
144 (methods '() (let ((entry (vector-ref entries i)))
145 (if (struct? (car entry))
154 (define (method-cache-insert! exp entry)
155 (let* ((entries (method-cache-entries exp))
156 (n (n-cache-methods entries)))
157 (if (>= n (vector-length entries))
159 (let ((new-entries (make-vector (* 2 (vector-length entries))
160 the-list-of-no-method)))
163 (vector-set! new-entries i (vector-ref entries i)))
164 (vector-set! new-entries n entry)
165 (set-method-cache-entries! exp new-entries))
166 (vector-set! entries n entry))))
168 (define (hashed-method-cache-insert! exp entry)
169 (let* ((cache (hashed-method-cache-entries exp))
170 (size (vector-length cache)))
171 (let* ((entries (cons entry (cache-methods cache)))
172 (size (if (<= (length entries) size)
174 ;; larger size required
175 (let ((new-size (* 2 size)))
176 (set-hashed-method-cache-mask! exp (- new-size 1))
180 (do ((hashset 0 (+ 1 hashset)))
181 ((= hashset hashsets))
182 (let* ((test-cache (make-vector size the-list-of-no-method))
183 (misses (cache-try-hash! min-misses hashset test-cache entries)))
184 (cond ((zero? misses)
187 (set! cache test-cache)
188 (set! hashset (- hashsets 1)))
189 ((< misses min-misses)
190 (set! min-misses misses)
192 (set! cache test-cache)))))
193 (set-hashed-method-cache-hashset! exp best)
194 (set-hashed-method-cache-entries! exp cache))))
200 (define environment? pair?)
202 (define (cache-hashval hashset entry)
203 (let ((hashset-index (+ hashset-index hashset)))
205 (classes entry (cdr classes)))
206 ((environment? (car classes)) sum)
207 (set! sum (+ sum (struct-ref (car classes) hashset-index))))))
209 (define (cache-try-hash! min-misses hashset cache entries)
211 (mask (- (vector-length cache) 1)))
214 (do ((ls entries (cdr ls))
216 ((null? ls) max-misses)
217 (do ((i (logand mask (cache-hashval hashset (car ls)))
218 (logand mask (+ i 1))))
219 ((not (struct? (car (vector-ref cache i))))
220 (vector-set! cache i (car ls)))
221 (set! misses (+ 1 misses))
222 (if (>= misses min-misses)
223 (throw 'misses misses)))
224 (if (> misses max-misses)
225 (set! max-misses misses))))
233 ;; Backward compatibility
234 (if (not (defined? 'lookup-create-cmethod))
235 (define (lookup-create-cmethod gf args)
236 (no-applicable-method (car args) (cadr args))))
238 (define (memoize-method! gf args exp)
239 (if (not (slot-ref gf 'used-by))
240 (slot-set! gf 'used-by '()))
241 (let ((applicable ((if (eq? gf compute-applicable-methods)
242 %compute-applicable-methods
243 compute-applicable-methods)
246 ;; *fixme* dispatch.scm needs rewriting Since the current
247 ;; code mutates the method cache, we have to work on a
248 ;; copy. Otherwise we might disturb another thread
249 ;; currently dispatching on the cache. (No need to copy
251 (let* ((new (list-copy exp))
253 (cond ((method-cache-hashed? new)
254 (method-cache-install! hashed-method-cache-insert!
255 new args applicable))
256 ((passed-hash-threshold? new)
257 (method-cache-install! hashed-method-cache-insert!
258 (method-cache->hashed! new)
262 (method-cache-install! method-cache-insert!
263 new args applicable)))))
264 (set-cdr! (cdr exp) (cddr new))
267 (lookup-create-cmethod no-applicable-method (list gf '())))
269 ;; Mutate arglist to fit no-applicable-method
270 (set-cdr! args (list (cons (car args) (cdr args))))
272 (lookup-create-cmethod no-applicable-method args)))))
274 (set-procedure-property! memoize-method! 'system-procedure #t)
276 (define method-cache-install!
279 (if (or (zero? n) (null? ls))
281 (cons (car ls) (first-n (cdr ls) (- n 1)))))))
282 (lambda (insert! exp args applicable)
283 (let* ((specializers (method-specializers (car applicable)))
285 (if (list? specializers)
286 (length specializers)
287 (+ 1 (slot-ref (method-cache-generic-function exp)
289 (let* ((types (map class-of (first-n args n-specializers)))
290 (entry+cmethod (compute-entry-with-cmethod applicable types)))
291 (insert! exp (car entry+cmethod)) ; entry = types + cmethod
292 (cdr entry+cmethod) ; cmethod