From 6d96c913573f93f88ac40bf821ce715b65d60653 Mon Sep 17 00:00:00 2001 From: TIHan Date: Fri, 1 Nov 2019 19:01:33 -0700 Subject: [PATCH] Use cache value if available early in a let binding. Stops from having to traverse entire expr. --- src/fsharp/TastOps.fs | 19 +++++++++++++------ src/fsharp/lib.fs | 4 ++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/fsharp/TastOps.fs b/src/fsharp/TastOps.fs index 5ea0d49b5..94d0bcffa 100644 --- a/src/fsharp/TastOps.fs +++ b/src/fsharp/TastOps.fs @@ -4388,6 +4388,10 @@ let bindLhs opts (bind: Binding) fvs = boundLocalVal opts bind.Var fvs let freeVarsCacheCompute opts cache f = if opts.canCache then cached cache f else f() +let tryGetFreeVarsCacheValue opts cache = + if opts.canCache then tryGetCacheValue cache + else ValueNone + let rec accBindRhs opts (TBind(_, repr, _)) acc = accFreeInExpr opts repr acc and accFreeInSwitchCases opts csl dflt (acc: FreeVars) = @@ -4485,13 +4489,16 @@ and accFreeInExpr (opts: FreeVarOptions) x acc = and accFreeInExprLinear (opts: FreeVarOptions) x acc contf = // for nested let-bindings, we need to continue after the whole let-binding is processed match x with - | Expr.Let (bind, e, _, cache) -> - let contf = contf << (fun free -> - unionFreeVars (freeVarsCacheCompute opts cache (fun () -> bindLhs opts bind (accBindRhs opts bind free))) acc ) - accFreeInExprLinear opts e emptyFreeVars contf + | Expr.Let (bind, e, _, cache) -> + match tryGetFreeVarsCacheValue opts cache with + | ValueSome free -> contf (unionFreeVars free acc) + | _ -> + accFreeInExprLinear opts e emptyFreeVars (contf << (fun free -> + unionFreeVars (freeVarsCacheCompute opts cache (fun () -> bindLhs opts bind (accBindRhs opts bind free))) acc + )) | _ -> - // No longer linear expr - accFreeInExpr opts x acc |> contf + // No longer linear expr + contf (accFreeInExpr opts x acc) and accFreeInExprNonLinear opts x acc = match x with diff --git a/src/fsharp/lib.fs b/src/fsharp/lib.fs index 8d5fa3f0a..7ed3521ca 100755 --- a/src/fsharp/lib.fs +++ b/src/fsharp/lib.fs @@ -394,6 +394,10 @@ let inline cacheOptRef cache f = cache := Some res res +let inline tryGetCacheValue cache = + match box cache.cacheVal with + | null -> ValueNone + | _ -> ValueSome cache.cacheVal #if DUMPER type Dumper(x:obj) = -- GitLab