Skip to content

Commit ae336e4

Browse files
authored
Merge pull request #5856 from unisonweb/topic/profiling
2 parents e1b98c8 + 8b62f3c commit ae336e4

File tree

22 files changed

+979
-193
lines changed

22 files changed

+979
-193
lines changed

contrib/cabal.project

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,7 @@ program-options
8383

8484
-- These options are applied to all packages, local ones and also external dependencies.
8585
package *
86-
ghc-options: -haddock
86+
-- -fno-omit-yields allows non-allocating loops to be preempted by the
87+
-- GHC scheduler. Building all dependencies with it minimizes
88+
-- uninterruptible loops.
89+
ghc-options: -haddock -fno-omit-yields

parser-typechecker/src/Unison/Codebase/Runtime.hs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Unison.ABT qualified as ABT
99
import Unison.Builtin.Decls (tupleTerm, pattern TupleTerm')
1010
import Unison.Codebase.CodeLookup qualified as CL
1111
import Unison.Codebase.CodeLookup.Util qualified as CL
12+
import Unison.Codebase.Runtime.Profile
1213
import Unison.Hashing.V2.Convert qualified as Hashing
1314
import Unison.Parser.Ann (Ann)
1415
import Unison.Prelude
@@ -27,6 +28,22 @@ import Unison.WatchKind qualified as WK
2728

2829
type Error = P.Pretty P.ColorText
2930

31+
data Response
32+
= DecompErrs [Error]
33+
| Profile (P.Pretty P.ColorText)
34+
| EmptyResponse
35+
36+
instance Semigroup Response where
37+
DecompErrs l <> DecompErrs r = DecompErrs (l <> r)
38+
d@(DecompErrs _) <> _ = d
39+
_ <> d@(DecompErrs _) = d
40+
p@(Profile _) <> _ = p
41+
_ <> p@(Profile _) = p
42+
EmptyResponse <> r = r
43+
44+
instance Monoid Response where
45+
mempty = EmptyResponse
46+
3047
type Term v = Term.Term v ()
3148

3249
data CompileOpts = COpts
@@ -41,8 +58,9 @@ data Runtime v = Runtime
4158
evaluate ::
4259
CL.CodeLookup v IO () ->
4360
PPE.PrettyPrintEnv ->
61+
ProfileSpec ->
4462
Term v ->
45-
IO (Either Error ([Error], Term v)),
63+
IO (Either Error (Response, Term v)),
4664
compileTo ::
4765
CompileOpts ->
4866
CL.CodeLookup v IO () ->
@@ -65,7 +83,7 @@ type WatchResults v a =
6583
-- Bindings:
6684
( [(v, Term v)],
6785
-- Map watchName (loc, hash, expression, value, isHit)
68-
[Error],
86+
Response,
6987
Map v (a, WatchKind, Reference.Id, Term v, Term v, IsCacheHit)
7088
)
7189
)
@@ -83,11 +101,12 @@ evaluateWatches ::
83101
(Var v) =>
84102
CL.CodeLookup v IO a ->
85103
PPE.PrettyPrintEnv ->
104+
ProfileSpec ->
86105
(Reference.Id -> IO (Maybe (Term v))) ->
87106
Runtime v ->
88107
TypecheckedUnisonFile v a ->
89108
IO (WatchResults v a)
90-
evaluateWatches code ppe evaluationCache rt tuf = do
109+
evaluateWatches code ppe prof evaluationCache rt tuf = do
91110
-- 1. compute hashes for everything in the file
92111
let m :: Map v (Reference.Id, Term.Term v a)
93112
m = fmap (\(_a, id, _wk, tm, _tp) -> (id, tm)) (UF.hashTermsId tuf)
@@ -113,7 +132,7 @@ evaluateWatches code ppe evaluationCache rt tuf = do
113132
cl = void (CL.fromTypecheckedUnisonFile tuf) <> void code
114133
-- 4. evaluate it and get all the results out of the tuple, then
115134
-- create the result Map
116-
out <- evaluate rt cl ppe bigOl'LetRec
135+
out <- evaluate rt cl ppe prof bigOl'LetRec
117136
case out of
118137
Right (errs, out) -> do
119138
let (bindings, results) = case out of
@@ -150,21 +169,22 @@ evaluateTerm' ::
150169
CL.CodeLookup v IO a ->
151170
(Reference.Id -> IO (Maybe (Term v))) ->
152171
PPE.PrettyPrintEnv ->
172+
ProfileSpec ->
153173
Runtime v ->
154174
Term.Term v a ->
155-
IO (Either Error ([Error], Term v))
156-
evaluateTerm' codeLookup cache ppe rt tm = do
175+
IO (Either Error (Response, Term v))
176+
evaluateTerm' codeLookup cache ppe prof rt tm = do
157177
result <- cache (Hashing.hashClosedTerm tm)
158178
case result of
159-
Just r -> pure (Right ([], r))
179+
Just r -> pure (Right (EmptyResponse, r))
160180
Nothing -> do
161181
let tuf =
162182
UF.typecheckedUnisonFile
163183
mempty
164184
mempty
165185
mempty
166186
[(WK.RegularWatch, [(Var.nameds "result", mempty, tm, mempty <$> mainType rt)])]
167-
r <- evaluateWatches (void codeLookup) ppe cache rt (void tuf)
187+
r <- evaluateWatches (void codeLookup) ppe prof cache rt (void tuf)
168188
pure $
169189
r <&> \(_, errs, map) ->
170190
case Map.elems map of
@@ -175,7 +195,8 @@ evaluateTerm ::
175195
(Var v, Monoid a) =>
176196
CL.CodeLookup v IO a ->
177197
PPE.PrettyPrintEnv ->
198+
ProfileSpec ->
178199
Runtime v ->
179200
Term.Term v a ->
180-
IO (Either Error ([Error], Term v))
201+
IO (Either Error (Response, Term v))
181202
evaluateTerm codeLookup = evaluateTerm' codeLookup noCache

0 commit comments

Comments
 (0)