cache live template context types to avoid allocating them like crazy during save

This commit is contained in:
peter
2017-02-24 12:42:12 +01:00
parent 470a8d36f4
commit b69f3cb3d3
3 changed files with 22 additions and 4 deletions
@@ -21,6 +21,8 @@ import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NullableLazyValue;
import com.intellij.openapi.util.VolatileNullableLazyValue;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -36,7 +38,7 @@ public abstract class TemplateContextType {
private final String myContextId;
@NotNull
private final String myPresentableName;
private final Class<? extends TemplateContextType> myBaseContextType;
private final NullableLazyValue<TemplateContextType> myBaseContextType;
protected TemplateContextType(@NotNull @NonNls String id, @NotNull String presentableName) {
this(id, presentableName, EverywhereContextType.class);
@@ -47,7 +49,7 @@ public abstract class TemplateContextType {
@Nullable Class<? extends TemplateContextType> baseContextType) {
myContextId = id;
myPresentableName = presentableName;
myBaseContextType = baseContextType;
myBaseContextType = VolatileNullableLazyValue.createValue(() -> baseContextType == null ? null : EP_NAME.findExtension(baseContextType));
}
@NotNull
@@ -77,7 +79,7 @@ public abstract class TemplateContextType {
@Nullable
public TemplateContextType getBaseContextType() {
return myBaseContextType != null ? EP_NAME.findExtension(myBaseContextType) : null;
return myBaseContextType.getValue();
}
public Document createDocument(CharSequence text, Project project) {
@@ -49,6 +49,7 @@ import org.jetbrains.annotations.TestOnly;
import java.util.*;
public class TemplateManagerImpl extends TemplateManager implements Disposable {
private static final TemplateContextType[] ourContextTypes = Extensions.getExtensions(TemplateContextType.EP_NAME);
private final Project myProject;
private boolean myTemplateTesting;
@@ -512,7 +513,7 @@ public class TemplateManagerImpl extends TemplateManager implements Disposable {
}
public static TemplateContextType[] getAllContextTypes() {
return Extensions.getExtensions(TemplateContextType.EP_NAME);
return ourContextTypes;
}
@Override
@@ -15,6 +15,7 @@
*/
package com.intellij.openapi.util;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
@@ -42,4 +43,18 @@ public abstract class VolatileNullableLazyValue<T> extends NullableLazyValue<T>
}
return value;
}
@SuppressWarnings("MethodOverridesStaticMethodOfSuperclass")
@NotNull
public static <T> VolatileNullableLazyValue<T> createValue(@NotNull final Factory<T> value) {
return new VolatileNullableLazyValue<T>() {
@Nullable
@Override
protected T compute() {
return value.create();
}
};
}
}