Optimize KClass creation from Class instance
Don't create unnecessary iterator every time in HashPMap.get (see https://github.com/hrldcpr/pcollections/pull/41). Also fix a warning and remove misleading comment
This commit is contained in:
@@ -34,7 +34,7 @@ internal fun <T : Any> getOrCreateKotlinClass(jClass: Class<T>): KClassImpl<T> {
|
|||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
val kClass = cached.get() as KClassImpl<T>?
|
val kClass = cached.get() as KClassImpl<T>?
|
||||||
if (kClass?.jClass == jClass) {
|
if (kClass?.jClass == jClass) {
|
||||||
return kClass!!
|
return kClass
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (cached != null) {
|
else if (cached != null) {
|
||||||
@@ -51,7 +51,6 @@ internal fun <T : Any> getOrCreateKotlinClass(jClass: Class<T>): KClassImpl<T> {
|
|||||||
// This is the most unlikely case: we found a cached array of references of length at least 2 (can't be 1 because
|
// This is the most unlikely case: we found a cached array of references of length at least 2 (can't be 1 because
|
||||||
// the single element would be cached instead), and none of those classes is the one we're looking for
|
// the single element would be cached instead), and none of those classes is the one we're looking for
|
||||||
val size = cached.size
|
val size = cached.size
|
||||||
// Don't use Array constructor because it creates a lambda
|
|
||||||
val newArray = arrayOfNulls<WeakReference<KClassImpl<*>>>(size + 1)
|
val newArray = arrayOfNulls<WeakReference<KClassImpl<*>>>(size + 1)
|
||||||
// Don't use Arrays.copyOf because it works reflectively
|
// Don't use Arrays.copyOf because it works reflectively
|
||||||
System.arraycopy(cached, 0, newArray, 0, size)
|
System.arraycopy(cached, 0, newArray, 0, size)
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ final class ConsPStack<E> implements Iterable<E> {
|
|||||||
return (ConsPStack<E>) EMPTY;
|
return (ConsPStack<E>) EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final E first;
|
final E first;
|
||||||
private final ConsPStack<E> rest;
|
final ConsPStack<E> rest;
|
||||||
private final int size;
|
private final int size;
|
||||||
|
|
||||||
private ConsPStack() { // EMPTY constructor
|
private ConsPStack() { // EMPTY constructor
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public final class HashPMap<K, V> {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@NotNull
|
@NotNull
|
||||||
public static <K, V> HashPMap<K, V> empty() {
|
public static <K, V> HashPMap<K, V> empty() {
|
||||||
return (HashPMap<K, V>) HashPMap.EMPTY;
|
return (HashPMap<K, V>) EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final IntTreePMap<ConsPStack<MapEntry<K, V>>> intMap;
|
private final IntTreePMap<ConsPStack<MapEntry<K, V>>> intMap;
|
||||||
@@ -49,9 +49,12 @@ public final class HashPMap<K, V> {
|
|||||||
|
|
||||||
public V get(Object key) {
|
public V get(Object key) {
|
||||||
ConsPStack<MapEntry<K, V>> entries = getEntries(key.hashCode());
|
ConsPStack<MapEntry<K, V>> entries = getEntries(key.hashCode());
|
||||||
for (MapEntry<K, V> entry : entries)
|
while (entries != null && entries.size() > 0) {
|
||||||
|
MapEntry<K, V> entry = entries.first;
|
||||||
if (entry.key.equals(key))
|
if (entry.key.equals(key))
|
||||||
return entry.value;
|
return entry.value;
|
||||||
|
entries = entries.rest;
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,9 +89,11 @@ public final class HashPMap<K, V> {
|
|||||||
|
|
||||||
private static <K, V> int keyIndexIn(ConsPStack<MapEntry<K, V>> entries, Object key) {
|
private static <K, V> int keyIndexIn(ConsPStack<MapEntry<K, V>> entries, Object key) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (MapEntry<K, V> entry : entries) {
|
while (entries != null && entries.size() > 0) {
|
||||||
|
MapEntry<K, V> entry = entries.first;
|
||||||
if (entry.key.equals(key))
|
if (entry.key.equals(key))
|
||||||
return i;
|
return i;
|
||||||
|
entries = entries.rest;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user