Skip to content

Commit 1c84dc0

Browse files
author
Max Schaefer
committed
JavaScript: Parallelise extraction of JavaScript (but not TypeScript) files.
1 parent 8014ded commit 1c84dc0

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

javascript/extractor/src/com/semmle/js/extractor/AutoBuild.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.LinkedHashSet;
2020
import java.util.List;
2121
import java.util.Set;
22+
import java.util.concurrent.ExecutorService;
23+
import java.util.concurrent.Executors;
2224
import java.util.stream.Stream;
2325

2426
import com.semmle.js.extractor.ExtractorConfig.SourceType;
@@ -173,6 +175,7 @@ public class AutoBuild {
173175
private final Path LGTM_SRC, SEMMLE_DIST;
174176
private final TypeScriptMode typeScriptMode;
175177
private final String defaultEncoding;
178+
private ExecutorService threadPool;
176179

177180
public AutoBuild() {
178181
this.LGTM_SRC = toRealPath(getPathFromEnvVar("LGTM_SRC"));
@@ -372,8 +375,13 @@ private boolean addPathPattern(Set<Path> patterns, Path base, String pattern) {
372375
* Perform extraction.
373376
*/
374377
public void run() throws IOException {
375-
extractExterns();
376-
extractSource();
378+
threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
379+
try {
380+
extractExterns();
381+
extractSource();
382+
} finally {
383+
threadPool.shutdown();
384+
}
377385
}
378386

379387
/**
@@ -595,18 +603,32 @@ private SourceType getSourceType() {
595603
}
596604

597605
/**
598-
* Extract a single file.
606+
* Extract a single file using the given extractor and state.
607+
*
608+
* If the state is {@code null}, the extraction job will be submitted to the {@link #threadPool},
609+
* otherwise extraction will happen on the main thread.
599610
*/
600-
protected void extract(FileExtractor extractor, Path file, ExtractorState state) throws IOException {
611+
protected void extract(FileExtractor extractor, Path file, ExtractorState state) {
612+
if (state == null)
613+
threadPool.submit(() -> doExtract(extractor, file, state));
614+
else
615+
doExtract(extractor, file, state);
616+
}
617+
618+
private void doExtract(FileExtractor extractor, Path file, ExtractorState state) {
601619
File f = file.toFile();
602620
if (!f.exists()) {
603621
warn("Skipping " + file + ", which does not exist.");
604622
return;
605623
}
606624

607-
long start = logBeginProcess("Extracting " + file);
608-
extractor.extract(f, state);
609-
logEndProcess(start);
625+
try {
626+
long start = logBeginProcess("Extracting " + file);
627+
extractor.extract(f, state);
628+
logEndProcess(start);
629+
} catch (IOException e) {
630+
throw new ResourceError("Exception while extracting " + file + ".", e);
631+
}
610632
}
611633

612634
private void warn(String msg) {

0 commit comments

Comments
 (0)