get rid of allocations in getVFileName()

This commit is contained in:
Alexey Kudravtsev
2018-03-06 13:05:43 +03:00
parent 6a68609af0
commit af2675883b
2 changed files with 11 additions and 6 deletions
@@ -15,7 +15,6 @@
*/
package com.intellij.openapi.vfs.newvfs.impl;
import com.intellij.openapi.util.ThrowableComputable;
import com.intellij.openapi.vfs.newvfs.persistent.FSRecords;
import com.intellij.util.IntSLRUCache;
import com.intellij.util.containers.IntObjectLinkedMap;
@@ -81,8 +80,14 @@ public class FileNameCache {
private static final AtomicInteger ourQueries = new AtomicInteger();
private static final AtomicInteger ourMisses = new AtomicInteger();
@FunctionalInterface
public interface NameComputer {
String compute(int id) throws IOException;
}
@NotNull
public static CharSequence getVFileName(int nameId, @NotNull ThrowableComputable<String,IOException> computeName) throws IOException {
public static CharSequence getVFileName(int nameId, @NotNull NameComputer computeName) throws IOException {
assert nameId > 0;
if (ourTrackStats) {
@@ -113,7 +118,7 @@ public class FileNameCache {
entry = cache.getCachedEntry(nameId);
}
if (entry == null) {
entry = cacheData(computeName.compute(), nameId, stripe);
entry = cacheData(computeName.compute(nameId), nameId, stripe);
}
ourArrayCache[l1] = entry;
return entry.value;
@@ -121,7 +126,7 @@ public class FileNameCache {
@NotNull
public static CharSequence getVFileName(int nameId) {
try {
return getVFileName(nameId, () -> FSRecords.getNameByNameId(nameId));
return getVFileName(nameId, FSRecords::getNameByNameId);
}
catch (IOException e) {
throw new RuntimeException(e); // actually will be caught in getNameByNameId
@@ -903,7 +903,7 @@ public class FSRecords {
int id = DataInputOutputUtil.readINT(input) + prevId;
prevId = id;
int nameId = doGetNameId(id);
result[i] = new NameId(id, nameId, FileNameCache.getVFileName(nameId, ()->doGetNameByNameId(nameId)));
result[i] = new NameId(id, nameId, FileNameCache.getVFileName(nameId, FSRecords::doGetNameByNameId));
}
return result;
}
@@ -1080,7 +1080,7 @@ public class FSRecords {
@NotNull
private static CharSequence doGetNameSequence(int id) throws IOException {
final int nameId = getRecordInt(id, NAME_OFFSET);
return nameId == 0 ? "" : FileNameCache.getVFileName(nameId, ()->doGetNameByNameId(nameId));
return nameId == 0 ? "" : FileNameCache.getVFileName(nameId, FSRecords::doGetNameByNameId);
}
public static String getNameByNameId(int nameId) {