IDEA-337454 ID - use JDK immutable collections

GitOrigin-RevId: f996ac67129f81ed49841c51b0c4e0f35fe1df41
This commit is contained in:
Vladimir Krivosheev
2023-11-30 11:29:24 +01:00
committed by intellij-monorepo-bot
parent 4d742fabfb
commit 3cffb3fe21
3 changed files with 31 additions and 33 deletions

View File

@@ -27,14 +27,15 @@ import static com.intellij.util.containers.UtilKt.without;
*/
public class ID<K, V> extends IndexId<K,V> {
private static final Logger LOG = Logger.getInstance(ID.class);
private static final PluginId CORE_PLUGIN_ID = PluginId.getId("com.intellij");
private static volatile SimpleStringPersistentEnumerator nameToIdRegistry = new SimpleStringPersistentEnumerator(getEnumFile());
private static final Map<String, ID<?, ?>> idObjects = new ConcurrentHashMap<>();
private static final Object lock = new Object();
private static volatile Map<ID<?, ?>, PluginId> idToPluginId = Java11Shim.INSTANCE.mapOf();
private static volatile Map<ID<?, ?>, Throwable> idToRegistrationStackTrace = Java11Shim.INSTANCE.mapOf();
private static volatile Map<@NotNull ID<?, ?>, @NotNull PluginId> idToPluginId = Java11Shim.INSTANCE.mapOf();
private static volatile Map<@NotNull ID<?, ?>, @NotNull Throwable> idToRegistrationStackTrace = Java11Shim.INSTANCE.mapOf();
static final int MAX_NUMBER_OF_INDICES = Short.MAX_VALUE;
private volatile int uniqueId;
@@ -96,15 +97,12 @@ public class ID<K, V> extends IndexId<K,V> {
synchronized (lock) {
PluginId oldPluginId = idToPluginId.get(this);
assert oldPluginId == null : "ID with name '" +
name +
"' is already registered in " +
oldPluginId +
" but current caller is " +
pluginId;
assert oldPluginId == null : "ID with name '" + name +
"' is already registered in " + oldPluginId +
" but current caller is " + pluginId;
//noinspection AssignmentToStaticFieldFromInstanceMethod
idToPluginId = with(idToPluginId, this, pluginId);
idToPluginId = with(idToPluginId, this, pluginId == null ? CORE_PLUGIN_ID : pluginId);
//noinspection AssignmentToStaticFieldFromInstanceMethod
idToRegistrationStackTrace = with(idToRegistrationStackTrace, this, new Throwable());
}
@@ -143,17 +141,17 @@ public class ID<K, V> extends IndexId<K,V> {
if (checkCallerPlugin && id != null) {
PluginId actualPluginId = idToPluginId.get(id);
String actualPluginIdStr = actualPluginId == null ? "IJ Core" : actualPluginId.getIdString();
String requiredPluginIdStr = requiredPluginId == null ? "IJ Core" : requiredPluginId.getIdString();
String actualPluginIdStr = actualPluginId == null ? "" : actualPluginId.getIdString();
String requiredPluginIdStr = requiredPluginId == null ? "" : requiredPluginId.getIdString();
if (!Objects.equals(actualPluginIdStr, requiredPluginIdStr)) {
Throwable registrationStackTrace = idToRegistrationStackTrace.get(id);
String message = getInvalidIdAccessMessage(name, actualPluginIdStr, requiredPluginIdStr, registrationStackTrace);
if (registrationStackTrace != null) {
throw new AssertionError(message, registrationStackTrace);
if (registrationStackTrace == null) {
throw new AssertionError(message);
}
else {
throw new AssertionError(message);
throw new AssertionError(message, registrationStackTrace);
}
}
}

View File

@@ -6,10 +6,7 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileSystemItem;
import com.intellij.psi.PsiManager;
import com.intellij.util.ArrayUtilRt;
import com.intellij.util.Processor;
import com.intellij.util.Processors;
import com.intellij.util.SmartList;
import com.intellij.util.*;
import com.intellij.util.containers.CollectionFactory;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.HashingStrategy;
@@ -21,7 +18,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public final class FilenameIndex {
@@ -31,7 +28,7 @@ public final class FilenameIndex {
public static final ID<String, Void> NAME = ID.create("FilenameIndex");
public static @NotNull String @NotNull [] getAllFilenames(@NotNull Project project) {
Set<String> names = CollectionFactory.createSmallMemoryFootprintSet();
Set<String> names = new HashSet<>();
processAllFileNames((String s) -> {
names.add(s);
return true;
@@ -191,11 +188,14 @@ public final class FilenameIndex {
public static @NotNull Collection<VirtualFile> getAllFilesByExt(@NotNull Project project,
@NotNull String ext,
@NotNull GlobalSearchScope searchScope) {
if (ext.isEmpty()) return Collections.emptyList();
if (ext.isEmpty()) {
return Java11Shim.INSTANCE.listOf();
}
String dotExt = "." + ext;
int len = ext.length() + 1;
Set<String> names = CollectionFactory.createSmallMemoryFootprintSet();
Set<String> names = new HashSet<>();
for (String name : getAllFilenames(project)) {
int length = name.length();
if (length > len && name.substring(length - len).equalsIgnoreCase(dotExt)) {
@@ -208,7 +208,7 @@ public final class FilenameIndex {
private static @NotNull Set<VirtualFile> getVirtualFilesByNames(@NotNull Set<String> names,
@NotNull GlobalSearchScope scope,
@Nullable IdFilter filter) {
Set<VirtualFile> files = CollectionFactory.createSmallMemoryFootprintSet();
Set<VirtualFile> files = new HashSet<>();
FileBasedIndex.getInstance().processFilesContainingAnyKey(NAME, names, scope, filter, null, file -> {
files.add(file);
return true;

View File

@@ -3,9 +3,9 @@ package com.intellij.util.io;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.NioFiles;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ArrayUtilRt;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import org.jetbrains.annotations.ApiStatus;
@@ -56,8 +56,8 @@ public final class SimpleStringPersistentEnumerator implements ScannableDataEnum
* [id -> value] mapping.
* Updated with CopyOnWrite under 'this' lock.
* Since id starts with 1 (0 is reserved as NULL_ID), so index in the array is (id-1)
* For backward-compatibility reasons, it could be >1 id for the same value -- i.e. it could be
* idToValue.length > valueToId.size()
* For backward-compatibility reasons, it could be >1 id for the same value -- i.e., it could be
* `idToValue.length > valueToId.size()`
*/
private volatile String @NotNull [] idToValue;
@@ -136,11 +136,11 @@ public final class SimpleStringPersistentEnumerator implements ScannableDataEnum
return id;
}
//CopyOnWrite: it is possible to do CoW without lock, with CAS only -- but it is hard to keep consistent
// on-disk representation: it is very possible to have correct version in memory, but outdated
//CopyOnWrite: it is possible to do CoW without a lock, with CAS only -- but it is hard to keep consistent
// on-disk representation: it is very possible to have a correct version in memory, but outdated
// version stored on disk -- and additional efforts needed to prevent it.
// So locking seems to be the simpler choice: this enumerator is aimed for ~small datasets,
// i.e. total number of updates must be very limited.
// i.e., total number of updates must be very limited.
// do not use nameToId.size because enumeration file may have duplicates on different lines
int newId = idToValue.length + 1;
@@ -235,8 +235,8 @@ public final class SimpleStringPersistentEnumerator implements ScannableDataEnum
public synchronized void closeAndClean() throws IOException {
close();
valueToId = new Object2IntOpenHashMap<>();
idToValue = ArrayUtil.EMPTY_STRING_ARRAY;
FileUtil.delete(file);
idToValue = ArrayUtilRt.EMPTY_STRING_ARRAY;
NioFiles.deleteRecursively(file);
}
private void checkNotClosed() {
@@ -269,7 +269,7 @@ public final class SimpleStringPersistentEnumerator implements ScannableDataEnum
}
Object2IntMap<String> nameToIdRegistry = new Object2IntOpenHashMap<>(lines.size());
String[] idToNameRegistry = lines.isEmpty() ? ArrayUtil.EMPTY_STRING_ARRAY : new String[lines.size()];
String[] idToNameRegistry = lines.isEmpty() ? ArrayUtilRt.EMPTY_STRING_ARRAY : new String[lines.size()];
for (int i = 0; i < lines.size(); i++) {
String name = lines.get(i);
int id = i + 1;