1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math sorting words parser io summary
4 quotations sequences prettyprint continuations effects
5 definitions compiler.units namespaces assocs tools.time generic
6 inspector fry locals generalizations macros ;
11 GENERIC: (reset) ( word -- )
14 subwords [ (reset) ] each ;
17 dup "unannotated-def" word-prop [
18 dup dup "unannotated-def" word-prop define
19 f "unannotated-def" set-word-prop
25 [ (reset) ] with-compilation-unit ;
27 ERROR: cannot-annotate-twice word ;
29 M: cannot-annotate-twice summary drop "Cannot annotate a word twice" ;
33 : check-annotate-twice ( word -- word )
34 dup "unannotated-def" word-prop [
38 GENERIC# (annotate) 1 ( word quot -- )
41 [ "methods" word-prop values ] dip '[ _ (annotate) ] each ;
44 [ check-annotate-twice ] dip
45 [ dup def>> 2dup "unannotated-def" set-word-prop ] dip
46 call( old -- new ) define ;
50 : annotate ( word quot -- )
51 [ (annotate) ] with-compilation-unit ;
55 :: trace-quot ( word effect quot str -- quot' )
56 effect quot call :> values
59 "--- " write str write bl word .
60 n ndup n narray values swap zip simple-table.
64 MACRO: entering ( word -- quot )
65 dup stack-effect [ in>> ] "Entering" trace-quot ;
67 MACRO: leaving ( word -- quot )
68 dup stack-effect [ out>> ] "Leaving" trace-quot ;
70 : (watch) ( word def -- def )
71 over '[ _ entering @ _ leaving ] ;
76 dup '[ [ _ ] dip (watch) ] annotate ;
80 : (watch-vars) ( word vars quot -- newquot )
82 "--- Entering: " write _ .
83 "--- Variable values:" print _ [ dup get ] H{ } map>assoc describe
89 : watch-vars ( word vars -- )
90 dupd '[ [ _ _ ] dip (watch-vars) ] annotate ;
94 word-timing [ H{ } clone ] initialize
96 : reset-word-timing ( -- )
97 word-timing get clear-assoc ;
101 : (add-timing) ( def word -- def' )
102 '[ _ benchmark _ word-timing get at+ ] ;
106 : add-timing ( word -- )
107 dup '[ _ (add-timing) ] annotate ;
109 : word-timing. ( -- )
111 >alist [ 1000000 /f ] assoc-map sort-values