gpt4 book ai didi

F# 编译器抛出 OutOfMemoryException

转载 作者:行者123 更新时间:2023-12-04 01:06:10 30 4
gpt4 key购买 nike

我使用的项目包含许多从单个基类继承的类。在单元测试中,我需要按类型和数据比较收到的结果。

当我在条件列表包含足够多的不同条件的情况下使用类型匹配比较时,编译器会抛出 OutOfMemoryException。

例如,以下 F# 代码在编译期间引发 System.OutOfMemoryException(参数错误 FS0193)(编译大约需要 30 秒才抛出异常)

type IBaseClass() = class end

type IChildClass1 () = inherit IBaseClass ()

type IChildClass2 () = inherit IBaseClass ()

type IChildClass3 () = inherit IBaseClass ()

type IChildClass4 () = inherit IBaseClass ()

type IChildClass5 () = inherit IBaseClass ()

type IChildClass6 () = inherit IBaseClass ()

type IChildClass7 () = inherit IBaseClass ()

type IChildClass8 () = inherit IBaseClass ()

type IChildClass9 () = inherit IBaseClass ()

type IChildClass10 () = inherit IBaseClass ()

type IChildClass11 () = inherit IBaseClass ()

type IChildClass12 () = inherit IBaseClass ()

type IChildClass13 () = inherit IBaseClass ()

type IChildClass14 () = inherit IBaseClass ()

type IChildClass15 () = inherit IBaseClass ()

type IChildClass16 () = inherit IBaseClass ()

type IChildClass17 () = inherit IBaseClass ()

type IChildClass18 () = inherit IBaseClass ()

type IChildClass19 () = inherit IBaseClass ()

type IChildClass20 () = inherit IBaseClass ()


let AreEqual (original: IBaseClass) (compareWith: IBaseClass) : bool =
match (original, compareWith) with
| (:? IChildClass1 as a), (:? IChildClass1 as b) -> a = b
| (:? IChildClass2 as a), (:? IChildClass2 as b) -> a = b
| (:? IChildClass3 as a), (:? IChildClass3 as b) -> a = b
| (:? IChildClass4 as a), (:? IChildClass4 as b) -> a = b
| (:? IChildClass5 as a), (:? IChildClass5 as b) -> a = b
| (:? IChildClass6 as a), (:? IChildClass6 as b) -> a = b
| (:? IChildClass7 as a), (:? IChildClass7 as b) -> a = b
| (:? IChildClass8 as a), (:? IChildClass8 as b) -> a = b
| (:? IChildClass9 as a), (:? IChildClass9 as b) -> a = b
| (:? IChildClass10 as a), (:? IChildClass10 as b) -> a = b
| (:? IChildClass11 as a), (:? IChildClass11 as b) -> a = b
| (:? IChildClass12 as a), (:? IChildClass12 as b) -> a = b
| (:? IChildClass13 as a), (:? IChildClass13 as b) -> a = b
| (:? IChildClass14 as a), (:? IChildClass14 as b) -> a = b
| (:? IChildClass15 as a), (:? IChildClass15 as b) -> a = b
| (:? IChildClass16 as a), (:? IChildClass16 as b) -> a = b
| (:? IChildClass17 as a), (:? IChildClass17 as b) -> a = b
| (:? IChildClass18 as a), (:? IChildClass18 as b) -> a = b
| (:? IChildClass19 as a), (:? IChildClass19 as b) -> a = b
| (:? IChildClass20 as a), (:? IChildClass20 as b) -> a = b
| _ -> false

当然,我可以将 IEquatable 接口(interface)添加到我的 IBaseClass 类中,这将避免使用这种匹配构造,或者将 int Kind 成员(或枚举)添加到
IBaseClass 接口(interface)并且不是按类型进行匹配,而是通过一些 int 值进行匹配。

请注意,我尝试在 MS VS 2010 和 MSVS 11 Beta 中编译相同的项目,并且遇到相同的编译器错误

问题:为什么在我的情况下发生编译器的 OutOfMemoryException(它是已知的编译器错误或其他限制),我应该如何重新组织我的匹配条件以避免它?

更新 当我将类放入可区分的联合并使用类似的匹配比较时,Fsc.exe 无一异常(exception)地编译项目
type AllClasses = 
| ChildClass1 of IChildClass1 | ChildClass2 of IChildClass2 | ChildClass3 of IChildClass3 | ChildClass4 of IChildClass4 | ChildClass5 of IChildClass5 | ChildClass6 of IChildClass6
| ChildClass7 of IChildClass7 | ChildClass8 of IChildClass8 | ChildClass9 of IChildClass9 | ChildClass10 of IChildClass10 | ChildClass11 of IChildClass11 | ChildClass12 of IChildClass12
| ChildClass13 of IChildClass13 | ChildClass14 of IChildClass14 | ChildClass15 of IChildClass15 | ChildClass16 of IChildClass16 | ChildClass17 of IChildClass17 | ChildClass18 of IChildClass18
| ChildClass19 of IChildClass19 | ChildClass20 of IChildClass20

let AreEqual2 (original: AllClasses) (compareWith: AllClasses) : bool =
match (original, compareWith) with
| ChildClass1(a), ChildClass1(b) -> a = b
| ChildClass2(a), ChildClass2(b) -> a = b
| ChildClass3(a), ChildClass3(b) -> a = b
| ChildClass4(a), ChildClass4(b) -> a = b
| ChildClass5(a), ChildClass5(b) -> a = b
| ChildClass6(a), ChildClass6(b) -> a = b
| ChildClass7(a), ChildClass7(b) -> a = b
| ChildClass8(a), ChildClass8(b) -> a = b
| ChildClass9(a), ChildClass9(b) -> a = b
| ChildClass10(a), ChildClass10(b) -> a = b
| ChildClass11(a), ChildClass11(b) -> a = b
| ChildClass12(a), ChildClass12(b) -> a = b
| ChildClass13(a), ChildClass13(b) -> a = b
| ChildClass14(a), ChildClass14(b) -> a = b
| ChildClass15(a), ChildClass15(b) -> a = b
| ChildClass16(a), ChildClass16(b) -> a = b
| ChildClass17(a), ChildClass17(b) -> a = b
| ChildClass18(a), ChildClass18(b) -> a = b
| ChildClass19(a), ChildClass19(b) -> a = b
| ChildClass20(a), ChildClass20(b) -> a = b
| _ -> false

谢谢

最佳答案

这是由 F# 编译器在这种情况下如何编译元组上的模式匹配造成的。我不完全确定您何时在这个特定问题中运行以及编译器何时使用其他方法,但这里解释了为什么它在这种情况下失败......

如果您编写与示例中类似的模式匹配,编译器本质上会生成一个决策树,用于测试 original 的第一个模式。 ( :? IChildClass1 ) 然后生成两个分支。第一个分支检查是否 compareWith也是IChildClass1 .如果是,则运行第一种情况。然后在两个分支中复制其余的模式匹配,所以你会得到类似的东西(你可以通过使用 ILSpy 查看已编译的代码来检查少数情况):

if (original is IChildClass1)
if (compareWith is IChildClass1)
case #1
if (original is IChildClass2)
if (compareWith is IChildClass2)
case #2
if (original is IChildClass3)
(...)
else
if (original is IChildClass2)
if (compareWith is IChildClass2)
case #2
if (original is IChildClass3)
(...)

这意味着生成代码的大小与此模式匹配中的案例数量成指数比例。对于 20 种情况,编译器尝试创建 2^20 个分支,但(不出所料)失败了。

关于F# 编译器抛出 OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10617364/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com