/*
 * Copyright 2017 The Closure Compiler Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.javascript.jscomp;

import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * A helper class to prebuild ASTs from a list of {@link CompilerInput}. Inputs are parsed into ASTs
 * the first time we try to get them. Get them all now using multiple threads, so they'll be parsed
 * in parallel and already available during the rest of the compilation.
 */
class PrebuildAst {

  private final AbstractCompiler compiler;
  private final int numParallelThreads;

  PrebuildAst(AbstractCompiler compiler, int numParalleThreads) {
    this.compiler = compiler;
    this.numParallelThreads = numParalleThreads;
  }

  void prebuild(List<CompilerInput> inputList) {
    ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
        numParallelThreads,
        numParallelThreads,
        Integer.MAX_VALUE,
        TimeUnit.SECONDS,
        new LinkedBlockingQueue<Runnable>());
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(poolExecutor);
    List<ListenableFuture<?>> futureList = new ArrayList<>(inputList.size());
    for (final CompilerInput input : inputList) {
      futureList.add(executorService.submit(new Runnable() {
        @Override
        public void run() {
          input.getAstRoot(compiler);
        }
      }));
    }

    poolExecutor.shutdown();
    try {
      Futures.allAsList(futureList).get();
    } catch (InterruptedException | ExecutionException e) {
      throw new RuntimeException(e);
    }
  }

}

@@ -1026,16 +1026,59 @@
 gQueue;%0A
+import java.util.concurrent.ThreadFactory;%0A
 import j
@@ -1117,16 +1117,16 @@
 ecutor;%0A
-
 import j
@@ -1457,16 +1457,180 @@
 ildAst %7B
+%0A  // We use many recursive algorithms that use O(d) memory in the depth%0A  // of the tree.%0A  private static final long COMPILER_STACK_SIZE = (1 %3C%3C 21); // About 2MB
 %0A%0A  priv
@@ -1902,24 +1902,340 @@
 nputList) %7B%0A
+    ThreadFactory threadFactory = new ThreadFactory() %7B%0A        @Override%0A        public Thread newThread(Runnable r) %7B%0A          Thread t = new Thread(null, r, %22jscompiler-PrebuildAst%22, COMPILER_STACK_SIZE);%0A          t.setDaemon(true);  // Do not prevent the JVM from exiting.%0A          return t;%0A        %7D%0A    %7D;%0A
     ThreadPo
@@ -2393,16 +2393,16 @@
 ECONDS,%0A
-
         
@@ -2436,16 +2436,39 @@
 nable%3E()
+,%0A        threadFactory
 );%0A    L

Increase thread stack size for PrebuildAst threads.