| /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- | |
| * | |
| * This Source Code Form is subject to the terms of the Mozilla Public | |
| * License, v. 2.0. If a copy of the MPL was not distributed with this | |
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
| package org.mozilla.javascript; | |
| /* | |
| * This class implements the SlotMap interface using an embedded hash table. This hash table | |
| * has the minimum overhead needed to get the job done. In particular, it embeds the Slot | |
| * directly into the hash table rather than creating an intermediate object, which seems | |
| * to have a measurable performance benefit. | |
| */ | |
| import java.util.Iterator; | |
| import java.util.NoSuchElementException; | |
| import java.util.Objects; | |
| public class EmbeddedSlotMap<T extends PropHolder<T>> implements SlotMap<T> { | |
| private Slot<T>[] slots; | |
| // gateways into the definition-order linked list of slots | |
| private Slot<T> firstAdded; | |
| private Slot<T> lastAdded; | |
| private int count; | |
| private boolean hasIndex = false; | |
| // initial slot array size, must be a power of 2 | |
| private static final int INITIAL_SLOT_SIZE = 4; | |
| private static final class Iter<T extends PropHolder<T>> implements Iterator<Slot<T>> { | |
| private Slot<T> next; | |
| Iter(Slot<T> slot) { | |
| next = slot; | |
| } | |
| @Override | |
| public boolean hasNext() { | |
| return next != null; | |
| } | |
| @Override | |
| public Slot<T> next() { | |
| var ret = next; | |
| if (ret == null) { | |
| throw new NoSuchElementException(); | |
| } | |
| next = next.orderedNext; | |
| return ret; | |
| } | |
| } | |
| public EmbeddedSlotMap() {} | |
| @SuppressWarnings("unchecked") | |
| public EmbeddedSlotMap(int capacity) { | |
| int n = -1 >>> Integer.numberOfLeadingZeros(capacity - 1); | |
| n = (n < 0) ? 1 : n + 1; | |
| slots = new Slot[n]; | |
| } | |
| @SuppressWarnings("unchecked") | |
| protected EmbeddedSlotMap(EmbeddedSlotMap<T> other) { | |
| slots = new Slot[other.slots.length]; | |
| for (var s : other) { | |
| insertNewSlot(s.copySlot()); | |
| } | |
| } | |
| @Override | |
| public int size() { | |
| return count; | |
| } | |
| @Override | |
| public boolean isEmpty() { | |
| return count == 0; | |
| } | |
| @Override | |
| public Iterator<Slot<T>> iterator() { | |
| return new Iter<T>(firstAdded); | |
| } | |
| /** Locate the slot with the given name or index. */ | |
| @Override | |
| public Slot<T> query(Object key, int index) { | |
| if (slots == null || (key == null && !hasIndex)) { | |
| return null; | |
| } | |
| int indexOrHash = (key != null ? key.hashCode() : index); | |
| int slotIndex = getSlotIndex(slots.length, indexOrHash); | |
| for (Slot<T> slot = slots[slotIndex]; slot != null; slot = slot.next) { | |
| if (slot.keyMatches(key, indexOrHash)) { | |
| return slot; | |
| } | |
| } | |
| return null; | |
| } | |
| /** | |
| * Locate the slot with given name or index, and create a new one if necessary. | |
| * | |
| * @param key either a String or a Symbol object that identifies the property | |
| * @param index index or 0 if slot holds property name. | |
| */ | |
| @Override | |
| public Slot<T> modify(SlotMapOwner<T> owner, Object key, int index, int attributes) { | |
| final int indexOrHash = (key != null ? key.hashCode() : index); | |
| Slot<T> slot; | |
| if (slots != null) { | |
| final int slotIndex = getSlotIndex(slots.length, indexOrHash); | |
| for (slot = slots[slotIndex]; slot != null; slot = slot.next) { | |
| if (slot.keyMatches(key, indexOrHash)) { | |
| break; | |
| } | |
| } | |
| if (slot != null) { | |
| return slot; | |
| } | |
| } | |
| var newSlot = new StandardSlot<T>(key, index, attributes); | |
| createNewSlot(owner, newSlot); | |
| return newSlot; | |
| } | |
| @SuppressWarnings("unchecked") | |
| private void createNewSlot(SlotMapOwner<T> owner, Slot<T> newSlot) { | |
| if (count == 0 && slots == null) { | |
| // Always throw away old slots if any on empty insert. | |
| slots = new Slot[INITIAL_SLOT_SIZE]; | |
| } | |
| // 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 >= SlotMapOwner.LARGE_HASH_SIZE) { | |
| promoteMap(owner, newSlot); | |
| return; | |
| } | |
| Slot<T>[] newSlots = new Slot[slots.length * 2]; | |
| copyTable(slots, newSlots); | |
| slots = newSlots; | |
| } | |
| insertNewSlot(newSlot); | |
| } | |
| protected void promoteMap(SlotMapOwner<T> owner, Slot<T> newSlot) { | |
| var newMap = new HashSlotMap<T>(this, newSlot); | |
| owner.setMap(newMap); | |
| } | |
| @Override | |
| public <S extends Slot<T>> S compute( | |
| SlotMapOwner<T> owner, | |
| CompoundOperationMap<T> compoundOp, | |
| Object key, | |
| int index, | |
| SlotComputer<S, T> c) { | |
| final int indexOrHash = (key != null ? key.hashCode() : index); | |
| if (slots != null) { | |
| Slot<T> slot; | |
| final int slotIndex = getSlotIndex(slots.length, indexOrHash); | |
| Slot<T> prev = slots[slotIndex]; | |
| for (slot = prev; slot != null; slot = slot.next) { | |
| if (slot.keyMatches(key, indexOrHash)) { | |
| break; | |
| } | |
| prev = slot; | |
| } | |
| if (slot != null) { | |
| return computeExisting(owner, compoundOp, key, index, c, slot, prev, slotIndex); | |
| } | |
| } | |
| return computeNew(owner, compoundOp, key, index, c); | |
| } | |
| private <S extends Slot<T>> S computeNew( | |
| SlotMapOwner<T> owner, | |
| CompoundOperationMap<T> compoundOp, | |
| Object key, | |
| int index, | |
| SlotComputer<S, T> c) { | |
| S newSlot = c.compute(key, index, null, compoundOp, owner); | |
| if (newSlot != null) { | |
| if (!compoundOp.touched) { | |
| createNewSlot(owner, newSlot); | |
| } else { | |
| owner.getMap().add(owner, newSlot); | |
| } | |
| } | |
| return newSlot; | |
| } | |
| private <S extends Slot<T>> S computeExisting( | |
| SlotMapOwner<T> owner, | |
| CompoundOperationMap<T> compoundOp, | |
| Object key, | |
| int index, | |
| SlotComputer<S, T> c, | |
| Slot<T> slot, | |
| Slot<T> prev, | |
| int slotIndex) { | |
| // Modify or remove existing slot | |
| S newSlot = c.compute(key, index, slot, compoundOp, owner); | |
| if (!compoundOp.touched) { | |
| if (newSlot == null) { | |
| // Need to delete this slot actually | |
| removeSlot(slot, prev, slotIndex, key); | |
| } else if (!Objects.equals(slot, newSlot)) { | |
| // Replace slot in hash table | |
| if (prev == slot) { | |
| slots[slotIndex] = newSlot; | |
| } else { | |
| prev.next = newSlot; | |
| } | |
| newSlot.next = slot.next; | |
| // Replace new slot in linked list, keeping same order | |
| if (slot == firstAdded) { | |
| firstAdded = newSlot; | |
| } else { | |
| Slot<T> ps = firstAdded; | |
| while ((ps != null) && (ps.orderedNext != slot)) { | |
| ps = ps.orderedNext; | |
| } | |
| if (ps != null) { | |
| ps.orderedNext = newSlot; | |
| } | |
| } | |
| newSlot.orderedNext = slot.orderedNext; | |
| if (slot == lastAdded) { | |
| lastAdded = newSlot; | |
| } | |
| } | |
| return newSlot; | |
| } else { | |
| return compoundOp.compute( | |
| owner, compoundOp, key, slotIndex, (k, i, s, m, o) -> newSlot); | |
| } | |
| } | |
| @Override | |
| @SuppressWarnings("unchecked") | |
| public void add(SlotMapOwner<T> owner, Slot<T> newSlot) { | |
| if (slots == null) { | |
| slots = new Slot[INITIAL_SLOT_SIZE]; | |
| } | |
| createNewSlot(owner, newSlot); | |
| } | |
| private void insertNewSlot(Slot<T> newSlot) { | |
| ++count; | |
| // add new slot to linked list | |
| if (lastAdded != null) { | |
| lastAdded.orderedNext = newSlot; | |
| } | |
| if (firstAdded == null) { | |
| firstAdded = newSlot; | |
| } | |
| lastAdded = newSlot; | |
| if (newSlot.getName() == null) hasIndex = true; | |
| addKnownAbsentSlot(slots, newSlot); | |
| } | |
| private void removeSlot(Slot<T> slot, Slot<T> prev, int ix, Object key) { | |
| count--; | |
| // remove slot from hash table | |
| if (prev == slot) { | |
| slots[ix] = slot.next; | |
| } else { | |
| prev.next = slot.next; | |
| } | |
| // remove from ordered list. Previously this was done lazily in | |
| // getIds() but delete is an infrequent operation so O(n) | |
| // should be ok | |
| // ordered list always uses the actual slot | |
| if (slot == firstAdded) { | |
| prev = null; | |
| firstAdded = slot.orderedNext; | |
| } else { | |
| prev = firstAdded; | |
| while (prev.orderedNext != slot) { | |
| prev = prev.orderedNext; | |
| } | |
| prev.orderedNext = slot.orderedNext; | |
| } | |
| if (slot == lastAdded) { | |
| lastAdded = prev; | |
| } | |
| } | |
| private static <T extends PropHolder<T>> void copyTable( | |
| Slot<T>[] oldSlots, Slot<T>[] newSlots) { | |
| for (var slot : oldSlots) { | |
| while (slot != null) { | |
| var nextSlot = slot.next; | |
| addKnownAbsentSlot(newSlots, slot); | |
| slot = nextSlot; | |
| } | |
| } | |
| } | |
| /** | |
| * Add slot with keys that are known to absent from the table. This is an optimization to use | |
| * when inserting into empty table, after table growth or during deserialization. | |
| */ | |
| private static <T extends PropHolder<T>> void addKnownAbsentSlot( | |
| Slot<T>[] addSlots, Slot<T> slot) { | |
| final int insertPos = getSlotIndex(addSlots.length, slot.getIndexOrHash()); | |
| slot.next = addSlots[insertPos]; | |
| addSlots[insertPos] = slot; | |
| } | |
| private static int getSlotIndex(int tableSize, int indexOrHash) { | |
| // This is a Java trick to efficiently "mod" the hash code by the table size. | |
| // It only works if the table size is a power of 2. | |
| // The performance improvement is measurable. | |
| return indexOrHash & (tableSize - 1); | |
| } | |
| @Override | |
| public SlotMap<T> copyMap() { | |
| return new EmbeddedSlotMap<>(this); | |
| } | |
| } |