Java对象初始化与钻石运算符可怕的javac编译时性能

Jim*_*age 9 java collections type-inference diamond-operator

我正在使用菱形运算符来启动列表中的对象.但是,随着数组对象数量的增加,编译时间从几秒增加到几小时.我的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
      );
      }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

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

Vic*_*ero 2

我目前正在研究JEP-215 分层归因。此 JEP 的目标是改进 javac 中的归因代码,并作为副作用提高编译器的性能。例如,bug JDK-8055984中列出的代码是由“正常”Javac9 编译的:很多时间!当前版本的分层归因在大约 2.5 秒内完成编译,这要好得多。分层归因的代码尚未公开。我希望会这么快。同时,此类报告确实很有用。

编辑:如果有人想尝试仍在开发中的分层归因,请查看此公告:所有人的分层归因