Skip to content

Commit

Permalink
Set initial capacity for collections (#1267)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck authored Nov 8, 2024
1 parent 9a8f36a commit 6c2df6f
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 57 deletions.
9 changes: 5 additions & 4 deletions cli/src/main/java/org/jboss/pnc/build/finder/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -73,6 +72,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.redhat.red.build.koji.KojiClientException;

import ch.qos.logback.classic.Level;
Expand Down Expand Up @@ -102,6 +102,8 @@
public final class Main implements Callable<Void> {
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

private static final int NEWMAP_SIZE = 45858;

private ExecutorService pool;

private ExecutorService finderPool;
Expand Down Expand Up @@ -556,7 +558,7 @@ public Void call() {
File checksumFile = new File(outputDirectory, BuildFinder.getChecksumFilename(checksumType));

if (checksumFile.exists()) {
checksumsFromFile.put(checksumType, new ArrayListValuedHashMap<>());
checksumsFromFile.put(checksumType, new ArrayListValuedHashMap<>()); // TODO: size

LOGGER.info("Loading checksums from file: {}", green(checksumFile));

Expand Down Expand Up @@ -703,7 +705,7 @@ public Void call() {
finder = new BuildFinder(session, config, analyzer, cacheManager);
}

Map<Checksum, Collection<String>> newMap = new HashMap<>();
Map<Checksum, Collection<String>> newMap = Maps.newHashMapWithExpectedSize(NEWMAP_SIZE);

for (ChecksumType checksumType : checksumTypes) {
Map<String, Collection<LocalFile>> map = checksums.get(checksumType).asMap();
Expand All @@ -718,7 +720,6 @@ public Void call() {
}

finder.setOutputDirectory(outputDirectory);

builds = finder.findBuilds(newMap);
} catch (KojiClientException e) {
LOGGER.error("Error finding builds: {}", boldRed(e.getMessage()));
Expand Down
4 changes: 4 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.redhat.red.build</groupId>
<artifactId>kojiji</artifactId>
Expand Down
38 changes: 28 additions & 10 deletions core/src/main/java/org/jboss/pnc/build/finder/core/BuildFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.redhat.red.build.koji.KojiClientException;
import com.redhat.red.build.koji.model.xmlrpc.KojiArchiveInfo;
import com.redhat.red.build.koji.model.xmlrpc.KojiArchiveQuery;
Expand All @@ -87,6 +89,18 @@ public class BuildFinder

private static final String CHECKSUMS_FILENAME_BASENAME = "checksums-";

private static final int CHECKSUMS_SIZE = 18130;

private static final int ALL_BUILDS_SIZE = 2048;

private static final int BUILDS_SIZE = 1330;

private static final int FOUND_CHECKSUMS_SIZE = 5112;

private static final int NOT_FOUND_CHECKSUMS_SIZE = 8996;

private static final int ALL_KOJI_BUILDS_SIZE = 1835;

private final ClientSession session;

private final BuildConfig config;
Expand Down Expand Up @@ -148,8 +162,8 @@ public BuildFinder(
this.outputDirectory = new File("");
this.analyzer = analyzer;
this.cacheManager = cacheManager;
this.allKojiBuilds = new HashMap<>();

// this.allKojiBuilds = Maps.newHashMapWithExpectedSize(ALL_KOJI_BUILDS_SIZE);
this.allKojiBuilds = new HashMap<>(); // FIXME
this.buildFinderUtils = new BuildFinderUtils(config, analyzer, session);
this.pncBuildFinder = new PncBuildFinder(pncclient, buildFinderUtils, config);

Expand All @@ -167,8 +181,8 @@ public BuildFinder(
}
}

this.foundChecksums = new HashMap<>();
this.notFoundChecksums = new HashMap<>();
this.foundChecksums = Maps.newHashMapWithExpectedSize(FOUND_CHECKSUMS_SIZE);
this.notFoundChecksums = Maps.newHashMapWithExpectedSize(NOT_FOUND_CHECKSUMS_SIZE);

initBuilds();
}
Expand All @@ -182,7 +196,7 @@ public static String getBuildsFilename() {
}

private void initBuilds() {
builds = new HashMap<>();
builds = Maps.newHashMapWithExpectedSize(BUILDS_SIZE);
KojiBuild build = BuildFinderUtils.createKojiBuildZero();
builds.put(new BuildSystemInteger(0), build);
}
Expand Down Expand Up @@ -948,7 +962,6 @@ public Map<BuildSystemInteger, KojiBuild> findBuilds(Map<Checksum, Collection<St
* Find the optional scmSourceZip, projectSourceZip and patchesZip and them to each archive
*/
List<KojiArchiveInfo> archivesToUpdate = new ArrayList<>(3 * archiveBuilds.size());

Collection<KojiBuild> values = allKojiBuilds.values();

for (KojiBuild build : values) {
Expand Down Expand Up @@ -1000,7 +1013,6 @@ public Map<BuildSystemInteger, KojiBuild> findBuilds(Map<Checksum, Collection<St
LOGGER.debug("Got empty archive list for checksum: {}", green(checksum));
markNotFound(entry);
} else {

KojiArchiveInfo archive;
Integer buildId;
String archiveFilenames;
Expand Down Expand Up @@ -1048,8 +1060,10 @@ public Map<BuildSystemInteger, KojiBuild> findBuilds(Map<Checksum, Collection<St

BuildSystemInteger buildSystemBuildId = new BuildSystemInteger(buildId, BuildSystem.koji);
KojiBuild build = builds.get(buildSystemBuildId);

if (build == null) {
build = allKojiBuilds.get(buildId);

if (build != null) {
builds.put(buildSystemBuildId, build);

Expand All @@ -1064,6 +1078,7 @@ public Map<BuildSystemInteger, KojiBuild> findBuilds(Map<Checksum, Collection<St
}
}
}

if (build != null) {
// It's ok to not create a new build for the same local archive if it already exists, but we
// always need to mark the checksum as found. This handles the scenario where there is a file
Expand Down Expand Up @@ -1238,11 +1253,14 @@ public void outputToFile() throws IOException {
@Override
public Map<BuildSystemInteger, KojiBuild> call() throws KojiClientException {
Instant startTime = Instant.now();
MultiValuedMap<Checksum, String> localchecksumMap = new ArrayListValuedHashMap<>();
Collection<Checksum> checksums = new HashSet<>();
int initialMapCapacity;
int initialListCapacity;
MultiValuedMap<Checksum, String> localchecksumMap = new ArrayListValuedHashMap<>(CHECKSUMS_SIZE); // TODO: fix
// size
Collection<Checksum> checksums = Sets.newHashSetWithExpectedSize(CHECKSUMS_SIZE);
Checksum checksum;
boolean finished = false;
Map<BuildSystemInteger, KojiBuild> allBuilds = new HashMap<>();
Map<BuildSystemInteger, KojiBuild> allBuilds = Maps.newHashMapWithExpectedSize(ALL_BUILDS_SIZE);

while (!finished) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ public static boolean isNotBuildZero(KojiBuild build) {

public void addFilesInError(KojiBuild buildZero) {
if (distributionAnalyzer != null) {
for (FileError fileError : distributionAnalyzer.getFileErrors()) {
Collection<FileError> fileErrors = distributionAnalyzer.getFileErrors();

for (FileError fileError : fileErrors) {
String filename = fileError.getFilename();
String extension = FilenameUtils.getExtension(filename);
List<String> archiveExtensions = config.getArchiveExtensions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ public class DistributionAnalyzer implements Callable<Map<ChecksumType, MultiVal

private static final String LICENSES_FILENAME_BASENAME = "licenses";

private static final Pattern SPACE_PATTERN = Pattern.compile("\\s+");

private static final int FILE_ERRORS_SIZE = 2;

private static final int LOCAL_FILES_SIZE = 44515;

private final List<String> inputs;

private final MultiValuedMap<String, Checksum> inverseMap;
Expand Down Expand Up @@ -186,10 +192,10 @@ public DistributionAnalyzer(List<String> inputs, BuildConfig config, BasicCacheC
green(licenseListSize));

for (ChecksumType checksumType : checksumTypesToCheck) {
map.put(checksumType, new HashSetValuedHashMap<>());
map.put(checksumType, new HashSetValuedHashMap<>()); // TODO: size
}

inverseMap = new HashSetValuedHashMap<>();
inverseMap = new HashSetValuedHashMap<>(); // TODO: size

this.cacheManager = cacheManager;

Expand All @@ -203,7 +209,7 @@ public DistributionAnalyzer(List<String> inputs, BuildConfig config, BasicCacheC

level = new AtomicInteger();
pool = Executors.newWorkStealingPool();
fileErrors = new ArrayList<>();
fileErrors = new ArrayList<>(FILE_ERRORS_SIZE);
}

private static boolean isJavaArchive(FileObject fo) {
Expand Down Expand Up @@ -571,24 +577,24 @@ private void handleFutureChecksum(Future<Set<Checksum>> future) throws IOExcepti
}

private void listChildren(FileObject fo) throws IOException {
List<FileObject> localFiles = new ArrayList<>();
List<FileObject> localFiles2 = new ArrayList<>();
List<FileObject> pomFiles = new ArrayList<>(LOCAL_FILES_SIZE);
List<FileObject> localFiles = new ArrayList<>(LOCAL_FILES_SIZE);

try {
FileExtensionSelector pomSelector = new FileExtensionSelector("pom");
fo.findFiles(pomSelector, true, localFiles);
fo.findFiles(new InvertIncludeFileSelector(pomSelector), true, localFiles2);
localFiles.addAll(localFiles2);
int numChildren = localFiles.size();
fo.findFiles(pomSelector, true, pomFiles);
fo.findFiles(new InvertIncludeFileSelector(pomSelector), true, localFiles);
pomFiles.addAll(localFiles);
int numChildren = pomFiles.size();
Iterable<Future<Set<Checksum>>> futures;
Collection<Callable<Set<Checksum>>> tasks = new ArrayList<>(numChildren);

if (isMainJar(fo)) {
List<LicenseInfo> licenseInfos = addLicensesFromJar(fo, localFiles);
List<LicenseInfo> licenseInfos = addLicensesFromJar(fo, pomFiles);
putLicenses(Utils.normalizePath(fo, root), licenseInfos);
}

for (FileObject file : localFiles) {
for (FileObject file : pomFiles) {
if (file.isFile()) {
if (!checksumTypesToCheck.isEmpty()) {
if (includeFile(file)) {
Expand Down Expand Up @@ -650,7 +656,7 @@ private void listChildren(FileObject fo) throws IOException {
}
}
} finally {
for (FileObject file : localFiles2) {
for (FileObject file : localFiles) {
file.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,29 @@
package org.jboss.pnc.build.finder.core;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.jboss.pnc.build.finder.koji.KojiBuild;

import com.google.common.collect.Maps;

/**
* Container holding results of the findBuilds methods
*
* @author Jakub Bartecek
*/
public class FindBuildsResult {
private static final int FOUND_BUILDS_SIZE = 755;

private static final int NOT_FOUND_CHECKSUMS_SIZE = 5174;

private final Map<BuildSystemInteger, KojiBuild> foundBuilds;

private final Map<Checksum, Collection<String>> notFoundChecksums;

public FindBuildsResult() {
this.foundBuilds = new HashMap<>();
this.notFoundChecksums = new HashMap<>();
this.foundBuilds = Maps.newHashMapWithExpectedSize(FOUND_BUILDS_SIZE);
this.notFoundChecksums = Maps.newHashMapWithExpectedSize(NOT_FOUND_CHECKSUMS_SIZE);
}

public FindBuildsResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
public class PncBuildFinder {
private static final Logger LOGGER = LoggerFactory.getLogger(PncBuildFinder.class);

private static final int PNC_BUILDS_SIZE = 774;

private final long concurrentMapParallelismThreshold;

private final PncClient pncClient;
Expand Down Expand Up @@ -184,7 +186,7 @@ private Set<EnhancedArtifact> lookupArtifactsInPnc(ConcurrentHashMap<Checksum, C
* @return A map pncBuildId,pncBuild
*/
private static ConcurrentHashMap<String, PncBuild> groupArtifactsAsPncBuilds(Iterable<EnhancedArtifact> artifacts) {
ConcurrentHashMap<String, PncBuild> pncBuilds = new ConcurrentHashMap<>();
ConcurrentHashMap<String, PncBuild> pncBuilds = new ConcurrentHashMap<>(PNC_BUILDS_SIZE);
Build buildZero = Build.builder().id(BUILD_ID_ZERO).build();

artifacts.forEach(artifact -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
public final class Utils {
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);

private static final int ERROR_MESSAGE_SIZE = 483;

private static final String PROPERTIES_FILE = "build-finder.properties";

private static final Properties PROPERTIES;
Expand Down Expand Up @@ -188,7 +190,7 @@ public static void printBanner() {
}

public static String getAllErrorMessages(Throwable t) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(ERROR_MESSAGE_SIZE);
Throwable t2 = t;
String message = t2.getMessage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public class KojiBuild {

private static final String TASK_METHOD_MAVEN = "maven";

private static final int ARCHIVES_SIZE = 4436;

private static final int DUPLICATE_ARCHIVES_SIZE = 14;

private KojiBuildInfo buildInfo;

private KojiTaskInfo taskInfo;
Expand All @@ -61,14 +65,14 @@ public class KojiBuild {
private transient List<KojiArchiveInfo> duplicateArchives;

public KojiBuild() {
archives = new ArrayList<>();
duplicateArchives = new ArrayList<>();
archives = new ArrayList<>(ARCHIVES_SIZE);
duplicateArchives = new ArrayList<>(DUPLICATE_ARCHIVES_SIZE);
}

public KojiBuild(KojiBuildInfo buildInfo) {
this.buildInfo = buildInfo;
archives = new ArrayList<>();
duplicateArchives = new ArrayList<>();
archives = new ArrayList<>(ARCHIVES_SIZE);
duplicateArchives = new ArrayList<>(DUPLICATE_ARCHIVES_SIZE);
}

public KojiBuild(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class EnhancedArtifact {
private Collection<String> fileNames;

public EnhancedArtifact() {
fileNames = new ArrayList<>();
fileNames = new ArrayList<>(1);
}

public EnhancedArtifact(Artifact artifact, Checksum checksum, Collection<String> fileNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.fasterxml.jackson.annotation.JsonIgnore;

public class PncBuild {
private static final int BUILT_ARTIFACTS_SIZE = 2498;

private Build build;

private BuildPushResult buildPushResult;
Expand All @@ -35,12 +37,12 @@ public class PncBuild {
private List<EnhancedArtifact> builtArtifacts;

public PncBuild() {
this.builtArtifacts = new ArrayList<>();
this.builtArtifacts = new ArrayList<>(BUILT_ARTIFACTS_SIZE);
}

public PncBuild(Build build) {
this.build = build;
this.builtArtifacts = new ArrayList<>();
this.builtArtifacts = new ArrayList<>(BUILT_ARTIFACTS_SIZE);
}

public PncBuild(
Expand Down
Loading

0 comments on commit 6c2df6f

Please sign in to comment.