Skip to content

Commit

Permalink
rewrite SizeOfValueInfo in Optimizer.fs to be tail-recursive (#16559)
Browse files Browse the repository at this point in the history
* rewrite SizeOfValueInfo in Optimizer.fs to be tail-recursive

* use Brians rewrite into one local function
  • Loading branch information
dawedawe authored Jan 22, 2024
1 parent 1112c11 commit 09ac400
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/Compiler/Optimize/Optimizer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,23 @@ type Summary<'Info> =
// Note, this is a different notion of "size" to the one used for inlining heuristics
//-------------------------------------------------------------------------

let rec SizeOfValueInfos (arr:_[]) =
if arr.Length <= 0 then 0 else max 0 (SizeOfValueInfo arr[0])

and SizeOfValueInfo x =
match x with
| SizeValue (vdepth, _v) -> vdepth // terminate recursion at CACHED size nodes
| ConstValue (_x, _) -> 1
| UnknownValue -> 1
| ValValue (_vr, vinfo) -> SizeOfValueInfo vinfo + 1
| TupleValue vinfos
| RecdValue (_, vinfos)
| UnionCaseValue (_, vinfos) -> 1 + SizeOfValueInfos vinfos
| CurriedLambdaValue _ -> 1
| ConstExprValue (_size, _) -> 1
let SizeOfValueInfo valueInfo =
let rec loop acc valueInfo =
match valueInfo with
| SizeValue (vdepth, _v) -> assert (vdepth >= 0); acc + vdepth // terminate recursion at CACHED size nodes
| CurriedLambdaValue _
| ConstExprValue _
| ConstValue _
| UnknownValue -> acc + 1
| TupleValue vinfos
| RecdValue (_, vinfos)
| UnionCaseValue (_, vinfos) when vinfos.Length = 0 -> acc + 1
| TupleValue vinfos
| RecdValue (_, vinfos)
| UnionCaseValue (_, vinfos) -> loop (acc + 1) vinfos[0]
| ValValue (_vr, vinfo) -> loop (acc + 1) vinfo

loop 0 valueInfo

let [<Literal>] minDepthForASizeNode = 5 // for small vinfos do not record size info, save space

Expand Down

0 comments on commit 09ac400

Please sign in to comment.