Skip to content

Commit

Permalink
Optimize map access in Hashtable
Browse files Browse the repository at this point in the history
  • Loading branch information
rPraml committed Sep 23, 2024
1 parent 484fc3d commit 972f5e2
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions rhino/src/main/java/org/mozilla/javascript/Hashtable.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,25 @@ public int size() {
public void put(Object key, Object value) {
final Entry nv = new Entry(key, value);

if (!map.containsKey(nv)) {
// New value -- insert to end of doubly-linked list
map.put(nv, nv);
if (first == null) {
first = last = nv;
} else {
last.next = nv;
nv.prev = last;
last = nv;
}
} else {
// Update the existing value and keep it in the same place in the list
map.get(nv).value = value;
}
map.compute(
nv,
(k, existing) -> {
if (existing == null) {
// New value -- insert to end of doubly-linked list
if (first == null) {
first = last = nv;
} else {
last.next = nv;
nv.prev = last;
last = nv;
}
return nv;
} else {
// Update the existing value and keep it in the same place in the list
existing.value = value;
return existing;
}
});
}

/**
Expand Down

0 comments on commit 972f5e2

Please sign in to comment.