Skip to content

Commit

Permalink
Only check for EmbeddedSlotMap promotion when resizing storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
aardvark179 committed Jan 17, 2025
1 parent 5762e44 commit 8e256fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
41 changes: 11 additions & 30 deletions rhino/src/main/java/org/mozilla/javascript/EmbeddedSlotMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,12 @@ public Slot modify(SlotMapOwner owner, Object key, int index, int attributes) {
}
}

// A new slot has to be inserted.
if (slots != null && slots.length > SlotMapContainer.LARGE_HASH_SIZE) {
var map = copyToNewMap(owner);
return map.modify(owner, key, index, attributes);
} else {
Slot newSlot = new Slot(key, index, attributes);
createNewSlot(newSlot);
return newSlot;
}
Slot newSlot = new Slot(key, index, attributes);
createNewSlot(owner, newSlot);
return newSlot;
}

private void createNewSlot(Slot newSlot) {
private void createNewSlot(SlotMapOwner owner, Slot newSlot) {
if (count == 0) {
// Always throw away old slots if any on empty insert.
slots = new Slot[INITIAL_SLOT_SIZE];
Expand All @@ -130,6 +124,11 @@ private void createNewSlot(Slot newSlot) {
// Check if the table is not too full before inserting.
if (4 * (count + 1) > 3 * slots.length) {
// table size must be a power of 2 -- always grow by x2!
if (count > SlotMapContainer.LARGE_HASH_SIZE) {
var newMap = new HashSlotMap(this, newSlot);
owner.setMap(newMap);
return;
}
Slot[] newSlots = new Slot[slots.length * 2];
copyTable(slots, newSlots);
slots = newSlots;
Expand Down Expand Up @@ -162,14 +161,9 @@ public <S extends Slot> S compute(

private <S extends Slot> S computeNew(
SlotMapOwner owner, Object key, int index, SlotComputer<S> c) {
// If we get here, we know we are potentially adding a new slot
if (slots != null && slots.length > SlotMapContainer.LARGE_HASH_SIZE) {
var map = copyToNewMap(owner);
return map.compute(owner, key, index, c);
}
S newSlot = c.compute(key, index, null);
if (newSlot != null) {
createNewSlot(newSlot);
createNewSlot(owner, newSlot);
}
return newSlot;
}
Expand Down Expand Up @@ -209,25 +203,12 @@ private <S extends Slot> S computeExisting(
return newSlot;
}

private HashSlotMap copyToNewMap(SlotMapOwner owner) {
var newMap = new HashSlotMap();
for (Slot n : this) {
newMap.add(owner, n);
}
owner.setMap(newMap);
return newMap;
}

@Override
public void add(SlotMapOwner owner, Slot newSlot) {
if (slots == null) {
slots = new Slot[INITIAL_SLOT_SIZE];
} else if (slots.length > SlotMapContainer.LARGE_HASH_SIZE) {
var map = copyToNewMap(owner);
map.add(owner, newSlot);
return;
}
insertNewSlot(newSlot);
createNewSlot(owner, newSlot);
}

private void insertNewSlot(Slot newSlot) {
Expand Down
8 changes: 8 additions & 0 deletions rhino/src/main/java/org/mozilla/javascript/HashSlotMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public HashSlotMap(SlotMap oldMap) {
}
}

public HashSlotMap(SlotMap oldMap, Slot newSlot) {
map = new LinkedHashMap<>(oldMap.dirtySize() + 1);
for (Slot n : oldMap) {
add(null, n.copySlot());
}
add(null, newSlot);
}

@Override
public int size() {
return map.size();
Expand Down

0 comments on commit 8e256fe

Please sign in to comment.