Skip to content

HashMaps improvements #1345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test-app/runtime/src/main/cpp/ObjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ void ObjectManager::MakeRegularObjectsWeak(const set<int> &instances, DirectBuff
* so we tell java to take the JAVA objects out of strong, BUT KEEP THEM AS WEEK REFERENCES,
* so that if java needs to release them, it can, on a later stage.
* */
void ObjectManager::MakeImplObjectsWeak(const map<int, Persistent<Object> *> &instances,
void ObjectManager::MakeImplObjectsWeak(const unordered_map<int, Persistent<Object> *> &instances,
DirectBuffer &inputBuff) {
jboolean keepAsWeak = JNI_TRUE;

Expand Down
6 changes: 3 additions & 3 deletions test-app/runtime/src/main/cpp/ObjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class ObjectManager {

void MakeRegularObjectsWeak(const std::set<int>& instances, DirectBuffer& inputBuff);

void MakeImplObjectsWeak(const std::map<int, v8::Persistent<v8::Object>*>& instances, DirectBuffer& inputBuff);
void MakeImplObjectsWeak(const std::unordered_map<int, v8::Persistent<v8::Object>*>& instances, DirectBuffer& inputBuff);

void CheckWeakObjectsAreAlive(const std::vector<PersistentObjectIdPair>& instances, DirectBuffer& inputBuff, DirectBuffer& outputBuff);

Expand Down Expand Up @@ -201,7 +201,7 @@ class ObjectManager {

std::stack<GarbageCollectionInfo> m_markedForGC;

std::map<int, v8::Persistent<v8::Object>*> m_idToObject;
std::unordered_map<int, v8::Persistent<v8::Object>*> m_idToObject;

PersistentObjectIdSet m_released;

Expand All @@ -211,7 +211,7 @@ class ObjectManager {

std::set<v8::Persistent<v8::Object>*> m_visitedPOs;
std::vector<PersistentObjectIdPair> m_implObjWeak;
std::map<int, v8::Persistent<v8::Object>*> m_implObjStrong;
std::unordered_map<int, v8::Persistent<v8::Object>*> m_implObjStrong;

volatile int m_currentObjectId;

Expand Down
20 changes: 10 additions & 10 deletions test-app/runtime/src/main/java/com/tns/NativeScriptHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @param <V> the type of mapped values
*/

public class NativeScriptHashMap<K, V> extends AbstractMap<K, V> implements Cloneable, Serializable {
public class NativeScriptHashMap<K, V extends Number> extends AbstractMap<K, V> implements Cloneable, Serializable {
/**
* Min capacity (other than zero) for a HashMap. Must be a power of two
* greater than 1 (and less than 1 << 30).
Expand Down Expand Up @@ -283,7 +283,7 @@ public V get(Object key) {
HashMapEntry<K, V>[] tab = table;
for (HashMapEntry<K, V> e = tab[hash & (tab.length - 1)]; e != null; e = e.next) {
K eKey = e.key;
if (eKey == key || (e.hash == hash && key.equals(eKey))) {
if (eKey == key) {
return e.value;
}
}
Expand All @@ -306,7 +306,7 @@ public boolean containsKey(Object key) {
HashMapEntry<K, V>[] tab = table;
for (HashMapEntry<K, V> e = tab[hash & (tab.length - 1)]; e != null; e = e.next) {
K eKey = e.key;
if (eKey == key || (e.hash == hash && key.equals(eKey))) {
if (eKey == key) {
return true;
}
}
Expand Down Expand Up @@ -337,7 +337,7 @@ public boolean containsValue(Object value) {
// value is non-null
for (int i = 0; i < len; i++) {
for (HashMapEntry<K, V> e = tab[i]; e != null; e = e.next) {
if (value.equals(e.value)) {
if (value == e.value) {
return true;
}
}
Expand All @@ -363,7 +363,7 @@ public V put(K key, V value) {
int index = hash & (tab.length - 1);
for (HashMapEntry<K, V> e = tab[index]; e != null; e = e.next) {
K eKey = e.key;
if (eKey == key || (e.hash == hash && key.equals(eKey))) {
if (eKey == key) {
preModify(e);
V oldValue = e.value;
e.value = value;
Expand Down Expand Up @@ -428,7 +428,7 @@ private void constructorPut(K key, V value) {
int index = hash & (tab.length - 1);
HashMapEntry<K, V> first = tab[index];
for (HashMapEntry<K, V> e = first; e != null; e = e.next) {
if (e.hash == hash && key.equals(e.key)) {
if (key == e.key) {
e.value = value;
return;
}
Expand Down Expand Up @@ -599,7 +599,7 @@ public V remove(Object key) {
int index = hash & (tab.length - 1);
for (HashMapEntry<K, V> e = tab[index], prev = null; e != null; prev = e, e = e.next) {
K eKey = e.key;
if (eKey == key || (e.hash == hash && key.equals(eKey))) {
if (eKey == key) {
if (prev == null) {
tab[index] = e.next;
} else {
Expand Down Expand Up @@ -726,7 +726,7 @@ public final boolean equals(Object o) {
return false;
}
Entry<?, ?> e = (Entry<?, ?>) o;
return Objects.equal(e.getKey(), key) && Objects.equal(e.getValue(), value);
return key == e.getKey() && value == e.getValue();
}

@Override
Expand Down Expand Up @@ -823,7 +823,7 @@ private boolean containsMapping(Object key, Object value) {
HashMapEntry<K, V>[] tab = table;
int index = hash & (tab.length - 1);
for (HashMapEntry<K, V> e = tab[index]; e != null; e = e.next) {
if (e.hash == hash && key.equals(e.key)) {
if (key == e.key) {
return Objects.equal(value, e.value);
}
}
Expand Down Expand Up @@ -851,7 +851,7 @@ private boolean removeMapping(Object key, Object value) {
HashMapEntry<K, V>[] tab = table;
int index = hash & (tab.length - 1);
for (HashMapEntry<K, V> e = tab[index], prev = null; e != null; prev = e, e = e.next) {
if (e.hash == hash && key.equals(e.key)) {
if (key == e.key) {
if (!Objects.equal(value, e.value)) {
return false; // Map has wrong value for key
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @see HashMap
* @see WeakReference
*/
public class NativeScriptWeakHashMap<K, V> extends NativeScriptAbstractMap<K, V> implements Map<K, V> {
public class NativeScriptWeakHashMap<K, V extends Number> extends NativeScriptAbstractMap<K, V> implements Map<K, V> {

private static final int DEFAULT_SIZE = 16;

Expand Down Expand Up @@ -103,7 +103,7 @@ public boolean equals(Object other) {
}
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) other;
Object key = super.get();
return (key == null ? key == entry.getKey() : key.equals(entry.getKey())) && (value == null ? value == entry.getValue() : value.equals(entry.getValue()));
return (key == null ? key == entry.getKey() : key == entry.getKey()) && (value == null ? value == entry.getValue() : value == entry.getValue());
}

@Override
Expand Down Expand Up @@ -449,7 +449,7 @@ public V get(Object key) {
int index = (secondaryHashForObject(key) & 0x7FFFFFFF) % elementData.length;
Entry<K, V> entry = elementData[index];
while (entry != null) {
if (key.equals(entry.get())) {
if (key == entry.get()) {
return entry.value;
}
entry = entry.next;
Expand All @@ -472,7 +472,7 @@ Entry<K, V> getEntry(Object key) {
int index = (secondaryHashForObject(key) & 0x7FFFFFFF) % elementData.length;
Entry<K, V> entry = elementData[index];
while (entry != null) {
if (key.equals(entry.get())) {
if (key == entry.get()) {
return entry;
}
entry = entry.next;
Expand Down Expand Up @@ -504,7 +504,7 @@ public boolean containsValue(Object value) {
Entry<K, V> entry = elementData[i];
while (entry != null) {
K key = entry.get();
if ((key != null || entry.isNull) && value.equals(entry.value)) {
if ((key != null || entry.isNull) && value == entry.value) {
return true;
}
entry = entry.next;
Expand Down Expand Up @@ -582,7 +582,7 @@ public V put(K key, V value) {
if (key != null) {
index = (secondaryHashForObject(key) & 0x7FFFFFFF) % elementData.length;
entry = elementData[index];
while (entry != null && !key.equals(entry.get())) {
while (entry != null && key != entry.get()) {
entry = entry.next;
}
} else {
Expand Down Expand Up @@ -658,7 +658,7 @@ public V remove(Object key) {
if (key != null) {
index = (secondaryHashForObject(key) & 0x7FFFFFFF) % elementData.length;
entry = elementData[index];
while (entry != null && !key.equals(entry.get())) {
while (entry != null && key != entry.get()) {
last = entry;
entry = entry.next;
}
Expand Down