Skip to content

Commit 11666b8

Browse files
authored
Merge pull request #7809 from TIHan/opt/freevars-let
FreeVars cache optimization on Expr.Let
2 parents e0209fe + 6d96c91 commit 11666b8

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/fsharp/TastOps.fs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4388,6 +4388,10 @@ let bindLhs opts (bind: Binding) fvs = boundLocalVal opts bind.Var fvs
43884388

43894389
let freeVarsCacheCompute opts cache f = if opts.canCache then cached cache f else f()
43904390

4391+
let tryGetFreeVarsCacheValue opts cache =
4392+
if opts.canCache then tryGetCacheValue cache
4393+
else ValueNone
4394+
43914395
let rec accBindRhs opts (TBind(_, repr, _)) acc = accFreeInExpr opts repr acc
43924396

43934397
and accFreeInSwitchCases opts csl dflt (acc: FreeVars) =
@@ -4485,13 +4489,16 @@ and accFreeInExpr (opts: FreeVarOptions) x acc =
44854489
and accFreeInExprLinear (opts: FreeVarOptions) x acc contf =
44864490
// for nested let-bindings, we need to continue after the whole let-binding is processed
44874491
match x with
4488-
| Expr.Let (bind, e, _, cache) ->
4489-
let contf = contf << (fun free ->
4490-
unionFreeVars (freeVarsCacheCompute opts cache (fun () -> bindLhs opts bind (accBindRhs opts bind free))) acc )
4491-
accFreeInExprLinear opts e emptyFreeVars contf
4492+
| Expr.Let (bind, e, _, cache) ->
4493+
match tryGetFreeVarsCacheValue opts cache with
4494+
| ValueSome free -> contf (unionFreeVars free acc)
4495+
| _ ->
4496+
accFreeInExprLinear opts e emptyFreeVars (contf << (fun free ->
4497+
unionFreeVars (freeVarsCacheCompute opts cache (fun () -> bindLhs opts bind (accBindRhs opts bind free))) acc
4498+
))
44924499
| _ ->
4493-
// No longer linear expr
4494-
accFreeInExpr opts x acc |> contf
4500+
// No longer linear expr
4501+
contf (accFreeInExpr opts x acc)
44954502

44964503
and accFreeInExprNonLinear opts x acc =
44974504
match x with

src/fsharp/lib.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ let inline cacheOptRef cache f =
394394
cache := Some res
395395
res
396396

397+
let inline tryGetCacheValue cache =
398+
match box cache.cacheVal with
399+
| null -> ValueNone
400+
| _ -> ValueSome cache.cacheVal
397401

398402
#if DUMPER
399403
type Dumper(x:obj) =

0 commit comments

Comments
 (0)