Skip to content

Commit f2db8b4

Browse files
committed
Caching the result of 'isInternalImplementation' so that reflection doesn't get invoked on every subscribe
1 parent 2c4ab1a commit f2db8b4

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Arrays;
2222
import java.util.Comparator;
2323
import java.util.List;
24+
import java.util.concurrent.ConcurrentHashMap;
2425
import java.util.concurrent.Future;
2526
import java.util.concurrent.TimeUnit;
2627

@@ -128,6 +129,8 @@
128129
*/
129130
public class Observable<T> {
130131

132+
private final static ConcurrentHashMap<Class, Boolean> internalClassMap = new ConcurrentHashMap<Class, Boolean>();
133+
131134
/**
132135
* Executed when 'subscribe' is invoked.
133136
*/
@@ -4522,11 +4525,21 @@ private boolean isInternalImplementation(Object o) {
45224525
return true;
45234526
}
45244527
// prevent double-wrapping (yeah it happens)
4525-
if (o instanceof SafeObserver)
4528+
if (o instanceof SafeObserver) {
45264529
return true;
4527-
// we treat the following package as "internal" and don't wrap it
4528-
Package p = o.getClass().getPackage(); // it can be null
4529-
return p != null && p.getName().startsWith("rx.operators");
4530+
}
4531+
4532+
Class<?> clazz = o.getClass();
4533+
if (internalClassMap.containsKey(clazz)) {
4534+
//don't need to do reflection
4535+
return internalClassMap.get(clazz);
4536+
} else {
4537+
// we treat the following package as "internal" and don't wrap it
4538+
Package p = o.getClass().getPackage(); // it can be null
4539+
Boolean isInternal = (p != null && p.getName().startsWith("rx.operators"));
4540+
internalClassMap.put(clazz, isInternal);
4541+
return isInternal;
4542+
}
45304543
}
45314544

45324545
}

0 commit comments

Comments
 (0)