remove InstanceofCheckerGenerator since JMH shows no advantages of "instanceof" vs Class#isInstance (IDEA-CR-14734)

This commit is contained in:
peter
2016-10-20 09:18:51 +02:00
parent 74ae9731a8
commit ddb7a73994
5 changed files with 2 additions and 141 deletions
-1
View File
@@ -16,6 +16,5 @@
<orderEntry type="module" module-name="platform-resources-en" exported="" />
<orderEntry type="library" name="automaton" level="project" />
<orderEntry type="library" name="CGLIB" level="project" />
<orderEntry type="library" name="ASM" level="project" />
</component>
</module>
@@ -15,9 +15,7 @@
*/
package com.intellij.patterns;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Key;
import com.intellij.util.InstanceofCheckerGenerator;
import com.intellij.util.PairProcessor;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NonNls;
@@ -40,9 +38,8 @@ public abstract class ObjectPattern<T, Self extends ObjectPattern<T, Self>> impl
protected ObjectPattern(final Class<T> aClass) {
this(new InitialPatternCondition<T>(aClass) {
final Condition<Object> checker = InstanceofCheckerGenerator.getInstance().getInstanceofChecker(aClass);
public boolean accepts(@Nullable final Object o, final ProcessingContext context) {
return checker.value(o);
return aClass.isInstance(o);
}
});
}
@@ -1,129 +0,0 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.util;
import com.intellij.openapi.util.Condition;
import com.intellij.util.containers.ConcurrentFactoryMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.org.objectweb.asm.ClassWriter;
import org.jetbrains.org.objectweb.asm.MethodVisitor;
import org.jetbrains.org.objectweb.asm.Opcodes;
import org.jetbrains.org.objectweb.asm.Type;
import java.lang.reflect.Modifier;
/**
* @author peter
*/
public class InstanceofCheckerGenerator {
private static final InstanceofCheckerGenerator ourInstance;
static {
try {
ourInstance = new InstanceofCheckerGenerator();
}
catch (Throwable e) {
throw new RuntimeException(e);
}
}
public static InstanceofCheckerGenerator getInstance() {
return ourInstance;
}
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private final ConcurrentFactoryMap<Class, Condition<Object>> myCache = new ConcurrentFactoryMap<Class, Condition<Object>>() {
@Override
protected Condition<Object> create(final Class key) {
if (key.isAnonymousClass() || Modifier.isPrivate(key.getModifiers())) {
return new Condition<Object>() {
@Override
public boolean value(Object o) {
return key.isInstance(o);
}
};
}
String name = "com.intellij.util.InstanceofChecker$$$$$" + key.getName().replace('.', '$');
//noinspection unchecked
return (Condition<Object>)ReflectionUtil.newInstance(obtainClass(key, name, generateConditionClass(key, name)));
}
};
private synchronized Class obtainClass(Class checkedClass, String name, byte[] bytes) {
ClassLoader loader = checkedClass.getClassLoader();
if (loader == null) loader = InstanceofCheckerGenerator.class.getClassLoader();
try {
return loader.loadClass(name);
}
catch (ClassNotFoundException ignore) {
}
try {
return ReflectionUtil.defineClass(name, bytes, loader);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
@NotNull
public Condition<Object> getInstanceofChecker(final Class<?> someClass) {
return myCache.get(someClass);
}
private static String toInternalName(Class<?> someClass) {
return toInternalName(someClass.getName());
}
@NotNull
private static String toInternalName(String name) {
return name.replace('.', '/');
}
private static byte[] generateConditionClass(Class<?> checkedClass, final String generatedName) {
ClassWriter cv = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
cv.visit(Opcodes.V1_2, Modifier.PUBLIC, toInternalName(generatedName), null, toInternalName(Object.class), new String[]{toInternalName(Condition.class)});
defaultConstructor(cv);
conditionValue(checkedClass, cv);
cv.visitEnd();
return cv.toByteArray();
}
private static void defaultConstructor(ClassWriter cv) {
MethodVisitor mv = cv.visitMethod(Modifier.PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, toInternalName(Object.class), "<init>", "()V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
private static void conditionValue(Class<?> checkedClass, ClassWriter cv) {
MethodVisitor mv = cv.visitMethod(Modifier.PUBLIC, "value", "(L" + toInternalName(Object.class) + ";)Z", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitTypeInsn(Opcodes.INSTANCEOF, Type.getType(checkedClass).getInternalName());
mv.visitInsn(Opcodes.IRETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
}
@@ -15,17 +15,13 @@
*/
package com.intellij.openapi.util;
import com.intellij.util.InstanceofCheckerGenerator;
/**
* @author peter
*/
public class ClassConditionKey<T> {
private final Condition<Object> myCondition;
private final Class<T> myConditionClass;
private ClassConditionKey(Class<T> aClass) {
myCondition = InstanceofCheckerGenerator.getInstance().getInstanceofChecker(aClass);
myConditionClass = aClass;
}
@@ -34,7 +30,7 @@ public class ClassConditionKey<T> {
}
public boolean isInstance(Object o) {
return myCondition.value(o);
return myConditionClass.isInstance(o);
}
@Override
@@ -151,8 +151,6 @@
serviceImplementation="com.intellij.reporting.FreezeLoggerImpl"/>
<applicationService serviceInterface="com.intellij.featureStatistics.ProductivityFeaturesRegistry"
serviceImplementation="com.intellij.featureStatistics.ProductivityFeaturesRegistryImpl"/>
<applicationService serviceInterface="com.intellij.util.InstanceofCheckerGenerator"
serviceImplementation="com.intellij.util.InstanceofCheckerGenerator"/>
<applicationService serviceInterface="com.intellij.internal.psiView.PsiViewerSettings"
serviceImplementation="com.intellij.internal.psiView.PsiViewerSettings"/>