aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src
diff options
context:
space:
mode:
authorAlain Kermis <alain.kermis@sonarsource.com>2023-11-30 14:16:50 +0100
committersonartech <sonartech@sonarsource.com>2023-12-01 20:02:43 +0000
commit614c53b5f13f2fe09e07e18d3a47747c6532442f (patch)
treeb965dc7adc6d84fffb4688730f08521158a5fd17 /sonar-scanner-engine/src
parent96b001dde8e74bec827cb369024e5376aa7f7604 (diff)
downloadsonarqube-614c53b5f13f2fe09e07e18d3a47747c6532442f.tar.gz
sonarqube-614c53b5f13f2fe09e07e18d3a47747c6532442f.zip
SONAR-20737 Improve performance of large SARIF imports
Diffstat (limited to 'sonar-scanner-engine/src')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java42
1 files changed, 35 insertions, 7 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java
index 129d4b61d93..d41de202f89 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java
@@ -20,14 +20,19 @@
package org.sonar.scanner.externalissue.sarif;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
+import java.util.concurrent.TimeUnit;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import org.jetbrains.annotations.NotNull;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.predicates.AbstractFilePredicate;
import org.sonar.api.batch.sensor.SensorContext;
@@ -43,10 +48,18 @@ import static org.sonar.api.utils.Preconditions.checkArgument;
@ScannerSide
public class LocationMapper {
+ private static final int CACHE_SIZE = 500;
+ private static final int CACHE_EXPIRY = 60;
private final SensorContext sensorContext;
private final RegionMapper regionMapper;
+ LoadingCache<String, Optional<InputFile>> inputFileCache = CacheBuilder.newBuilder()
+ .maximumSize(CACHE_SIZE)
+ .expireAfterAccess(CACHE_EXPIRY, TimeUnit.SECONDS)
+ .concurrencyLevel(Runtime.getRuntime().availableProcessors())
+ .build(getCacheLoader());
+
LocationMapper(SensorContext sensorContext, RegionMapper regionMapper) {
this.sensorContext = sensorContext;
this.regionMapper = regionMapper;
@@ -64,12 +77,13 @@ public class LocationMapper {
PhysicalLocation physicalLocation = location.getPhysicalLocation();
String fileUri = getFileUriOrThrow(location);
- InputFile file = findFile(sensorContext, fileUri);
- if (file == null) {
+ Optional<InputFile> file = findFile(fileUri);
+ if (file.isEmpty()) {
return null;
}
- newIssueLocation.on(file);
- regionMapper.mapRegion(physicalLocation.getRegion(), file).ifPresent(newIssueLocation::at);
+ InputFile inputFile = file.get();
+ newIssueLocation.on(inputFile);
+ regionMapper.mapRegion(physicalLocation.getRegion(), inputFile).ifPresent(newIssueLocation::at);
return newIssueLocation;
}
@@ -88,10 +102,23 @@ public class LocationMapper {
return Optional.ofNullable(location).map(PhysicalLocation::getArtifactLocation).map(ArtifactLocation::getUri).isPresent();
}
- @CheckForNull
- private static InputFile findFile(SensorContext context, String filePath) {
+ private Optional<InputFile> findFile(String filePath) {
+ return inputFileCache.getUnchecked(filePath);
+ }
+
+ private CacheLoader<String, Optional<InputFile>> getCacheLoader() {
+ return new CacheLoader<>() {
+ @NotNull
+ @Override
+ public Optional<InputFile> load(final String filePath) {
+ return computeInputFile(filePath);
+ }
+ };
+ }
+
+ private Optional<InputFile> computeInputFile(String key) {
// we use a custom predicate (which is not optimized) because fileSystem().predicates().is() doesn't handle symlinks correctly
- return context.fileSystem().inputFile(new IsPredicate(getFileFromAbsoluteUriOrPath(filePath).toPath()));
+ return Optional.ofNullable(sensorContext.fileSystem().inputFile(new LocationMapper.IsPredicate(getFileFromAbsoluteUriOrPath(key).toPath())));
}
private static File getFileFromAbsoluteUriOrPath(String filePath) {
@@ -120,4 +147,5 @@ public class LocationMapper {
}
}
}
+
}