Skip to content

Commit a6cb63e

Browse files
author
Max Schaefer
committed
JavaScript: Make number of threads configurable through LGTM_INDEX_THREADS.
If the number of threads specified is one or lower, we fall back onto single-threaded extraction.
1 parent d625ebf commit a6cb63e

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,16 @@
161161
* </p>
162162
*
163163
* <p>
164-
* Finally, the environment variables <code>LGTM_TRAP_CACHE</code> and
165-
* <code>LGTM_TRAP_CACHE_BOUND</code> can optionally be used to specify the location and size
166-
* of a trap cache to be used during extraction.
164+
* To customise the actual extraction (as opposed to determining which files to extract),
165+
* the following environment variables are available:
167166
* </p>
167+
* <ul>
168+
* <li><code>LGTM_INDEX_THREADS</code> determines how many threads are used for parallel extraction
169+
* of JavaScript files (TypeScript files cannot currently be extracted in parallel). If left
170+
* unspecified, the extractor uses as many threads as there are cores.</li>
171+
* <li><code>LGTM_TRAP_CACHE</code> and <code>LGTM_TRAP_CACHE_BOUND</code> can be used to specify the
172+
* location and size of a trap cache to be used during extraction.</li>
173+
* </ul>
168174
*/
169175
public class AutoBuild {
170176
private final ExtractorOutputConfig outputConfig;
@@ -375,15 +381,29 @@ private boolean addPathPattern(Set<Path> patterns, Path base, String pattern) {
375381
* Perform extraction.
376382
*/
377383
public void run() throws IOException {
378-
threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
384+
startThreadPool();
379385
try {
380386
extractSource();
381387
extractExterns();
382388
} finally {
383-
threadPool.shutdown();
389+
shutdownThreadPool();
384390
}
385391
}
386392

393+
private void startThreadPool() {
394+
int defaultNumThreads = Runtime.getRuntime().availableProcessors();
395+
int numThreads = Env.systemEnv().getInt("LGTM_INDEX_THREADS", defaultNumThreads);
396+
if (numThreads > 1)
397+
threadPool = Executors.newFixedThreadPool(numThreads);
398+
else
399+
threadPool = null;
400+
}
401+
402+
private void shutdownThreadPool() {
403+
if (threadPool != null)
404+
threadPool.shutdown();
405+
}
406+
387407
/**
388408
* Extract all "*.js" files under <code>$SEMMLE_DIST/tools/data/externs</code> as
389409
* externs.
@@ -624,7 +644,7 @@ private SourceType getSourceType() {
624644
* otherwise extraction will happen on the main thread.
625645
*/
626646
protected void extract(FileExtractor extractor, Path file, ExtractorState state) {
627-
if (state == null)
647+
if (state == null && threadPool != null)
628648
threadPool.submit(() -> doExtract(extractor, file, state));
629649
else
630650
doExtract(extractor, file, state);

0 commit comments

Comments
 (0)