public
Last active

Caliper Benchmark for getContainerOfType

  • Download Gist
Benchmark.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.xbase.XAbstractFeatureCall;
import org.eclipse.xtext.xbase.XFeatureCall;
import org.eclipse.xtext.xbase.XMemberFeatureCall;
import org.eclipse.xtext.xbase.XNullLiteral;
import org.eclipse.xtext.xbase.XUnaryOperation;
import org.eclipse.xtext.xbase.XbaseFactory;
 
import com.google.caliper.Param;
import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
 
public class ContainerByTypeBenchmark extends SimpleBenchmark {
static class Iter extends AbstractIterator<EObject> {
 
private EObject current;
 
Iter(EObject seed) {
this.current = seed;
}
@Override
protected EObject computeNext() {
if (current == null)
return endOfData();
EObject result = current;
current = current.eContainer();
return result;
}
}
enum ContainerByType {
Recursive {
 
@SuppressWarnings("unchecked")
@Override
<T> T getContainerOfType(EObject object, Class<T> clazz)
throws Exception {
if (clazz.isAssignableFrom(object.getClass()))
return (T) object;
if (object.eContainer() != null)
return getContainerOfType(object.eContainer(), clazz);
return null;
}
 
},
Iterator {
 
@Override
<T> T getContainerOfType(EObject object, Class<T> clazz)
throws Exception {
Iter iter = new Iter(object);
UnmodifiableIterator<T> filtered = Iterators.filter(iter, clazz);
return Iterators.getNext(filtered, null);
}
 
},
Loop {
 
@SuppressWarnings("unchecked")
@Override
<T> T getContainerOfType(EObject object, Class<T> clazz)
throws Exception {
for(EObject e = object; e != null; e = e.eContainer()) {
if (clazz.isInstance(e)) {
return (T) e;
}
}
return null;
}
 
 
};
abstract <T> T getContainerOfType(EObject object, Class<T> clazz) throws Exception;
}
@Param
private ContainerByType testCase;
 
@Param({ "0", "10", "100", "1000" })
private int containers;
private EObject object;
@Override
protected void setUp() throws Exception {
XNullLiteral leaf = XbaseFactory.eINSTANCE.createXNullLiteral();
XFeatureCall root = XbaseFactory.eINSTANCE.createXFeatureCall();
XMemberFeatureCall intermediate = XbaseFactory.eINSTANCE.createXMemberFeatureCall();
XAbstractFeatureCall current = root;
for(int i = 0; i < containers; i++) {
XAbstractFeatureCall between = XbaseFactory.eINSTANCE.createXBinaryOperation();
current.setImplicitReceiver(between);
current = between;
}
current.setImplicitFirstArgument(intermediate);
current = intermediate;
for(int i = 0; i < containers; i++) {
XAbstractFeatureCall between = XbaseFactory.eINSTANCE.createXBinaryOperation();
current.setImplicitFirstArgument(between);
current = between;
}
current.setImplicitReceiver(leaf);
object = leaf;
}
 
public int timeRoot(int count) throws Exception {
int result = 0;
for (int i = 0; i < count; i++) {
result ^= testCase.getContainerOfType(object, XFeatureCall.class).eClass().getClassifierID();
}
return result;
}
public int timeNotFound(int count) throws Exception {
int result = 0;
for (int i = 0; i < count; i++) {
if (testCase.getContainerOfType(object, XUnaryOperation.class) != null)
result++;
}
return result;
}
public int timeLeaf(int count) throws Exception {
int result = 0;
for (int i = 0; i < count; i++) {
result ^= testCase.getContainerOfType(object, XNullLiteral.class).eClass().getClassifierID();
}
return result;
}
public int timeIntermediate(int count) throws Exception {
int result = 0;
for (int i = 0; i < count; i++) {
result ^= testCase.getContainerOfType(object, XMemberFeatureCall.class).eClass().getClassifierID();
}
return result;
}
 
public static void main(String[] args) throws Exception {
Runner.main(ContainerByTypeBenchmark.class, args);
}
 
}
Original.java
Java
1 2 3 4 5 6 7
public static <T extends EObject> T getContainerOfType(EObject ele, Class<T> type) {
if (type.isAssignableFrom(ele.getClass()))
return (T) ele;
if (ele.eContainer() != null)
return getContainerOfType(ele.eContainer(), type);
return null;
}
Rewritten.java
Java
1 2 3 4 5 6 7 8 9
public static <T extends EObject> T getContainerOfType(EObject object, Class<T> clazz)
throws Exception {
for(EObject e = object; e != null; e = e.eContainer()) {
if (clazz.isInstance(e)) {
return (T) e;
}
}
return null;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.