forked from dmitryvk/sbcl-win32-threads
/
dfun.lisp
1593 lines (1447 loc) · 56.9 KB
/
dfun.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;; This software is derived from software originally released by Xerox
;;;; Corporation. Copyright and release statements follow. Later modifications
;;;; to the software are in the public domain and are provided with
;;;; absolutely no warranty. See the COPYING and CREDITS files for more
;;;; information.
;;;; copyright information from original PCL sources:
;;;;
;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
;;;; All rights reserved.
;;;;
;;;; Use and copying of this software and preparation of derivative works based
;;;; upon this software are permitted. Any distribution of this software or
;;;; derivative works must comply with all applicable United States export
;;;; control laws.
;;;;
;;;; This software is made available AS IS, and Xerox Corporation makes no
;;;; warranty about the software, its performance or its conformity to any
;;;; specification.
(in-package "SB-PCL")
#|
This implementation of method lookup was redone in early August of 89.
It has the following properties:
- Its modularity makes it easy to modify the actual caching algorithm.
The caching algorithm is almost completely separated into the files
cache.lisp and dlap.lisp. This file just contains the various uses
of it. There will be more tuning as we get more results from Luis'
measurements of caching behavior.
- The metacircularity issues have been dealt with properly. All of
PCL now grounds out properly. Moreover, it is now possible to have
metaobject classes which are themselves not instances of standard
metaobject classes.
** Modularity of the code **
The actual caching algorithm is isolated in a modest number of functions.
The code which generates cache lookup code is all found in cache.lisp and
dlap.lisp. Certain non-wrapper-caching special cases are in this file.
** Handling the metacircularity **
In CLOS, method lookup is the potential source of infinite metacircular
regress. The metaobject protocol specification gives us wide flexibility
in how to address this problem. PCL uses a technique which handles the
problem not only for the metacircular language described in Chapter 3, but
also for the PCL protocol which includes additional generic functions
which control more aspects of the CLOS implementation.
The source of the metacircular regress can be seen in a number of ways.
One is that the specified method lookup protocol must, as part of doing
the method lookup (or at least the cache miss case), itself call generic
functions. It is easy to see that if the method lookup for a generic
function ends up calling that same generic function there can be trouble.
Fortunately, there is an easy solution at hand. The solution is based on
the restriction that portable code cannot change the class of a specified
metaobject. This restriction implies that for specified generic functions,
the method lookup protocol they follow is fixed.
More precisely, for such specified generic functions, most generic functions
that are called during their own method lookup will not run portable methods.
This allows the implementation to usurp the actual generic function call in
this case. In short, method lookup of a standard generic function, in the
case where the only applicable methods are themselves standard doesn't
have to do any method lookup to implement itself.
And so, we are saved.
|#
;;; an alist in which each entry is of the form
;;; (<generator> . (<subentry> ...)).
;;; Each subentry is of the form
;;; (<args> <constructor> <system>).
(defvar *dfun-constructors* ())
;;; If this is NIL, then the whole mechanism for caching dfun constructors is
;;; turned off. The only time that makes sense is when debugging LAP code.
(defvar *enable-dfun-constructor-caching* t)
(defun show-dfun-constructors ()
(format t "~&DFUN constructor caching is ~A."
(if *enable-dfun-constructor-caching*
"enabled" "disabled"))
(dolist (generator-entry *dfun-constructors*)
(dolist (args-entry (cdr generator-entry))
(format t "~&~S ~S"
(cons (car generator-entry) (caar args-entry))
(caddr args-entry)))))
(defvar *raise-metatypes-to-class-p* t)
(defun get-dfun-constructor (generator &rest args)
(when (and *raise-metatypes-to-class-p*
(member generator '(emit-checking emit-caching
emit-in-checking-cache-p emit-constant-value)))
(setq args (cons (mapcar #'(lambda (mt)
(if (eq mt t)
mt
'class))
(car args))
(cdr args))))
(let* ((generator-entry (assq generator *dfun-constructors*))
(args-entry (assoc args (cdr generator-entry) :test #'equal)))
(if (null *enable-dfun-constructor-caching*)
(apply (fdefinition generator) args)
(or (cadr args-entry)
(multiple-value-bind (new not-best-p)
(apply (symbol-function generator) args)
(let ((entry (list (copy-list args) new (unless not-best-p 'pcl)
not-best-p)))
(if generator-entry
(push entry (cdr generator-entry))
(push (list generator entry)
*dfun-constructors*)))
(values new not-best-p))))))
(defun load-precompiled-dfun-constructor (generator args system constructor)
(let* ((generator-entry (assq generator *dfun-constructors*))
(args-entry (assoc args (cdr generator-entry) :test #'equal)))
(if args-entry
(when (fourth args-entry)
(let* ((dfun-type (case generator
(emit-checking 'checking)
(emit-caching 'caching)
(emit-constant-value 'constant-value)
(emit-default-only 'default-method-only)))
(metatypes (car args))
(gfs (when dfun-type (gfs-of-type dfun-type))))
(dolist (gf gfs)
(when (and (equal metatypes
(arg-info-metatypes (gf-arg-info gf)))
(let ((gf-name (generic-function-name gf)))
(and (not (eq gf-name 'slot-value-using-class))
(not (equal gf-name
'(setf slot-value-using-class)))
(not (eq gf-name 'slot-boundp-using-class)))))
(update-dfun gf)))
(setf (second args-entry) constructor)
(setf (third args-entry) system)
(setf (fourth args-entry) nil)))
(let ((entry (list args constructor system nil)))
(if generator-entry
(push entry (cdr generator-entry))
(push (list generator entry) *dfun-constructors*))))))
(defmacro precompile-dfun-constructors (&optional system)
(let ((*precompiling-lap* t))
`(progn
,@(gathering1 (collecting)
(dolist (generator-entry *dfun-constructors*)
(dolist (args-entry (cdr generator-entry))
(when (or (null (caddr args-entry))
(eq (caddr args-entry) system))
(when system (setf (caddr args-entry) system))
(gather1
`(load-precompiled-dfun-constructor
',(car generator-entry)
',(car args-entry)
',system
,(apply (fdefinition (car generator-entry))
(car args-entry)))))))))))
;;; When all the methods of a generic function are automatically
;;; generated reader or writer methods a number of special
;;; optimizations are possible. These are important because of the
;;; large number of generic functions of this type.
;;;
;;; There are a number of cases:
;;;
;;; ONE-CLASS-ACCESSOR
;;; In this case, the accessor generic function has only been
;;; called with one class of argument. There is no cache vector,
;;; the wrapper of the one class, and the slot index are stored
;;; directly as closure variables of the discriminating function.
;;; This case can convert to either of the next kind.
;;;
;;; TWO-CLASS-ACCESSOR
;;; Like above, but two classes. This is common enough to do
;;; specially. There is no cache vector. The two classes are
;;; stored a separate closure variables.
;;;
;;; ONE-INDEX-ACCESSOR
;;; In this case, the accessor generic function has seen more than
;;; one class of argument, but the index of the slot is the same
;;; for all the classes that have been seen. A cache vector is
;;; used to store the wrappers that have been seen, the slot index
;;; is stored directly as a closure variable of the discriminating
;;; function. This case can convert to the next kind.
;;;
;;; N-N-ACCESSOR
;;; This is the most general case. In this case, the accessor
;;; generic function has seen more than one class of argument and
;;; more than one slot index. A cache vector stores the wrappers
;;; and corresponding slot indexes. Because each cache line is
;;; more than one element long, a cache lock count is used.
(defstruct (dfun-info (:constructor nil)
(:copier nil))
(cache nil))
(defstruct (no-methods (:constructor no-methods-dfun-info ())
(:include dfun-info)
(:copier nil)))
(defstruct (initial (:constructor initial-dfun-info ())
(:include dfun-info)
(:copier nil)))
(defstruct (initial-dispatch (:constructor initial-dispatch-dfun-info ())
(:include dfun-info)
(:copier nil)))
(defstruct (dispatch (:constructor dispatch-dfun-info ())
(:include dfun-info)
(:copier nil)))
(defstruct (default-method-only (:constructor default-method-only-dfun-info ())
(:include dfun-info)
(:copier nil)))
;without caching:
; dispatch one-class two-class default-method-only
;with caching:
; one-index n-n checking caching
;accessor:
; one-class two-class one-index n-n
(defstruct (accessor-dfun-info (:constructor nil)
(:include dfun-info)
(:copier nil))
accessor-type) ; (member reader writer)
(defmacro dfun-info-accessor-type (di)
`(accessor-dfun-info-accessor-type ,di))
(defstruct (one-index-dfun-info (:constructor nil)
(:include accessor-dfun-info)
(:copier nil))
index)
(defmacro dfun-info-index (di)
`(one-index-dfun-info-index ,di))
(defstruct (n-n (:constructor n-n-dfun-info (accessor-type cache))
(:include accessor-dfun-info)
(:copier nil)))
(defstruct (one-class (:constructor one-class-dfun-info
(accessor-type index wrapper0))
(:include one-index-dfun-info)
(:copier nil))
wrapper0)
(defmacro dfun-info-wrapper0 (di)
`(one-class-wrapper0 ,di))
(defstruct (two-class (:constructor two-class-dfun-info
(accessor-type index wrapper0 wrapper1))
(:include one-class)
(:copier nil))
wrapper1)
(defmacro dfun-info-wrapper1 (di)
`(two-class-wrapper1 ,di))
(defstruct (one-index (:constructor one-index-dfun-info
(accessor-type index cache))
(:include one-index-dfun-info)
(:copier nil)))
(defstruct (checking (:constructor checking-dfun-info (function cache))
(:include dfun-info)
(:copier nil))
function)
(defmacro dfun-info-function (di)
`(checking-function ,di))
(defstruct (caching (:constructor caching-dfun-info (cache))
(:include dfun-info)
(:copier nil)))
(defstruct (constant-value (:constructor constant-value-dfun-info (cache))
(:include dfun-info)
(:copier nil)))
(defmacro dfun-update (generic-function function &rest args)
`(multiple-value-bind (dfun cache info)
(funcall ,function ,generic-function ,@args)
(update-dfun ,generic-function dfun cache info)))
(defun accessor-miss-function (gf dfun-info)
(ecase (dfun-info-accessor-type dfun-info)
(reader
(lambda (arg)
(accessor-miss gf nil arg dfun-info)))
(writer
(lambda (new arg)
(accessor-miss gf new arg dfun-info)))))
#-sb-fluid (declaim (sb-ext:freeze-type dfun-info))
(defun make-one-class-accessor-dfun (gf type wrapper index)
(let ((emit (if (eq type 'reader) 'emit-one-class-reader 'emit-one-class-writer))
(dfun-info (one-class-dfun-info type index wrapper)))
(values
(funcall (get-dfun-constructor emit (consp index))
wrapper index
(accessor-miss-function gf dfun-info))
nil
dfun-info)))
(defun make-two-class-accessor-dfun (gf type w0 w1 index)
(let ((emit (if (eq type 'reader) 'emit-two-class-reader 'emit-two-class-writer))
(dfun-info (two-class-dfun-info type index w0 w1)))
(values
(funcall (get-dfun-constructor emit (consp index))
w0 w1 index
(accessor-miss-function gf dfun-info))
nil
dfun-info)))
;;; std accessors same index dfun
(defun make-one-index-accessor-dfun (gf type index &optional cache)
(let* ((emit (if (eq type 'reader) 'emit-one-index-readers 'emit-one-index-writers))
(cache (or cache (get-cache 1 nil #'one-index-limit-fn 4)))
(dfun-info (one-index-dfun-info type index cache)))
(declare (type cache cache))
(values
(funcall (get-dfun-constructor emit (consp index))
cache
index
(accessor-miss-function gf dfun-info))
cache
dfun-info)))
(defun make-final-one-index-accessor-dfun (gf type index table)
(let ((cache (fill-dfun-cache table nil 1 #'one-index-limit-fn)))
(make-one-index-accessor-dfun gf type index cache)))
(defun one-index-limit-fn (nlines)
(default-limit-fn nlines))
(defun make-n-n-accessor-dfun (gf type &optional cache)
(let* ((emit (if (eq type 'reader) 'emit-n-n-readers 'emit-n-n-writers))
(cache (or cache (get-cache 1 t #'n-n-accessors-limit-fn 2)))
(dfun-info (n-n-dfun-info type cache)))
(declare (type cache cache))
(values
(funcall (get-dfun-constructor emit)
cache
(accessor-miss-function gf dfun-info))
cache
dfun-info)))
(defun make-final-n-n-accessor-dfun (gf type table)
(let ((cache (fill-dfun-cache table t 1 #'n-n-accessors-limit-fn)))
(make-n-n-accessor-dfun gf type cache)))
(defun n-n-accessors-limit-fn (nlines)
(default-limit-fn nlines))
(defun make-checking-dfun (generic-function function &optional cache)
(unless cache
(when (use-caching-dfun-p generic-function)
(return-from make-checking-dfun (make-caching-dfun generic-function)))
(when (use-dispatch-dfun-p generic-function)
(return-from make-checking-dfun (make-dispatch-dfun generic-function))))
(multiple-value-bind (nreq applyp metatypes nkeys)
(get-generic-function-info generic-function)
(declare (ignore nreq))
(if (every #'(lambda (mt) (eq mt t)) metatypes)
(let ((dfun-info (default-method-only-dfun-info)))
(values
(funcall (get-dfun-constructor 'emit-default-only metatypes applyp)
function)
nil
dfun-info))
(let* ((cache (or cache (get-cache nkeys nil #'checking-limit-fn 2)))
(dfun-info (checking-dfun-info function cache)))
(values
(funcall (get-dfun-constructor 'emit-checking metatypes applyp)
cache
function
(lambda (&rest args)
(checking-miss generic-function args dfun-info)))
cache
dfun-info)))))
(defun make-final-checking-dfun (generic-function function
classes-list new-class)
(let ((metatypes (arg-info-metatypes (gf-arg-info generic-function))))
(if (every #'(lambda (mt) (eq mt t)) metatypes)
(values #'(lambda (&rest args)
(invoke-emf function args))
nil (default-method-only-dfun-info))
(let ((cache (make-final-ordinary-dfun-internal
generic-function nil #'checking-limit-fn
classes-list new-class)))
(make-checking-dfun generic-function function cache)))))
(defun use-default-method-only-dfun-p (generic-function)
(multiple-value-bind (nreq applyp metatypes nkeys)
(get-generic-function-info generic-function)
(declare (ignore nreq applyp nkeys))
(every #'(lambda (mt) (eq mt t)) metatypes)))
(defun use-caching-dfun-p (generic-function)
(some (lambda (method)
(let ((fmf (if (listp method)
(third method)
(method-fast-function method))))
(method-function-get fmf ':slot-name-lists)))
;; KLUDGE: As of sbcl-0.6.4, it's very important for
;; efficiency to know the type of the sequence argument to
;; quantifiers (SOME/NOTANY/etc.) at compile time, but
;; the compiler isn't smart enough to understand the :TYPE
;; slot option for DEFCLASS, so we just tell
;; it the type by hand here.
(the list
(if (early-gf-p generic-function)
(early-gf-methods generic-function)
(generic-function-methods generic-function)))))
(defun checking-limit-fn (nlines)
(default-limit-fn nlines))
(defun make-caching-dfun (generic-function &optional cache)
(unless cache
(when (use-constant-value-dfun-p generic-function)
(return-from make-caching-dfun (make-constant-value-dfun generic-function)))
(when (use-dispatch-dfun-p generic-function)
(return-from make-caching-dfun (make-dispatch-dfun generic-function))))
(multiple-value-bind (nreq applyp metatypes nkeys)
(get-generic-function-info generic-function)
(declare (ignore nreq))
(let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
(dfun-info (caching-dfun-info cache)))
(values
(funcall (get-dfun-constructor 'emit-caching metatypes applyp)
cache
(lambda (&rest args)
(caching-miss generic-function args dfun-info)))
cache
dfun-info))))
(defun make-final-caching-dfun (generic-function classes-list new-class)
(let ((cache (make-final-ordinary-dfun-internal
generic-function t #'caching-limit-fn
classes-list new-class)))
(make-caching-dfun generic-function cache)))
(defun caching-limit-fn (nlines)
(default-limit-fn nlines))
(defun insure-caching-dfun (gf)
(multiple-value-bind (nreq applyp metatypes nkeys)
(get-generic-function-info gf)
(declare (ignore nreq nkeys))
(when (and metatypes
(not (null (car metatypes)))
(dolist (mt metatypes nil)
(unless (eq mt t) (return t))))
(get-dfun-constructor 'emit-caching metatypes applyp))))
(defun use-constant-value-dfun-p (gf &optional boolean-values-p)
(multiple-value-bind (nreq applyp metatypes nkeys)
(get-generic-function-info gf)
(declare (ignore nreq metatypes nkeys))
(let* ((early-p (early-gf-p gf))
(methods (if early-p
(early-gf-methods gf)
(generic-function-methods gf)))
(default '(unknown)))
(and (null applyp)
(or (not (eq *boot-state* 'complete))
(compute-applicable-methods-emf-std-p gf))
(notany #'(lambda (method)
(or (and (eq *boot-state* 'complete)
(some #'eql-specializer-p
(method-specializers method)))
(let ((value (method-function-get
(if early-p
(or (third method) (second method))
(or (method-fast-function method)
(method-function method)))
:constant-value default)))
(if boolean-values-p
(not (or (eq value t) (eq value nil)))
(eq value default)))))
methods)))))
(defun make-constant-value-dfun (generic-function &optional cache)
(multiple-value-bind (nreq applyp metatypes nkeys)
(get-generic-function-info generic-function)
(declare (ignore nreq applyp))
(let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
(dfun-info (constant-value-dfun-info cache)))
(values
(funcall (get-dfun-constructor 'emit-constant-value metatypes)
cache
(lambda (&rest args)
(constant-value-miss generic-function args dfun-info)))
cache
dfun-info))))
(defun make-final-constant-value-dfun (generic-function classes-list new-class)
(let ((cache (make-final-ordinary-dfun-internal
generic-function :constant-value #'caching-limit-fn
classes-list new-class)))
(make-constant-value-dfun generic-function cache)))
(defun use-dispatch-dfun-p (gf &optional (caching-p (use-caching-dfun-p gf)))
(when (eq *boot-state* 'complete)
(unless caching-p
;; This should return T when almost all dispatching is by
;; eql specializers or built-in classes. In other words,
;; return NIL if we might ever need to do more than
;; one (non built-in) typep.
;; Otherwise, it is probably at least as fast to use
;; a caching dfun first, possibly followed by secondary dispatching.
#||;;; Original found in cmu 17f -- S L O W
(< (dispatch-dfun-cost gf) (caching-dfun-cost gf))
||#
;; This uses improved dispatch-dfun-cost below
(let ((cdc (caching-dfun-cost gf))) ; fast
(> cdc (dispatch-dfun-cost gf cdc))))))
(defparameter *non-built-in-typep-cost* 1)
(defparameter *structure-typep-cost* 1)
(defparameter *built-in-typep-cost* 0)
;;; According to comments in the original CMU CL version of PCL,
;;; the cost LIMIT is important to cut off exponential growth for
;;; large numbers of gf methods and argument lists.
(defun dispatch-dfun-cost (gf &optional limit)
(generate-discrimination-net-internal
gf (generic-function-methods gf) nil
#'(lambda (methods known-types)
(declare (ignore methods known-types))
0)
#'(lambda (position type true-value false-value)
(declare (ignore position))
(let* ((type-test-cost
(if (eq 'class (car type))
(let* ((metaclass (class-of (cadr type)))
(mcpl (class-precedence-list metaclass)))
(cond ((memq *the-class-built-in-class* mcpl)
*built-in-typep-cost*)
((memq *the-class-structure-class* mcpl)
*structure-typep-cost*)
(t
*non-built-in-typep-cost*)))
0))
(max-cost-so-far
(+ (max true-value false-value) type-test-cost)))
(when (and limit (<= limit max-cost-so-far))
(return-from dispatch-dfun-cost max-cost-so-far))
max-cost-so-far))
#'identity))
(defparameter *cache-lookup-cost* 1)
(defparameter *wrapper-of-cost* 0)
(defparameter *secondary-dfun-call-cost* 1)
(defun caching-dfun-cost (gf)
(let* ((arg-info (gf-arg-info gf))
(nreq (length (arg-info-metatypes arg-info))))
(+ *cache-lookup-cost*
(* *wrapper-of-cost* nreq)
(if (methods-contain-eql-specializer-p
(generic-function-methods gf))
*secondary-dfun-call-cost*
0))))
(setq *non-built-in-typep-cost* 100)
(setq *structure-typep-cost* 15)
(setq *built-in-typep-cost* 5)
(setq *cache-lookup-cost* 30)
(setq *wrapper-of-cost* 15)
(setq *secondary-dfun-call-cost* 30)
(defun make-dispatch-dfun (gf)
(values (get-dispatch-function gf) nil (dispatch-dfun-info)))
(defun get-dispatch-function (gf)
(let ((methods (generic-function-methods gf)))
(function-funcall (get-secondary-dispatch-function1 gf methods nil nil nil
nil nil t)
nil nil)))
(defun make-final-dispatch-dfun (gf)
(make-dispatch-dfun gf))
(defun update-dispatch-dfuns ()
(dolist (gf (gfs-of-type '(dispatch initial-dispatch)))
(dfun-update gf #'make-dispatch-dfun)))
(defun fill-dfun-cache (table valuep nkeys limit-fn &optional cache)
(let ((cache (or cache (get-cache nkeys valuep limit-fn
(+ (hash-table-count table) 3)))))
(maphash #'(lambda (classes value)
(setq cache (fill-cache cache
(class-wrapper classes)
value
t)))
table)
cache))
(defun make-final-ordinary-dfun-internal (generic-function valuep limit-fn
classes-list new-class)
(let* ((arg-info (gf-arg-info generic-function))
(nkeys (arg-info-nkeys arg-info))
(new-class (and new-class
(equal (type-of (gf-dfun-info generic-function))
(cond ((eq valuep t) 'caching)
((eq valuep :constant-value) 'constant-value)
((null valuep) 'checking)))
new-class))
(cache (if new-class
(copy-cache (gf-dfun-cache generic-function))
(get-cache nkeys (not (null valuep)) limit-fn 4))))
(make-emf-cache generic-function valuep cache classes-list new-class)))
(defvar *dfun-miss-gfs-on-stack* ())
(defmacro dfun-miss ((gf args wrappers invalidp nemf
&optional type index caching-p applicable)
&body body)
(unless applicable (setq applicable (gensym)))
`(multiple-value-bind (,nemf ,applicable ,wrappers ,invalidp
,@(when type `(,type ,index)))
(cache-miss-values ,gf ,args ',(cond (caching-p 'caching)
(type 'accessor)
(t 'checking)))
(when (and ,applicable (not (memq ,gf *dfun-miss-gfs-on-stack*)))
(let ((*dfun-miss-gfs-on-stack* (cons ,gf *dfun-miss-gfs-on-stack*)))
,@body))
(invoke-emf ,nemf ,args)))
;;; The dynamically adaptive method lookup algorithm is implemented is
;;; implemented as a kind of state machine. The kinds of
;;; discriminating function is the state, the various kinds of reasons
;;; for a cache miss are the state transitions.
;;;
;;; The code which implements the transitions is all in the miss
;;; handlers for each kind of dfun. Those appear here.
;;;
;;; Note that within the states that cache, there are dfun updates
;;; which simply select a new cache or cache field. Those are not
;;; considered as state transitions.
(defvar *lazy-dfun-compute-p* t)
(defvar *early-p* nil)
(defun make-initial-dfun (gf)
(let ((initial-dfun
#'(sb-kernel:instance-lambda (&rest args)
(initial-dfun gf args))))
(multiple-value-bind (dfun cache info)
(if (and (eq *boot-state* 'complete)
(compute-applicable-methods-emf-std-p gf))
(let* ((caching-p (use-caching-dfun-p gf))
(classes-list (precompute-effective-methods
gf caching-p
(not *lazy-dfun-compute-p*))))
(if *lazy-dfun-compute-p*
(cond ((use-dispatch-dfun-p gf caching-p)
(values initial-dfun
nil
(initial-dispatch-dfun-info)))
(caching-p
(insure-caching-dfun gf)
(values initial-dfun nil (initial-dfun-info)))
(t
(values initial-dfun nil (initial-dfun-info))))
(make-final-dfun-internal gf classes-list)))
(let ((arg-info (if (early-gf-p gf)
(early-gf-arg-info gf)
(gf-arg-info gf)))
(type nil))
(if (and (gf-precompute-dfun-and-emf-p arg-info)
(setq type (final-accessor-dfun-type gf)))
(if *early-p*
(values (make-early-accessor gf type) nil nil)
(make-final-accessor-dfun gf type))
(values initial-dfun nil (initial-dfun-info)))))
(set-dfun gf dfun cache info))))
(defun make-early-accessor (gf type)
(let* ((methods (early-gf-methods gf))
(slot-name (early-method-standard-accessor-slot-name (car methods))))
(ecase type
(reader #'(sb-kernel:instance-lambda (instance)
(let* ((class (class-of instance))
(class-name (!bootstrap-get-slot 'class class 'name)))
(!bootstrap-get-slot class-name instance slot-name))))
(writer #'(sb-kernel:instance-lambda (new-value instance)
(let* ((class (class-of instance))
(class-name (!bootstrap-get-slot 'class class 'name)))
(!bootstrap-set-slot class-name instance slot-name new-value)))))))
(defun initial-dfun (gf args)
(dfun-miss (gf args wrappers invalidp nemf ntype nindex)
(cond (invalidp)
((and ntype nindex)
(dfun-update
gf #'make-one-class-accessor-dfun ntype wrappers nindex))
((use-caching-dfun-p gf)
(dfun-update gf #'make-caching-dfun))
(t
(dfun-update
gf #'make-checking-dfun
;; nemf is suitable only for caching, have to do this:
(cache-miss-values gf args 'checking))))))
(defun make-final-dfun (gf &optional classes-list)
(multiple-value-bind (dfun cache info)
(make-final-dfun-internal gf classes-list)
(set-dfun gf dfun cache info)))
(defvar *new-class* nil)
(defvar *free-hash-tables* (mapcar #'list '(eq equal eql)))
(defmacro with-hash-table ((table test) &body forms)
`(let* ((.free. (assoc ',test *free-hash-tables*))
(,table (if (cdr .free.)
(pop (cdr .free.))
(make-hash-table :test ',test))))
(multiple-value-prog1
(progn ,@forms)
(clrhash ,table)
(push ,table (cdr .free.)))))
(defmacro with-eq-hash-table ((table) &body forms)
`(with-hash-table (,table eq) ,@forms))
(defun final-accessor-dfun-type (gf)
(let ((methods (if (early-gf-p gf)
(early-gf-methods gf)
(generic-function-methods gf))))
(cond ((every #'(lambda (method)
(if (consp method)
(eq *the-class-standard-reader-method*
(early-method-class method))
(standard-reader-method-p method)))
methods)
'reader)
((every #'(lambda (method)
(if (consp method)
(eq *the-class-standard-writer-method*
(early-method-class method))
(standard-writer-method-p method)))
methods)
'writer))))
(defun make-final-accessor-dfun (gf type &optional classes-list new-class)
(with-eq-hash-table (table)
(multiple-value-bind (table all-index first second size no-class-slots-p)
(make-accessor-table gf type table)
(if table
(cond ((= size 1)
(let ((w (class-wrapper first)))
(make-one-class-accessor-dfun gf type w all-index)))
((and (= size 2) (or (integerp all-index) (consp all-index)))
(let ((w0 (class-wrapper first))
(w1 (class-wrapper second)))
(make-two-class-accessor-dfun gf type w0 w1 all-index)))
((or (integerp all-index) (consp all-index))
(make-final-one-index-accessor-dfun
gf type all-index table))
(no-class-slots-p
(make-final-n-n-accessor-dfun gf type table))
(t
(make-final-caching-dfun gf classes-list new-class)))
(make-final-caching-dfun gf classes-list new-class)))))
(defun make-final-dfun-internal (gf &optional classes-list)
(let ((methods (generic-function-methods gf)) type
(new-class *new-class*) (*new-class* nil)
specls all-same-p)
(cond ((null methods)
(values
#'(sb-kernel:instance-lambda (&rest args)
(apply #'no-applicable-method gf args))
nil
(no-methods-dfun-info)))
((setq type (final-accessor-dfun-type gf))
(make-final-accessor-dfun gf type classes-list new-class))
((and (not (and (every #'(lambda (specl) (eq specl *the-class-t*))
(setq specls
(method-specializers (car methods))))
(setq all-same-p
(every #'(lambda (method)
(and (equal specls
(method-specializers
method))))
methods))))
(use-constant-value-dfun-p gf))
(make-final-constant-value-dfun gf classes-list new-class))
((use-dispatch-dfun-p gf)
(make-final-dispatch-dfun gf))
((and all-same-p (not (use-caching-dfun-p gf)))
(let ((emf (get-secondary-dispatch-function gf methods nil)))
(make-final-checking-dfun gf emf classes-list new-class)))
(t
(make-final-caching-dfun gf classes-list new-class)))))
(defun accessor-miss (gf new object dfun-info)
(let* ((ostate (type-of dfun-info))
(otype (dfun-info-accessor-type dfun-info))
oindex ow0 ow1 cache
(args (ecase otype ; The congruence rules ensure
(reader (list object)) ; that this is safe despite not
(writer (list new object))))) ; knowing the new type yet.
(dfun-miss (gf args wrappers invalidp nemf ntype nindex)
;; The following lexical functions change the state of the
;; dfun to that which is their name. They accept arguments
;; which are the parameters of the new state, and get other
;; information from the lexical variables bound above.
(flet ((two-class (index w0 w1)
(when (zerop (random 2)) (psetf w0 w1 w1 w0))
(dfun-update gf
#'make-two-class-accessor-dfun
ntype
w0
w1
index))
(one-index (index &optional cache)
(dfun-update gf
#'make-one-index-accessor-dfun
ntype
index
cache))
(n-n (&optional cache)
(if (consp nindex)
(dfun-update gf #'make-checking-dfun nemf)
(dfun-update gf #'make-n-n-accessor-dfun ntype cache)))
(caching () ; because cached accessor emfs are much faster
; for accessors
(dfun-update gf #'make-caching-dfun))
(do-fill (update-fn)
(let ((ncache (fill-cache cache wrappers nindex)))
(unless (eq ncache cache)
(funcall update-fn ncache)))))
(cond ((null ntype)
(caching))
((or invalidp
(null nindex)))
((not (pcl-instance-p object))
(caching))
((or (neq ntype otype) (listp wrappers))
(caching))
(t
(ecase ostate
(one-class
(setq oindex (dfun-info-index dfun-info))
(setq ow0 (dfun-info-wrapper0 dfun-info))
(unless (eq ow0 wrappers)
(if (eql nindex oindex)
(two-class nindex ow0 wrappers)
(n-n))))
(two-class
(setq oindex (dfun-info-index dfun-info))
(setq ow0 (dfun-info-wrapper0 dfun-info))
(setq ow1 (dfun-info-wrapper1 dfun-info))
(unless (or (eq ow0 wrappers) (eq ow1 wrappers))
(if (eql nindex oindex)
(one-index nindex)
(n-n))))
(one-index
(setq oindex (dfun-info-index dfun-info))
(setq cache (dfun-info-cache dfun-info))
(if (eql nindex oindex)
(do-fill #'(lambda (ncache)
(one-index nindex ncache)))
(n-n)))
(n-n
(setq cache (dfun-info-cache dfun-info))
(if (consp nindex)
(caching)
(do-fill #'n-n))))))))))
(defun checking-miss (generic-function args dfun-info)
(let ((oemf (dfun-info-function dfun-info))
(cache (dfun-info-cache dfun-info)))
(dfun-miss (generic-function args wrappers invalidp nemf)
(cond (invalidp)
((eq oemf nemf)
(let ((ncache (fill-cache cache wrappers nil)))
(unless (eq ncache cache)
(dfun-update generic-function #'make-checking-dfun
nemf ncache))))
(t
(dfun-update generic-function #'make-caching-dfun))))))
(defun caching-miss (generic-function args dfun-info)
(let ((ocache (dfun-info-cache dfun-info)))
(dfun-miss (generic-function args wrappers invalidp emf nil nil t)
(cond (invalidp)
(t
(let ((ncache (fill-cache ocache wrappers emf)))
(unless (eq ncache ocache)
(dfun-update generic-function
#'make-caching-dfun ncache))))))))
(defun constant-value-miss (generic-function args dfun-info)
(let ((ocache (dfun-info-cache dfun-info)))
(dfun-miss (generic-function args wrappers invalidp emf nil nil t)
(cond (invalidp)
(t
(let* ((function (typecase emf
(fast-method-call (fast-method-call-function
emf))
(method-call (method-call-function emf))))
(value (method-function-get function :constant-value))
(ncache (fill-cache ocache wrappers value)))
(unless (eq ncache ocache)
(dfun-update generic-function
#'make-constant-value-dfun ncache))))))))
;;; Given a generic function and a set of arguments to that generic
;;; function, return a mess of values.
;;;
;;; <function> The compiled effective method function for this set of
;;; arguments.
;;;
;;; <applicable> Sorted list of applicable methods.
;;;
;;; <wrappers> Is a single wrapper if the generic function has only
;;; one key, that is arg-info-nkeys of the arg-info is 1.
;;; Otherwise a list of the wrappers of the specialized
;;; arguments to the generic function.
;;;
;;; Note that all these wrappers are valid. This function
;;; does invalid wrapper traps when it finds an invalid
;;; wrapper and then returns the new, valid wrapper.
;;;
;;; <invalidp> True if any of the specialized arguments had an invalid
;;; wrapper, false otherwise.
;;;
;;; <type> READER or WRITER when the only method that would be run
;;; is a standard reader or writer method. To be specific,
;;; the value is READER when the method combination is eq to
;;; *standard-method-combination*; there are no applicable
;;; :before, :after or :around methods; and the most specific
;;; primary method is a standard reader method.
;;;
;;; <index> If <type> is READER or WRITER, and the slot accessed is
;;; an :instance slot, this is the index number of that slot
;;; in the object argument.
(defun cache-miss-values (gf args state)
(multiple-value-bind (nreq applyp metatypes nkeys arg-info)
(get-generic-function-info gf)
(declare (ignore nreq applyp nkeys))
(with-dfun-wrappers (args metatypes)
(dfun-wrappers invalid-wrapper-p wrappers classes types)
(error-need-at-least-n-args gf (length metatypes))
(multiple-value-bind (emf methods accessor-type index)
(cache-miss-values-internal
gf arg-info wrappers classes types state)
(values emf methods
dfun-wrappers
invalid-wrapper-p
accessor-type index)))))
(defun cache-miss-values-internal (gf arg-info wrappers classes types state)
(let* ((for-accessor-p (eq state 'accessor))
(for-cache-p (or (eq state 'caching) (eq state 'accessor)))
(cam-std-p (or (null arg-info)
(gf-info-c-a-m-emf-std-p arg-info))))
(multiple-value-bind (methods all-applicable-and-sorted-p)
(if cam-std-p
(compute-applicable-methods-using-types gf types)
(compute-applicable-methods-using-classes gf classes))
(let ((emf (if (or cam-std-p all-applicable-and-sorted-p)
(function-funcall (get-secondary-dispatch-function1
gf methods types nil (and for-cache-p
wrappers)
all-applicable-and-sorted-p)
nil (and for-cache-p wrappers))
(default-secondary-dispatch-function gf))))
(multiple-value-bind (index accessor-type)
(and for-accessor-p all-applicable-and-sorted-p methods
(accessor-values gf arg-info classes methods))
(values (if (integerp index) index emf)
methods accessor-type index))))))