Java对象初始化中使用钻石操作符会导致javac编译时间性能差

9
我正在使用菱形操作符来初始化列表中的对象。然而,随着数组对象数量的增加,编译时间从几秒钟增加到数小时。我的Eclipse自动构建使Eclipse无响应。我注意到这是一个javac问题。当我用<String, List<Category>>替换所有的<>时,编译时间又回到了几秒钟。这是我做错了什么还是Java性能问题?以下是代码,它将花费Java数小时来编译(或者崩溃javac v8u25):
    List<Pair<String, List<Category>>> categoryMappings = null;

    public void reloadStaticData() {                  
      // Left one is the provider's category and right one is ours
      try(UoW luow = CoreModule.getInstance(UoW.class)) {
        CategoryRepo categoryRepo = luow.getCategoryRepo();
        categoryMappings = Arrays.asList(

                  // Nightlife
                  new ImmutablePair<>("Bars", Arrays.asList(categoryRepo.findByName("Bar & Pubs").get())),
                  new ImmutablePair<>("Ski-Bar", Arrays.asList(categoryRepo.findByName("Bar & Pubs").get())),
                  new ImmutablePair<>("Bar", Arrays.asList(categoryRepo.findByName("Bar & Pubs").get())),
                  new ImmutablePair<>("Beer", Arrays.asList(categoryRepo.findByName("Bar & Pubs").get())),
                  new ImmutablePair<>("Pubs", Arrays.asList(categoryRepo.findByName("Bar & Pubs").get())),
                  new ImmutablePair<>("Clubs", Arrays.asList(categoryRepo.findByName("Bar & Pubs").get())),
                  new ImmutablePair<>("Dance", Arrays.asList(categoryRepo.findByName("Bar & Pubs").get()
                          ,categoryRepo.findByName("Clubs").get())),    
                  // if I got more than 20 of these ImmutablePairs, javac crashes or takes hours to compile
      );
      }
    }

编辑:正如Sotirios在评论中提到的,这似乎是JDK中报告的问题:

类型推断指数编译性能: https://bugs.openjdk.java.net/browse/JDK-8055984

类型推断性能回归: https://bugs.openjdk.java.net/browse/JDK-8048838


6
你确定这是javac的问题吗?Eclipse使用自己的增量编译器,而不是javac。 - WonderCsabo
1
编译器崩溃是什么意思?你有堆栈跟踪吗? - Alexis C.
1
类型推断很难。https://bugs.openjdk.java.net/browse/JDK-8055984 - Sotirios Delimanolis
1
你确定反复调用 Arrays.asList(categoryRepo.findByName("Bar & Pubs").get()) 是高效的吗?难道 categoryMappings 不应该像其名称所示的那样是一个映射表吗? - Peter Lawrey
@JimmyPage,您能否提供一个自包含的示例? - Vicente Romero
显示剩余3条评论
2个回答

2
我目前正在开发JEP-215 Tiered attribution。该JEP的目标是改进javac中的归属代码,并通过副作用提高编译器的性能。例如,bugJDK-8055984中列出的代码在“普通”Javac9中编译需要很长时间!而现有版本的分层归属只需约2.5秒即可编译完成,这要好得多。分层归属的代码尚未公开。我希望它能尽快公开。同时,这种报告非常有用。

编辑:如果有人想尝试正在开发中的分层归属,请查看此公告:tiered attribution for all


0

更改

List<Pair<String, List<Category>>> categoryMappings = null;

List<? extends Pair<String, List<Category>>> categoryMappings = null;

看看这是否能加速。否则我的JDK/IDE(IntelliJ)无法编译您的代码片段。


1
我认为这是一个更好的做法,我已经进行了更改。然而,它并没有解决问题。Eclipse崩溃,mvn clean install在“编译”部分仍需要数小时才能运行。 - Jimmy Page

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接