reduce GC a little bit, don't use ContainerUtil

GitOrigin-RevId: f05f5f0b4a381eed5fda4b5780c028de4afe4b5b
This commit is contained in:
Vladimir Krivosheev
2024-09-10 08:13:05 +02:00
committed by intellij-monorepo-bot
parent e2b0847717
commit edf084acb2

View File

@@ -1,8 +1,6 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.jps.incremental.artifacts;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.io.DataExternalizer;
import com.intellij.util.io.IOUtil;
import org.jetbrains.annotations.NotNull;
@@ -12,13 +10,16 @@ import org.jetbrains.jps.incremental.storage.AbstractStateStorage;
import org.jetbrains.jps.incremental.storage.PathStringDescriptor;
import java.io.*;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Stores source paths for each output path. If a source file or an output file is located in a jar file the path to the jar file is stored.
* Stores source paths for each output path.
* If a source file or an output file is located in a jar file, the path to the jar file is stored.
*/
public final class ArtifactOutputToSourceMapping extends AbstractStateStorage<String, List<ArtifactOutputToSourceMapping.SourcePathAndRootIndex>> {
public final class ArtifactOutputToSourceMapping
extends AbstractStateStorage<String, List<ArtifactOutputToSourceMapping.SourcePathAndRootIndex>> {
private final PathRelativizerService myRelativizer;
public ArtifactOutputToSourceMapping(File storePath, PathRelativizerService relativizer) throws IOException {
@@ -28,16 +29,16 @@ public final class ArtifactOutputToSourceMapping extends AbstractStateStorage<St
@Override
public void update(String path, @Nullable List<SourcePathAndRootIndex> state) throws IOException {
super.update(normalizePath(path), state != null ? normalizePaths(state) : null);
super.update(normalizePath(path), state == null ? null : normalizePaths(state));
}
@Override
public void appendData(String path, List<SourcePathAndRootIndex> data) throws IOException {
super.appendData(normalizePath(path), data != null ? normalizePaths(data) : null);
super.appendData(normalizePath(path), data == null ? null : normalizePaths(data));
}
public void appendData(String outputPath, int rootIndex, String sourcePath) throws IOException {
super.appendData(normalizePath(outputPath), Collections.singletonList(new SourcePathAndRootIndex(normalizePath(sourcePath), rootIndex)));
super.appendData(normalizePath(outputPath), List.of(new SourcePathAndRootIndex(normalizePath(sourcePath), rootIndex)));
}
@Override
@@ -48,7 +49,21 @@ public final class ArtifactOutputToSourceMapping extends AbstractStateStorage<St
@Override
public @Nullable List<SourcePathAndRootIndex> getState(String path) throws IOException {
List<SourcePathAndRootIndex> list = super.getState(normalizePath(path));
return list != null ? ContainerUtil.map(list, it -> new SourcePathAndRootIndex(myRelativizer.toFull(it.myPath), it.myRootIndex)) : null;
if (list == null) {
return null;
}
else {
if (list.isEmpty()) {
return List.of();
}
SourcePathAndRootIndex[] result = new SourcePathAndRootIndex[list.size()];
for (int i = 0; i < list.size(); i++) {
ArtifactOutputToSourceMapping.SourcePathAndRootIndex t = list.get(i);
result[i] = new SourcePathAndRootIndex(myRelativizer.toFull(t.myPath), t.myRootIndex);
}
return Arrays.asList(result);
}
}
private String normalizePath(@NotNull String path) {
@@ -56,9 +71,12 @@ public final class ArtifactOutputToSourceMapping extends AbstractStateStorage<St
}
private List<SourcePathAndRootIndex> normalizePaths(@NotNull List<SourcePathAndRootIndex> state) {
List<SourcePathAndRootIndex> normalizePathList = new SmartList<>();
state.forEach(it -> normalizePathList.add(new SourcePathAndRootIndex(normalizePath(it.myPath), it.myRootIndex)));
return normalizePathList;
SourcePathAndRootIndex[] result = new SourcePathAndRootIndex[state.size()];
for (int i = 0, size = state.size(); i < size; i++) {
SourcePathAndRootIndex it = state.get(i);
result[i] = new SourcePathAndRootIndex(normalizePath(it.myPath), it.myRootIndex);
}
return Arrays.asList(result);
}
public static final class SourcePathAndRootIndex {
@@ -90,11 +108,11 @@ public final class ArtifactOutputToSourceMapping extends AbstractStateStorage<St
@Override
public List<SourcePathAndRootIndex> read(@NotNull DataInput in) throws IOException {
List<SourcePathAndRootIndex> result = new SmartList<>();
final DataInputStream stream = (DataInputStream)in;
List<SourcePathAndRootIndex> result = new ArrayList<>();
DataInputStream stream = (DataInputStream)in;
while (stream.available() > 0) {
final String path = IOUtil.readUTF(stream);
final int index = stream.readInt();
String path = IOUtil.readUTF(stream);
int index = stream.readInt();
result.add(new SourcePathAndRootIndex(path, index));
}
return result;