From 15a3fff602e2e83bf1305148fe48e033585af719 Mon Sep 17 00:00:00 2001 From: Victor Baybekov Date: Sat, 9 Jan 2021 22:34:20 +0100 Subject: [PATCH] Map: optimize comparer: replace embedded IL with verifiable operations --- src/fsharp/FSharp.Core/map.fs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/fsharp/FSharp.Core/map.fs b/src/fsharp/FSharp.Core/map.fs index 4dac3e843b7..eae1076506d 100644 --- a/src/fsharp/FSharp.Core/map.fs +++ b/src/fsharp/FSharp.Core/map.fs @@ -96,24 +96,32 @@ module MapTree = else if Type.op_Equality(typeof<'T>, typeof) then unbox(box(l)).CompareTo(unbox(box(r))) else if Type.op_Equality(typeof<'T>, typeof) then unbox(box(l)).CompareTo(unbox(box(r))) else if Type.op_Equality(typeof<'T>, typeof) then unbox(box(l)).CompareTo(unbox(box(r))) - else if Type.op_Equality(typeof<'T>, typeof) then if (# "clt" l r : bool #) then (-1) else (# "cgt" l r : int #) - else if Type.op_Equality(typeof<'T>, typeof) then if (# "clt" l r : bool #) then (-1) else (# "cgt" l r : int #) + else if Type.op_Equality(typeof<'T>, typeof) then + unbox(box(l)).ToInt64().CompareTo( (unbox(box(r))).ToInt64()) + else if Type.op_Equality(typeof<'T>, typeof) then + unbox(box(l)).ToUInt64().CompareTo( (unbox(box(r))).ToUInt64()) else if Type.op_Equality(typeof<'T>, typeof) then unbox(box(l)).CompareTo(unbox(box(r))) else if Type.op_Equality(typeof<'T>, typeof) then unbox(box(l)).CompareTo(unbox(box(r))) // F# rules for floats else if Type.op_Equality(typeof<'T>, typeof) then - if (# "clt" l r : bool #) then (-1) - elif (# "cgt" l r : bool #) then (1) - elif (# "ceq" l r : bool #) then (0) - elif (# "ceq" r r : bool #) then (-1) - else (# "ceq" l l : int #) + let l = unbox(box(l)) + let r = unbox(box(r)) + if l < r then (-1) + elif l > r then (1) + elif l = r then (0) + elif r = r then (-1) + elif l = l then (1) + else 0 else if Type.op_Equality(typeof<'T>, typeof) then - if (# "clt" l r : bool #) then (-1) - elif (# "cgt" l r : bool #) then (1) - elif (# "ceq" l r : bool #) then (0) - elif (# "ceq" r r : bool #) then (-1) - else (# "ceq" l l : int #) + let l = unbox(box(l)) + let r = unbox(box(r)) + if l < r then (-1) + elif l > r then (1) + elif l = r then (0) + elif r = r then (-1) + elif l = l then (1) + else 0 else if Type.op_Equality(typeof<'T>, typeof) then unbox(box(l)).CompareTo(unbox(box(r))) else if Type.op_Equality(typeof<'T>, typeof) then unbox(box(l)).CompareTo(unbox(box(r))) else if Type.op_Equality(typeof<'T>, typeof) then unbox(box(l)).CompareTo(unbox(box(r)))