Java-SAM类型优化

发布于 2024-11-29 17:34:33 字数 367 浏览 1 评论 0原文

描述 工作文档 的状态="http://openjdk.java.net/projects/lambda/" rel="nofollow">Project Lambda 提到了所谓的 SAM(单一抽象方法)类型。据我所知,当前的 lambda 提案不会影响运行时,只会影响编译器,因为它可以实现从 lambda 表达式到这些类型的自动转换。

我认为在理想情况下,SAM 类型的实例可以在内部由函数指针表示。因此,JVM 可以避免为这些实例分配内存。

我想知道现代虚拟机是否能够提供这样的优化。

A working document describing the state of Project Lambda mentions the so-called SAM (single abstract method) types. As far as I know the current lambda proposal doesn't affect the runtime just the compiler by making possible automatic conversion from lambda expressions to these types.

I think in ideal circumstances instances of SAM types could be internally represented by function pointers. Therefore the JVM could avoid memory allocation for these instances.

I am wondering whether the modern virtual machines are able to provide such optimization.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

债姬 2024-12-06 17:34:33

@Tamás 您可能应该阅读 Brian Goetz 的邮件列表帖子:

http://mail.openjdk.java.net/pipermail/lambda-dev/2011-August/003877.html

基本上, lambda 抽象目前是使用对象实现的。然而,它被设计为允许 lambda 的替代实现,这将比类的实例“更小”。

您可以将这种情况视为类似于自动装箱 - 整数被装箱为整数,但具有“较小”的表示形式(如整数)。

目前,lambda 必须装箱到 SAM 类型的实例,因为 JVM 目前无法用任何更小的构造来表示 lambda。将来,可能会有一个新的 JVM 标准,其中包括“原始函数”,可以将 lambda 表示为对象以外的东西。

因此,为了回答您的问题,您上面提出的优化类型可能是可能的,但它可能会随着 Java 8 之后的“原始函数”工作而出现,而不是特定于实现的功能。

@Tamás You should probably have a read of this mailing list post by Brian Goetz:

http://mail.openjdk.java.net/pipermail/lambda-dev/2011-August/003877.html

Basically, the lambda abstraction is currently implemented using objects. However, it has been designed to permit alternative realizations of lambdas, which would be "smaller" than instances of classes.

You can think of the situation as similar to autoboxing - ints are boxed to Integer, but have a "smaller" representation (as ints).

Currently, lambdas have to be boxed to instances of SAM types, b/c the JVM currently has no way to represent a lambda with any smaller construct. In the future, there may be a new JVM standard which includes "primitive functions" which could represent lambdas as something other than objects.

So, to answer your question, the type of optimization you propose above, may be possible, but it probably would come with post-Java 8 work on "primitive functions" rather than being an implementation-specific feature.

内心荒芜 2024-12-06 17:34:33

将单个方法类转换为函数指针并不困难,但您错过了一件事:lambda 表达式不仅仅是函数,它们还是闭包。不同之处在于闭包可以捕获外部变量。考虑伪 Java 中的下一个示例:

public Adder makeAdder(double startNumber) {
    return #{ int number -> number + startNumber}
}

...

int startNumber = 5; 
Adder add5 = makeAdder(startNumber);
add5.invoke(4);  // ==> 9 

在此示例中,通过调用 makeAdder() 生成的 lambda 函数引用在此 lambda 外部定义的变量。这就是为什么它被称为“闭包” - 它们在其自由变量上“封闭”(在本例中 - 在 startNumber 上)。为了处理这种情况,闭包必须同时保存指向函数的指针和指向其环境的指针。因此,您将获得一些具有方法和至少一个变量的数据结构。但这不是 OOP 中对象的定义吗?那么,如果可以将其作为匿名类的实例,那么创建新类型对象的原因是什么?

尽管如此,可以对此类匿名类进行一些其他优化。您指出的工作文档提到了其中的一些内容,例如,推断和有效使用最终变量(尽管这样做主要是为了原则上允许 JVM 上的 lambda,而不是优化代码)。生成的匿名类也可以最终完成,并且大多数 JVM 已经对最终变量和类进行了良好的优化。

其他改进也可能涉及环境参考——那里有大量的选择。

There's nothing hard in converting single method classes to function pointers, but you are missing one thing: lambda expressions are not just functions, they are closures. The difference is that closures can capture outer variables. Consider next example in pseudo Java:

public Adder makeAdder(double startNumber) {
    return #{ int number -> number + startNumber}
}

...

int startNumber = 5; 
Adder add5 = makeAdder(startNumber);
add5.invoke(4);  // ==> 9 

In this example lambda function, produced by the call to makeAdder(), refers to the variable that was defined outside of this lambda. That is why it is called "closures" - they are "closed over" their free variables (in this case - over startNumber). To handle such situations closures must hold both pointer to a function and pointer to its environment. So, you get some data structure that has a method and at least one variable. But isn't it a definition of an object in OOP? So what's the reason to create new kind of objects if you can make it an instance of anonymous class?

Nevertheless some other optimizations on such anonymous classes may be done. Working document you pointed to mentions some of them, for example, inferring and using effectively final variables (though this is done mostly to allow lambdas on JVM in principal, not to optimize code). Produced anonymous class also may be done final, and most JVMs already have good optimizations for final vars and classes.

Other improvements may also concern references to environment - there are tons of options there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文