From 5149fc38c6b78672a01bedb4070ca009baf31815 Mon Sep 17 00:00:00 2001 From: dawe Date: Fri, 19 Jan 2024 21:09:43 +0100 Subject: [PATCH 1/2] rewrite SizeOfValueInfo in Optimizer.fs to be tail-recursive --- src/Compiler/Optimize/Optimizer.fs | 31 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/Compiler/Optimize/Optimizer.fs b/src/Compiler/Optimize/Optimizer.fs index e1eaddef8a8..aabc7e58694 100644 --- a/src/Compiler/Optimize/Optimizer.fs +++ b/src/Compiler/Optimize/Optimizer.fs @@ -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 x = + let rec SizeOfValueInfosHelper (arr:_[]) acc = + if arr.Length = 0 then acc else SizeOfValueInfoHelper arr[0] acc + + and SizeOfValueInfoHelper x acc = + match x with + | SizeValue (vdepth, _v) -> max 0 (acc + vdepth) // terminate recursion at CACHED size nodes + | ConstValue (_x, _) -> acc + 1 + | UnknownValue -> acc + 1 + | ValValue (_vr, vinfo) -> SizeOfValueInfoHelper vinfo (acc + 1) + | TupleValue vinfos + | RecdValue (_, vinfos) + | UnionCaseValue (_, vinfos) -> SizeOfValueInfosHelper vinfos (acc + 1) + | CurriedLambdaValue _ -> acc + 1 + | ConstExprValue (_size, _) -> acc + 1 + + SizeOfValueInfoHelper x 0 let [] minDepthForASizeNode = 5 // for small vinfos do not record size info, save space From 6b18f2c05776cd7246e6768cfcb0e22b673ad3d6 Mon Sep 17 00:00:00 2001 From: dawe Date: Sun, 21 Jan 2024 00:14:04 +0100 Subject: [PATCH 2/2] use Brians rewrite into one local function --- src/Compiler/Optimize/Optimizer.fs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Compiler/Optimize/Optimizer.fs b/src/Compiler/Optimize/Optimizer.fs index aabc7e58694..73ed2972ea0 100644 --- a/src/Compiler/Optimize/Optimizer.fs +++ b/src/Compiler/Optimize/Optimizer.fs @@ -230,23 +230,23 @@ type Summary<'Info> = // Note, this is a different notion of "size" to the one used for inlining heuristics //------------------------------------------------------------------------- -let SizeOfValueInfo x = - let rec SizeOfValueInfosHelper (arr:_[]) acc = - if arr.Length = 0 then acc else SizeOfValueInfoHelper arr[0] acc - - and SizeOfValueInfoHelper x acc = - match x with - | SizeValue (vdepth, _v) -> max 0 (acc + vdepth) // terminate recursion at CACHED size nodes - | ConstValue (_x, _) -> acc + 1 - | UnknownValue -> acc + 1 - | ValValue (_vr, vinfo) -> SizeOfValueInfoHelper vinfo (acc + 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) -> SizeOfValueInfosHelper vinfos (acc + 1) - | CurriedLambdaValue _ -> acc + 1 - | ConstExprValue (_size, _) -> acc + 1 + | 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 - SizeOfValueInfoHelper x 0 + loop 0 valueInfo let [] minDepthForASizeNode = 5 // for small vinfos do not record size info, save space