gpt4 book ai didi

java - jit 会优化新对象吗

转载 作者:行者123 更新时间:2023-12-01 16:46:20 28 4
gpt4 key购买 nike

我创建这个类是为了不可变并且具有流畅的 API:

public final class Message {
public final String email;
public final String escalationEmail;
public final String assignee;
public final String conversationId;
public final String subject;
public final String userId;

public Message(String email, String escalationEmail, String assignee, String conversationId, String subject, String userId) {
this.email = email;
this.escalationEmail = escalationEmail;
this.assignee = assignee;
this.conversationId = conversationId;
this.subject = subject;
this.userId = userId;
}

public Message() {
email = "";
escalationEmail = "";
assignee = "";
conversationId = "";
subject = "";
userId = "";
}

public Message email(String e) { return new Message(e, escalationEmail, assignee, conversationId, subject, userId); }
public Message escalationEmail(String e) { return new Message(email, e, assignee, conversationId, subject, userId); }
public Message assignee(String a) { return new Message(email, escalationEmail, a, conversationId, subject, userId); }
public Message conversationId(String c) { return new Message(email, escalationEmail, assignee, c, subject, userId); }
public Message subject(String s) { return new Message(email, escalationEmail, assignee, conversationId, s, userId); }
public Message userId(String u) { return new Message(email, escalationEmail, assignee, conversationId, subject, u); }

}

我的问题是,当像这样创建新对象时,优化器是否能够避免大量对象创建:

Message m = new Message()
.email("foo@bar.com")
.assignee("bar@bax.com")
.subject("subj");

通过创建单独的可变构建器对象可以获得什么好处吗?

更新 2:阅读 apangin 的答案后,我的基准测试无效。我将其保留在这里,以供引用如何不进行基准测试:)

更新:我冒昧地用这段代码自己测量了这一点:

public final class Message {
public final String email;
public final String escalationEmail;
public final String assignee;
public final String conversationId;
public final String subject;
public final String userId;

public static final class MessageBuilder {
private String email;
private String escalationEmail;
private String assignee;
private String conversationId;
private String subject;
private String userId;

MessageBuilder email(String e) { email = e; return this; }
MessageBuilder escalationEmail(String e) { escalationEmail = e; return this; }
MessageBuilder assignee(String e) { assignee = e; return this; }
MessageBuilder conversationId(String e) { conversationId = e; return this; }
MessageBuilder subject(String e) { subject = e; return this; }
MessageBuilder userId(String e) { userId = e; return this; }

public Message create() {
return new Message(email, escalationEmail, assignee, conversationId, subject, userId);
}

}

public static MessageBuilder createNew() {
return new MessageBuilder();
}

public Message(String email, String escalationEmail, String assignee, String conversationId, String subject, String userId) {
this.email = email;
this.escalationEmail = escalationEmail;
this.assignee = assignee;
this.conversationId = conversationId;
this.subject = subject;
this.userId = userId;
}

public Message() {
email = "";
escalationEmail = "";
assignee = "";
conversationId = "";
subject = "";
userId = "";
}

public Message email(String e) { return new Message(e, escalationEmail, assignee, conversationId, subject, userId); }
public Message escalationEmail(String e) { return new Message(email, e, assignee, conversationId, subject, userId); }
public Message assignee(String a) { return new Message(email, escalationEmail, a, conversationId, subject, userId); }
public Message conversationId(String c) { return new Message(email, escalationEmail, assignee, c, subject, userId); }
public Message subject(String s) { return new Message(email, escalationEmail, assignee, conversationId, s, userId); }
public Message userId(String u) { return new Message(email, escalationEmail, assignee, conversationId, subject, u); }


static String getString() {
return new String("hello");
// return "hello";
}

public static void main(String[] args) {
int n = 1000000000;

long before1 = System.nanoTime();

for (int i = 0; i < n; ++i) {
Message m = new Message()
.email(getString())
.assignee(getString())
.conversationId(getString())
.escalationEmail(getString())
.subject(getString())
.userId(getString());
}

long after1 = System.nanoTime();

long before2 = System.nanoTime();

for (int i = 0; i < n; ++i) {
Message m = Message.createNew()
.email(getString())
.assignee(getString())
.conversationId(getString())
.escalationEmail(getString())
.subject(getString())
.userId(getString())
.create();
}

long after2 = System.nanoTime();



System.out.println("no builder : " + (after1 - before1)/1000000000.0);
System.out.println("with builder: " + (after2 - before2)/1000000000.0);
}


}

