@@ -11,8 +11,9 @@ module Unison.Codebase.Runtime.Profile
11
11
aggregatePruned ,
12
12
fullProfile ,
13
13
miniProfile ,
14
- foldedProfile
15
- ) where
14
+ foldedProfile ,
15
+ )
16
+ where
16
17
17
18
import Data.Bifunctor (second )
18
19
import Data.Functor.Identity (Identity (.. ))
@@ -21,9 +22,7 @@ import Data.Map.Strict qualified as M
21
22
import Data.Set (Set )
22
23
import Data.Set qualified as S
23
24
import Data.String
24
-
25
25
import Numeric
26
-
27
26
import Unison.PrettyPrintEnv
28
27
import Unison.Reference
29
28
import Unison.Referent
@@ -43,7 +42,7 @@ newtype ProfTrie k a = ProfT (Map k (a, ProfTrie k a))
43
42
data Profile k = Prof ! Int ! (ProfTrie k Int ) ! (Map k Reference )
44
43
45
44
-- Abstracts over the exact key type used in a profile.
46
- data SomeProfile = forall k . Ord k => SomeProf (Profile k )
45
+ data SomeProfile = forall k . ( Ord k ) => SomeProf (Profile k )
47
46
48
47
data ProfileSpec = NoProf | MiniProf | FullProf String
49
48
deriving (Eq , Ord , Show )
@@ -52,42 +51,42 @@ emptyProfile :: Profile k
52
51
emptyProfile = Prof 0 (ProfT M. empty) M. empty
53
52
54
53
-- Creates a singleton profile trie from a path.
55
- singlePath :: Ord k => [k ] -> (Int , ProfTrie k Int )
54
+ singlePath :: ( Ord k ) => [k ] -> (Int , ProfTrie k Int )
56
55
singlePath [] = (1 , ProfT M. empty)
57
- singlePath (i: is) = (0 ,) . ProfT $! M. singleton i (singlePath is)
56
+ singlePath (i : is) = (0 ,) . ProfT $! M. singleton i (singlePath is)
58
57
59
- addPath0 :: Ord k => [k ] -> (Int , ProfTrie k Int ) -> (Int , ProfTrie k Int )
60
- addPath0 [] (m, p) = (,p) $! m+ 1
61
- addPath0 (i: is) (m, ProfT p) = (m,) . ProfT $! M. alter f i p
58
+ addPath0 :: ( Ord k ) => [k ] -> (Int , ProfTrie k Int ) -> (Int , ProfTrie k Int )
59
+ addPath0 [] (m, p) = (,p) $! m + 1
60
+ addPath0 (i : is) (m, ProfT p) = (m,) . ProfT $! M. alter f i p
62
61
where
63
62
f Nothing = Just $ singlePath is
64
63
f (Just q) = Just $ addPath0 is q
65
64
66
65
-- Adds a path to a profile trie, incrementing the count for the given
67
66
-- path.
68
- addPath :: Ord k => [k ] -> ProfTrie k Int -> ProfTrie k Int
67
+ addPath :: ( Ord k ) => [k ] -> ProfTrie k Int -> ProfTrie k Int
69
68
addPath [] p = p
70
- addPath (i: is) (ProfT m) = ProfT $ M. alter f i m
69
+ addPath (i : is) (ProfT m) = ProfT $ M. alter f i m
71
70
where
72
71
f Nothing = Just $ singlePath is
73
72
f (Just q) = Just $ addPath0 is q
74
73
75
- data AggInfo k = Ag {
76
- -- inherited sample count
74
+ data AggInfo k = Ag
75
+ { -- inherited sample count
77
76
inherited :: Int ,
78
77
-- total sample for all occurrences of a key
79
78
allOccs :: Map k Int
80
79
}
81
80
82
- instance Ord k => Semigroup (AggInfo k ) where
81
+ instance ( Ord k ) => Semigroup (AggInfo k ) where
83
82
Ag il al <> Ag ir ar =
84
83
Ag (il + ir) (M. unionWith (+) al ar)
85
84
86
- instance Ord k => Monoid (AggInfo k ) where
85
+ instance ( Ord k ) => Monoid (AggInfo k ) where
87
86
mempty = Ag 0 M. empty
88
87
89
88
aggregateWith ::
90
- Ord k =>
89
+ ( Ord k ) =>
91
90
(AggInfo k -> Int -> r ) ->
92
91
k ->
93
92
(Int , ProfTrie k Int ) ->
@@ -96,10 +95,10 @@ aggregateWith f k (m, ProfT t) =
96
95
case M. traverseWithKey (aggregateWith f) t of
97
96
(ag, t) -> (ag', (f ag' m, ProfT t))
98
97
where
99
- ag' = ag <> Ag m (M. singleton k m)
98
+ ag' = ag <> Ag m (M. singleton k m)
100
99
101
100
prune0 ::
102
- Ord k =>
101
+ ( Ord k ) =>
103
102
Set k ->
104
103
k ->
105
104
(a , ProfTrie k a ) ->
@@ -110,22 +109,22 @@ prune0 keep k (a, ProfT sub) =
110
109
| null sub, k `S.notMember` keep -> pure Nothing
111
110
| otherwise -> pure $ Just (a, ProfT sub)
112
111
113
- prune :: Ord k => Set k -> ProfTrie k a -> ProfTrie k a
112
+ prune :: ( Ord k ) => Set k -> ProfTrie k a -> ProfTrie k a
114
113
prune keep (ProfT m) = case M. traverseMaybeWithKey (prune0 keep) m of
115
114
Identity sub -> ProfT sub
116
115
117
- topN :: Ord k => Int -> Map k Int -> [(k , Int )]
116
+ topN :: ( Ord k ) => Int -> Map k Int -> [(k , Int )]
118
117
topN n0 = M. foldlWithKey (ins n0) []
119
118
where
120
119
ins 0 _ _ _ = []
121
120
ins _ [] k i = [(k, i)]
122
121
ins n pss@ ((k1, j) : ps) k0 i
123
- | i > j = (k0, i) : pop (n- 1 ) pss
124
- | otherwise = (k1, j) : ins (n- 1 ) ps k0 i
122
+ | i > j = (k0, i) : pop (n - 1 ) pss
123
+ | otherwise = (k1, j) : ins (n - 1 ) ps k0 i
125
124
126
125
pop 0 _ = []
127
126
pop _ [] = []
128
- pop n (p: ps) = p : pop (n- 1 ) ps
127
+ pop n (p : ps) = p : pop (n - 1 ) ps
129
128
130
129
fraction :: Int -> Int -> Double
131
130
fraction n d = fromIntegral n / fromIntegral d
@@ -141,15 +140,15 @@ topNum = 25
141
140
-- cost fractions of the positions in the trie, and prunes it to the
142
141
-- hottest spots.
143
142
aggregatePruned ::
144
- Ord k => Int -> ProfTrie k Int -> ProfTrie k (Double , Double )
143
+ ( Ord k ) => Int -> ProfTrie k Int -> ProfTrie k (Double , Double )
145
144
aggregatePruned total (ProfT t) =
146
145
case M. traverseWithKey (aggregateWith (fractions total)) t of
147
146
(ag, t)
148
147
| top <- topN topNum (allOccs ag) ->
149
148
prune (S. fromList $ fst <$> top) $ ProfT t
150
149
151
150
aggregate ::
152
- Ord k =>
151
+ ( Ord k ) =>
153
152
Int ->
154
153
ProfTrie k Int ->
155
154
([(k , Double )], ProfTrie k (Double , Double ))
@@ -163,12 +162,12 @@ aggregate total (ProfT t) =
163
162
-- Folds over a profile trie. The mapping function receives a reversed
164
163
-- path to the node, which can be used e.g. to see the node's key and to
165
164
-- calculate the depth in the trie.
166
- foldMapTrie :: Monoid m => ((k ,[k ]) -> v -> m ) -> ProfTrie k v -> m
165
+ foldMapTrie :: ( Monoid m ) => ((k , [k ]) -> v -> m ) -> ProfTrie k v -> m
167
166
foldMapTrie f = descend []
168
167
where
169
168
descend ks (ProfT m) =
170
169
M. foldMapWithKey
171
- (\ k (v, sub) -> f (k,ks) v <> descend (k: ks) sub)
170
+ (\ k (v, sub) -> f (k, ks) v <> descend (k : ks) sub)
172
171
m
173
172
174
173
showPercent :: Double -> String
@@ -177,17 +176,17 @@ showPercent d = pad $ showFFloat (Just 2) (100 * d) "%"
177
176
pad s = replicate (7 - length s) ' ' <> s
178
177
179
178
dispProfEntry ::
180
- Ord k =>
179
+ ( Ord k ) =>
181
180
PrettyPrintEnv ->
182
181
Map k Reference ->
183
- (k ,[k ]) ->
182
+ (k , [k ]) ->
184
183
(Double , Double ) ->
185
184
Pretty ColorText
186
- dispProfEntry ppe refs (k,ks) (inh, self) =
185
+ dispProfEntry ppe refs (k, ks) (inh, self) =
187
186
mconcat
188
187
[ P. indentN 2 . fromString $ showPercent inh,
189
188
P. indentN 4 . fromString $ showPercent self,
190
- P. indentN (2 * ind + 4 ) $ dispKey ppe refs k,
189
+ P. indentN (2 * ind + 4 ) $ dispKey ppe refs k,
191
190
" \n "
192
191
]
193
192
where
@@ -198,21 +197,21 @@ dispFunc ppe =
198
197
syntaxToColor . prettyHashQualified . termName ppe . Ref
199
198
200
199
dispKey ::
201
- Ord k => PrettyPrintEnv -> Map k Reference -> k -> Pretty ColorText
200
+ ( Ord k ) => PrettyPrintEnv -> Map k Reference -> k -> Pretty ColorText
202
201
dispKey ppe refs k
203
202
| Just r <- M. lookup k refs = dispFunc ppe r
204
203
| otherwise = " <unknown>"
205
204
206
205
dispProfTrie ::
207
- Ord k =>
206
+ ( Ord k ) =>
208
207
PrettyPrintEnv ->
209
208
Map k Reference ->
210
209
ProfTrie k (Double , Double ) ->
211
210
Pretty ColorText
212
211
dispProfTrie ppe refs ag = foldMapTrie (dispProfEntry ppe refs) ag
213
212
214
213
dispTopEntry ::
215
- Ord k =>
214
+ ( Ord k ) =>
216
215
PrettyPrintEnv ->
217
216
Map k Reference ->
218
217
(k , Double ) ->
@@ -225,11 +224,12 @@ dispTopEntry ppe refs (k, frac) =
225
224
]
226
225
where
227
226
dr :: Pretty ColorText
228
- dr | Just r <- M. lookup k refs = dispFunc ppe r
229
- | otherwise = " <unknown>"
227
+ dr
228
+ | Just r <- M. lookup k refs = dispFunc ppe r
229
+ | otherwise = " <unknown>"
230
230
231
231
dispTop ::
232
- Ord k =>
232
+ ( Ord k ) =>
233
233
PrettyPrintEnv ->
234
234
Map k Reference ->
235
235
[(k , Double )] ->
@@ -243,34 +243,34 @@ profileTopHeader =
243
243
profileTreeHeader :: Pretty ColorText
244
244
profileTreeHeader =
245
245
P. lines
246
- [ P. indentN 9 " Costs"
247
- , " Inherited Local Function Call Tree"
248
- , " "
246
+ [ P. indentN 9 " Costs" ,
247
+ " Inherited Local Function Call Tree" ,
248
+ " "
249
249
]
250
250
251
- miniProfile :: Ord k => PrettyPrintEnv -> Profile k -> Pretty ColorText
251
+ miniProfile :: ( Ord k ) => PrettyPrintEnv -> Profile k -> Pretty ColorText
252
252
miniProfile ppe (Prof total tr refs) =
253
- profileTreeHeader <>
254
- dispProfTrie ppe refs ag
253
+ profileTreeHeader
254
+ <> dispProfTrie ppe refs ag
255
255
where
256
256
ag = aggregatePruned total tr
257
257
258
258
fullProfile ::
259
- Ord k =>
259
+ ( Ord k ) =>
260
260
PrettyPrintEnv ->
261
261
Profile k ->
262
262
Pretty ColorText
263
263
fullProfile ppe (Prof total tr refs) =
264
- profileTopHeader <>
265
- dispTop ppe refs top <>
266
- " \n\n " <>
267
- profileTreeHeader <>
268
- dispProfTrie ppe refs ag
264
+ profileTopHeader
265
+ <> dispTop ppe refs top
266
+ <> " \n\n "
267
+ <> profileTreeHeader
268
+ <> dispProfTrie ppe refs ag
269
269
where
270
270
(top, ag) = aggregate total tr
271
271
272
272
foldedProfile ::
273
- Ord k =>
273
+ ( Ord k ) =>
274
274
PrettyPrintEnv ->
275
275
Profile k ->
276
276
String
@@ -279,10 +279,10 @@ foldedProfile ppe (Prof _ tr refs) =
279
279
where
280
280
dk = dispKey ppe refs
281
281
282
- f (k,ks) n =
282
+ f (k, ks) n =
283
283
mconcat
284
- [ foldl (\ tx k -> dk k <> " ;" <> tx) (dk k) ks
285
- , " "
286
- , fromString $ show n
287
- , " \n "
284
+ [ foldl (\ tx k -> dk k <> " ;" <> tx) (dk k) ks,
285
+ " " ,
286
+ fromString $ show n,
287
+ " \n "
288
288
]
0 commit comments