1 ! Copyright (C) 2007, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors words sequences math prettyprint kernel arrays io
4 io.styles namespaces assocs kernel.private strings combinators
5 sorting math.parser vocabs definitions tools.profiler.private
6 continuations generic compiler.units sets ;
10 [ t profiling call ] [ f profiling ] [ ] cleanup ;
12 : counters ( words -- assoc )
13 [ dup counter>> ] { } map>assoc ;
15 GENERIC: (profile.) ( obj -- )
17 TUPLE: usage-profile word ;
19 C: <usage-profile> usage-profile
22 [ name>> "( no name )" or ] [ <usage-profile> ] bi write-object ;
24 TUPLE: vocab-profile vocab ;
26 C: <vocab-profile> vocab-profile
29 dup <vocab-profile> write-object ;
31 M: method-body (profile.)
32 [ synopsis ] [ "method-generic" word-prop <usage-profile> ] bi
35 : counter. ( obj n -- )
37 [ [ (profile.) ] with-cell ] dip
38 [ number>string write ] with-cell
41 : counters. ( assoc -- )
42 [ second 0 > ] filter sort-values
43 standard-table-style [
44 [ counter. ] assoc-each
48 "Call counts for all words:" print
49 all-words counters counters. ;
51 : vocab-profile. ( vocab -- )
52 "Call counts for words in the " write
53 dup dup vocab write-object
55 words counters counters. ;
57 : usage-profile. ( word -- )
58 "Call counts for words which call " write
61 [ smart-usage [ word? ] filter ]
62 [ compiled-generic-usage keys ]
63 [ compiled-usage keys ]
64 tri 3append prune counters counters. ;
66 : vocabs-profile. ( -- )
67 "Call counts for all vocabularies:" print
70 [ "predicating" word-prop not ] filter
72 ] { } map>assoc counters. ;
74 : method-profile. ( -- )
75 all-words [ subwords ] map concat