| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; CMPOPT-CLOS. Optimization of CLOS related operations |
| 6 |
|
| 7 |
;;;; Copyright (c) 2010. Juan Jose Garcia-Ripol |
| 8 |
;;;; |
| 9 |
;;;; See file 'LICENSE' for the copyright details. |
| 10 |
|
| 11 |
(in-package "COMPILER") |
| 12 |
|
| 13 |
(defun clos-compiler-macro-expand (fname args) |
| 14 |
(when (and (si::valid-function-name-p fname) |
| 15 |
(fboundp fname)) |
| 16 |
(let ((function (fdefinition fname))) |
| 17 |
(when (typep function 'generic-function *cmp-env*) |
| 18 |
(generic-function-macro-expand function (list* fname args)))))) |
| 19 |
|
| 20 |
(defmethod generic-function-macro-expand ((g standard-generic-function) whole) |
| 21 |
(let* ((output (optimizable-slot-accessor g whole)) |
| 22 |
(success (and output t))) |
| 23 |
(values output success))) |
| 24 |
|
| 25 |
(defun optimizable-slot-reader (method whole) |
| 26 |
(declare (si::c-local)) |
| 27 |
(when (typep method 'clos:standard-reader-method *cmp-env*) |
| 28 |
(let ((class (first (clos:method-specializers method)))) |
| 29 |
(when (clos::class-sealedp class) |
| 30 |
(let* ((slotd (clos:accessor-method-slot-definition method)) |
| 31 |
(location (clos:slot-definition-location slotd))) |
| 32 |
(let ((object (gentemp))) |
| 33 |
(cmpnote "Inlining read access to slot ~a from class ~a" |
| 34 |
(clos:slot-definition-name slotd) |
| 35 |
(class-name class)) |
| 36 |
#+(or) |
| 37 |
`(let ((,object ,(second whole))) |
| 38 |
(locally (declare (notinline ,(first whole))) |
| 39 |
(if (typep ,object ',(class-name class)) |
| 40 |
(si::instance-ref ,object ,location) |
| 41 |
(,(first whole) ,object)))) |
| 42 |
;(format t "~&;;; Inlining accessor ~a" (first whole)) |
| 43 |
`(let ((,object ,(second whole))) |
| 44 |
(optional-type-check ,object ',class) |
| 45 |
(locally (declare (optimize speed (safety 0))) |
| 46 |
(si::instance-ref ,object ,location))))))))) |
| 47 |
|
| 48 |
(defun optimizable-slot-writer (method whole) |
| 49 |
(declare (si::c-local)) |
| 50 |
(when (typep method 'clos:standard-writer-method *cmp-env*) |
| 51 |
(let ((class (second (clos:method-specializers method)))) |
| 52 |
(when (clos::class-sealedp class) |
| 53 |
(let* ((slotd (clos:accessor-method-slot-definition method)) |
| 54 |
(location (clos:slot-definition-location slotd))) |
| 55 |
(let* ((object (gentemp)) |
| 56 |
(value (gentemp))) |
| 57 |
(cmpnote "Inlining write access to slot ~a from class ~a" |
| 58 |
(clos:slot-definition-name slotd) |
| 59 |
(class-name class)) |
| 60 |
#+(or) |
| 61 |
`(let ((,value ,(second whole)) |
| 62 |
(,object ,(third whole))) |
| 63 |
(locally (declare (notinline ,(first whole))) |
| 64 |
(if (typep ,object ',(class-name class)) |
| 65 |
(si::instance-set ,object ,location ,value) |
| 66 |
(funcall #',(first whole) ,value ,object)))) |
| 67 |
;(format t "~&;;; Inlining accessor ~a" (first whole)) |
| 68 |
`(let ((,value ,(second whole)) |
| 69 |
(,object ,(third whole))) |
| 70 |
(optional-type-check ,object ',class) |
| 71 |
(locally (declare (optimize speed (safety 0))) |
| 72 |
(si::instance-set ,object ,location ,value))))))))) |
| 73 |
|
| 74 |
(defun optimizable-slot-accessor (g whole) |
| 75 |
(declare (si::c-local)) |
| 76 |
(and (policy-inline-slot-access) |
| 77 |
(let ((methods (clos:generic-function-methods g))) |
| 78 |
(and methods |
| 79 |
(null (rest methods)) |
| 80 |
(let* ((principal (first methods))) |
| 81 |
(or (optimizable-slot-reader principal whole) |
| 82 |
(optimizable-slot-writer principal whole))))))) |