如果字符串参数不是新对象,但完全相同(请参阅 getString 中的注释代码),我发现差异很显着(构建器更快)

在我想象的更现实的场景中,当所有字符串都是新对象时,差异可以忽略不计,并且 JVM 启动会导致第一个字符串稍微慢一点(我尝试了两种方法)。

使用“新字符串”时,代码总体速度也慢了很多倍(我必须减少 n),这可能表明正在对“新消息”进行一些优化,但不是“新字符串”。

最佳答案

是的,HotSpot JIT 可以消除本地上下文中的冗余分配。

此优化由 Escape Analysis 提供从 JDK 6u23 开始启用。它经常与栈上分配混淆,但实际上它更强大,因为它不仅允许在栈上分配对象,而且通过用变量替换对象字段(标量替换)来完全消除分配,这些变量受到进一步的影响。优化。

优化由-XX:+EliminateAllocations JVM选项控制,默认情况下为ON。

<小时/>

由于分配消除优化,您创建 Message 对象的两个示例都以相同的方式有效工作。它们不分配中间对象;只是最后一个。

您的基准测试显示了误导性的结果,因为它收集了许多 common pitfalls微基准测试:

  • 它在一个方法中整合了多个基准;
  • 它测量 OSR stub而不是最终编译版本;
  • 它不进行预热迭代;
  • 它不消耗结果等。

让我们用 JMH 来正确测量它。作为奖励,JMH 具有分配分析器 (-prof gc),它显示每次迭代实际分配的字节数。我添加了在禁用 EliminateAllocations 优化的情况下运行的第三个测试,以显示差异。

package bench;

import org.openjdk.jmh.annotations.*;

@State(Scope.Benchmark)
public class MessageBench {

@Benchmark
public Message builder() {
return Message.createNew()
.email(getString())
.assignee(getString())
.conversationId(getString())
.escalationEmail(getString())
.subject(getString())
.userId(getString())
.create();
}

@Benchmark
public Message immutable() {
return new Message()
.email(getString())
.assignee(getString())
.conversationId(getString())
.escalationEmail(getString())
.subject(getString())
.userId(getString());
}

@Benchmark
@Fork(jvmArgs = "-XX:-EliminateAllocations")
public Message immutableNoOpt() {
return new Message()
.email(getString())
.assignee(getString())
.conversationId(getString())
.escalationEmail(getString())
.subject(getString())
.userId(getString());
}

private String getString() {
return "hello";
}
}

这是结果。 builderimmutable 的性能相同,每次迭代仅分配 40 个字节(恰好是一个 Message 对象的大小)。

Benchmark                                        Mode  Cnt     Score     Error   Units
MessageBench.builder avgt 10 6,232 ± 0,111 ns/op
MessageBench.immutable avgt 10 6,213 ± 0,087 ns/op
MessageBench.immutableNoOpt avgt 10 41,660 ± 2,466 ns/op

MessageBench.builder:·gc.alloc.rate.norm avgt 10 40,000 ± 0,001 B/op
MessageBench.immutable:·gc.alloc.rate.norm avgt 10 40,000 ± 0,001 B/op
MessageBench.immutableNoOpt:·gc.alloc.rate.norm avgt 10 280,000 ± 0,001 B/op

关于java - jit 会优化新对象吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42504862/

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