less allocations when iterating over lookup strings

This commit is contained in:
peter
2015-07-17 14:15:52 +02:00
parent f5097625fc
commit a5f53c95b4
3 changed files with 39 additions and 12 deletions
@@ -40,6 +40,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.filters.TrueFilter;
import com.intellij.util.ExceptionUtil;
import com.intellij.util.UnmodifiableIterator;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.HashMap;
import org.jetbrains.annotations.NonNls;
@@ -271,14 +272,40 @@ public class CompletionUtil {
return result;
}
public static List<String> getImmutableLookupStrings(@NotNull LookupElement element) {
try {
return ContainerUtil.newArrayList(element.getAllLookupStrings());
}
catch (ConcurrentModificationException e) {
final Attachment dump = new Attachment("threadDump.txt", ThreadDumper.dumpThreadsToString());
throw new LogEventException("Error while traversing lookup strings of " + element + " of " + element.getClass(),
ExceptionUtil.getThrowableText(e), dump);
}
public static Iterable<String> iterateLookupStrings(@NotNull final LookupElement element) {
return new Iterable<String>() {
@NotNull
@Override
public Iterator<String> iterator() {
final Iterator<String> original = element.getAllLookupStrings().iterator();
return new UnmodifiableIterator<String>(original) {
@Override
public boolean hasNext() {
try {
return super.hasNext();
}
catch (ConcurrentModificationException e) {
throw handleCME(e);
}
}
@Override
public String next() {
try {
return super.next();
}
catch (ConcurrentModificationException e) {
throw handleCME(e);
}
}
private LogEventException handleCME(ConcurrentModificationException e) {
final Attachment dump = new Attachment("threadDump.txt", ThreadDumper.dumpThreadsToString());
return new LogEventException("Error while traversing lookup strings of " + element + " of " + element.getClass(),
ExceptionUtil.getThrowableText(e), dump);
}
};
}
};
}
}
@@ -45,7 +45,7 @@ public class CamelHumpMatcher extends PrefixMatcher {
@Override
public boolean isStartMatch(LookupElement element) {
for (String s : CompletionUtil.getImmutableLookupStrings(element)) {
for (String s : CompletionUtil.iterateLookupStrings(element)) {
FList<TextRange> ranges = myCaseInsensitiveMatcher.matchingFragments(s);
if (ranges == null) continue;
if (ranges.isEmpty() || skipUnderscores(s) >= ranges.get(0).getStartOffset()) {
@@ -61,7 +61,7 @@ public class LiftShorterItemsClassifier extends Classifier<LookupElement> {
public void addElement(LookupElement added, ProcessingContext context) {
myCount++;
for (String string : CompletionUtil.getImmutableLookupStrings(added)) {
for (String string : CompletionUtil.iterateLookupStrings(added)) {
if (string.length() == 0) continue;
myElements.putValue(string, added);
@@ -97,7 +97,7 @@ public class LiftShorterItemsClassifier extends Classifier<LookupElement> {
private void calculateToLift(LookupElement element) {
boolean hasChanges = false;
for (String string : CompletionUtil.getImmutableLookupStrings(element)) {
for (String string : CompletionUtil.iterateLookupStrings(element)) {
for (int len = 1; len < string.length(); len++) {
String prefix = string.substring(0, len);
for (LookupElement shorterElement : myElements.get(prefix)) {