Skip to content

Commit ab329db

Browse files
committed
Init PEMap based on EconomicMapImpl
1 parent bd52530 commit ab329db

File tree

4 files changed

+923
-182
lines changed

4 files changed

+923
-182
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/DynamicObjectStorage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private static int incrementLen(DynamicObjectStorage self, ReadAttributeFromDyna
143143
@SuppressWarnings("unused")
144144
@ExportMessage
145145
@ImportStatic(PGuards.class)
146-
public static class GetItemWithState {
146+
static class GetItemWithState {
147147
@Specialization
148148
static Object string(DynamicObjectStorage self, String key, ThreadState state,
149149
@Shared("readKey") @Cached ReadAttributeFromDynamicObjectNode readKey,
@@ -165,7 +165,7 @@ static Object pstring(DynamicObjectStorage self, PString key, ThreadState state,
165165
static Object notString(DynamicObjectStorage self, Object key, ThreadState state,
166166
@Shared("readKey") @Cached ReadAttributeFromDynamicObjectNode readKey,
167167
@Exclusive @Cached("self.store.getShape()") Shape cachedShape,
168-
@Cached(value = "cachedShape.getKeyList().toArray()", dimensions = 1) Object[] keyList,
168+
@Exclusive @Cached(value = "cachedShape.getKeyList().toArray()", dimensions = 1) Object[] keyList,
169169
@Shared("builtinStringProfile") @Cached IsBuiltinClassProfile profile,
170170
@CachedLibrary(limit = "2") PythonObjectLibrary lib,
171171
@Exclusive @Cached("createBinaryProfile()") ConditionProfile gotState,
@@ -240,7 +240,7 @@ private static void invalidateAttributeInMROFinalAssumptions(MroSequenceStorage
240240
@SuppressWarnings("unused")
241241
@ExportMessage
242242
@ImportStatic(PGuards.class)
243-
public static class SetItemWithState {
243+
static class SetItemWithState {
244244

245245
@Specialization
246246
static HashingStorage string(DynamicObjectStorage self, String key, Object value, ThreadState state,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/EconomicMapStorage.java

Lines changed: 27 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public boolean equals(Object a, Object b) {
8181
return (a == b) || (a != null && objectEquals(a, b));
8282
}
8383

84-
@TruffleBoundary
8584
private boolean objectEquals(Object a, Object b) {
8685
return a.equals(b);
8786
}
@@ -134,24 +133,23 @@ public int hashCode() {
134133
}
135134
}
136135

137-
private final PEMap<DictKey, Object> map;
136+
private final PEMap map;
138137

139-
private EconomicMapStorage() {
140-
this.map = new PEMap<>();
138+
private EconomicMapStorage(int initialCapacity) {
139+
this.map = PEMap.create(EconomicMapStorage.DEFAULT_EQIVALENCE, initialCapacity, false);
141140
}
142141

143-
private EconomicMapStorage(int initialCapacity) {
144-
this.map = new PEMap<>(initialCapacity);
142+
private EconomicMapStorage() {
143+
this(4);
145144
}
146145

147146
private EconomicMapStorage(EconomicMapStorage original) {
148-
this.map = new PEMap<>(original.map.size());
147+
this(original.map.size());
149148
this.map.putAll(original.map);
150149
}
151150

152-
@TruffleBoundary
153151
public EconomicMapStorage(EconomicMap<? extends Object, ? extends Object> map) {
154-
this.map = new PEMap<>(map.size());
152+
this(map.size());
155153
MapCursor<? extends Object, ? extends Object> c = map.getEntries();
156154
while (c.advance()) {
157155
Object key = c.getKey();
@@ -255,7 +253,6 @@ public HashingStorage delItemWithState(Object key, ThreadState state,
255253

256254
@Override
257255
@ExportMessage
258-
@TruffleBoundary
259256
public HashingStorage[] injectInto(HashingStorage[] firstValue, InjectIntoNode node) {
260257
HashingStorage[] result = firstValue;
261258
MapCursor<DictKey, Object> cursor = map.getEntries();
@@ -274,7 +271,6 @@ static HashingStorage toSameType(EconomicMapStorage self, EconomicMapStorage oth
274271
}
275272

276273
@Specialization
277-
@TruffleBoundary
278274
static HashingStorage generic(EconomicMapStorage self, HashingStorage other,
279275
@CachedLibrary(limit = "1") HashingStorageLibrary lib) {
280276
HashingStorage result = other;
@@ -302,34 +298,34 @@ public HashingStorage copy() {
302298
@ExportMessage
303299
public static class EqualsWithState {
304300
@Specialization
305-
@TruffleBoundary
306-
static boolean toSameType(EconomicMapStorage self, EconomicMapStorage other, ThreadState state,
307-
@CachedLibrary(limit = "3") PythonObjectLibrary compareLib) {
301+
static boolean equalSameType(EconomicMapStorage self, EconomicMapStorage other, ThreadState state,
302+
@CachedLibrary(limit = "5") PythonObjectLibrary compareLib1,
303+
@CachedLibrary(limit = "5") PythonObjectLibrary compareLib2) {
308304
if (self.map.size() != other.map.size()) {
309305
return false;
310306
}
311307
MapCursor<DictKey, Object> cursor = self.map.getEntries();
312308
while (cursor.advance()) {
313309
Object otherValue = other.map.get(cursor.getKey());
314-
if (otherValue != null && !compareLib.equalsWithState(otherValue, cursor.getValue(), compareLib, state)) {
310+
if (otherValue != null && !compareLib1.equalsWithState(otherValue, cursor.getValue(), compareLib2, state)) {
315311
return false;
316312
}
317313
}
318314
return true;
319315
}
320316

321317
@Specialization
322-
@TruffleBoundary
323-
static boolean generic(EconomicMapStorage self, HashingStorage other, ThreadState state,
324-
@CachedLibrary(limit = "1") HashingStorageLibrary lib,
325-
@CachedLibrary(limit = "3") PythonObjectLibrary compareLib) {
318+
static boolean equalGeneric(EconomicMapStorage self, HashingStorage other, ThreadState state,
319+
@CachedLibrary(limit = "5") HashingStorageLibrary lib,
320+
@CachedLibrary(limit = "5") PythonObjectLibrary compareLib1,
321+
@CachedLibrary(limit = "5") PythonObjectLibrary compareLib2) {
326322
if (self.map.size() != lib.lengthWithState(other, state)) {
327323
return false;
328324
}
329325
MapCursor<DictKey, Object> cursor = self.map.getEntries();
330326
while (cursor.advance()) {
331327
Object otherValue = lib.getItemWithState(self, cursor.getKey().value, state);
332-
if (otherValue != null && !compareLib.equalsWithState(otherValue, cursor.getValue(), compareLib, state)) {
328+
if (otherValue != null && !compareLib1.equalsWithState(otherValue, cursor.getValue(), compareLib2, state)) {
333329
return false;
334330
}
335331
}
@@ -339,9 +335,8 @@ static boolean generic(EconomicMapStorage self, HashingStorage other, ThreadStat
339335

340336
@ExportMessage
341337
public static class CompareKeys {
342-
@TruffleBoundary
343338
@Specialization
344-
static int toSameType(EconomicMapStorage self, EconomicMapStorage other) {
339+
static int compareSameType(EconomicMapStorage self, EconomicMapStorage other) {
345340
int size = self.map.size();
346341
int size2 = other.map.size();
347342
if (size > size2) {
@@ -360,9 +355,8 @@ static int toSameType(EconomicMapStorage self, EconomicMapStorage other) {
360355
}
361356
}
362357

363-
@TruffleBoundary
364-
@Specialization(limit = "1")
365-
static int generic(EconomicMapStorage self, HashingStorage other,
358+
@Specialization(limit = "4")
359+
static int compareGeneric(EconomicMapStorage self, HashingStorage other,
366360
@CachedLibrary("other") HashingStorageLibrary lib) {
367361
int size = self.map.size();
368362
int length = lib.length(other);
@@ -386,8 +380,7 @@ static int generic(EconomicMapStorage self, HashingStorage other,
386380
@ExportMessage
387381
public static class Intersect {
388382
@Specialization
389-
@TruffleBoundary
390-
static HashingStorage toSameType(EconomicMapStorage self, EconomicMapStorage other) {
383+
static HashingStorage intersectSameType(EconomicMapStorage self, EconomicMapStorage other) {
391384
EconomicMapStorage result = EconomicMapStorage.create();
392385
MapCursor<DictKey, Object> cursor = self.map.getEntries();
393386
while (cursor.advance()) {
@@ -398,10 +391,9 @@ static HashingStorage toSameType(EconomicMapStorage self, EconomicMapStorage oth
398391
return result;
399392
}
400393

401-
@Specialization
402-
@TruffleBoundary
403-
static HashingStorage generic(EconomicMapStorage self, HashingStorage other,
404-
@CachedLibrary(limit = "1") HashingStorageLibrary lib) {
394+
@Specialization(limit = "4")
395+
static HashingStorage intersectGeneric(EconomicMapStorage self, HashingStorage other,
396+
@CachedLibrary("other") HashingStorageLibrary lib) {
405397
EconomicMapStorage result = EconomicMapStorage.create();
406398
MapCursor<DictKey, Object> cursor = self.map.getEntries();
407399
while (cursor.advance()) {
@@ -416,8 +408,7 @@ static HashingStorage generic(EconomicMapStorage self, HashingStorage other,
416408
@ExportMessage
417409
public static class Diff {
418410
@Specialization
419-
@TruffleBoundary
420-
static HashingStorage toSameType(EconomicMapStorage self, EconomicMapStorage other) {
411+
static HashingStorage diffSameType(EconomicMapStorage self, EconomicMapStorage other) {
421412
EconomicMapStorage result = EconomicMapStorage.create();
422413
MapCursor<DictKey, Object> cursor = self.map.getEntries();
423414
while (cursor.advance()) {
@@ -428,10 +419,9 @@ static HashingStorage toSameType(EconomicMapStorage self, EconomicMapStorage oth
428419
return result;
429420
}
430421

431-
@Specialization
432-
@TruffleBoundary
433-
static HashingStorage generic(EconomicMapStorage self, HashingStorage other,
434-
@CachedLibrary(limit = "1") HashingStorageLibrary lib) {
422+
@Specialization(limit = "4")
423+
static HashingStorage diffGeneric(EconomicMapStorage self, HashingStorage other,
424+
@CachedLibrary("other") HashingStorageLibrary lib) {
435425
EconomicMapStorage result = EconomicMapStorage.create();
436426
MapCursor<DictKey, Object> cursor = self.map.getEntries();
437427
while (cursor.advance()) {
@@ -481,128 +471,3 @@ public String toString() {
481471
return builder.toString();
482472
}
483473
}
484-
485-
final class PEMap<K, V> {
486-
private final EconomicMap<K, V> map;
487-
488-
@TruffleBoundary
489-
PEMap(int initialCapacity) {
490-
this.map = EconomicMap.create(EconomicMapStorage.DEFAULT_EQIVALENCE, initialCapacity);
491-
}
492-
493-
PEMap() {
494-
this(4);
495-
}
496-
497-
@TruffleBoundary
498-
public V get(K key) {
499-
return map.get(key);
500-
}
501-
502-
@TruffleBoundary
503-
public boolean containsKey(K key) {
504-
return map.containsKey(key);
505-
}
506-
507-
@TruffleBoundary
508-
public int size() {
509-
return map.size();
510-
}
511-
512-
@TruffleBoundary
513-
public boolean isEmpty() {
514-
return map.isEmpty();
515-
}
516-
517-
@TruffleBoundary
518-
public Iterable<K> getKeys() {
519-
return new KeysIterable<>(map.getKeys());
520-
}
521-
522-
private static final class KeysIterable<K> implements Iterable<K> {
523-
private final Iterable<K> keysIterable;
524-
525-
private KeysIterable(Iterable<K> keysIterable) {
526-
this.keysIterable = keysIterable;
527-
}
528-
529-
@TruffleBoundary
530-
public Iterator<K> iterator() {
531-
return new KeysIterator<>(keysIterable.iterator());
532-
}
533-
}
534-
535-
private static final class KeysIterator<K> implements Iterator<K> {
536-
private final Iterator<K> keysIterator;
537-
538-
KeysIterator(Iterator<K> iter) {
539-
this.keysIterator = iter;
540-
}
541-
542-
@TruffleBoundary
543-
public boolean hasNext() {
544-
return keysIterator.hasNext();
545-
}
546-
547-
@TruffleBoundary
548-
public K next() {
549-
return keysIterator.next();
550-
}
551-
}
552-
553-
@TruffleBoundary
554-
public V put(K key, V value) {
555-
return map.put(key, value);
556-
}
557-
558-
@TruffleBoundary
559-
public void clear() {
560-
map.clear();
561-
}
562-
563-
@TruffleBoundary
564-
public V removeKey(K key) {
565-
return map.removeKey(key);
566-
}
567-
568-
@TruffleBoundary
569-
public MapCursor<K, V> getEntries() {
570-
return new EntriesMapCursor<>(map.getEntries());
571-
}
572-
573-
@TruffleBoundary
574-
public void putAll(PEMap<K, V> other) {
575-
MapCursor<K, V> e = other.getEntries();
576-
while (e.advance()) {
577-
put(e.getKey(), e.getValue());
578-
}
579-
}
580-
581-
private static final class EntriesMapCursor<K, V> implements MapCursor<K, V> {
582-
private final MapCursor<K, V> mapCursor;
583-
584-
public EntriesMapCursor(MapCursor<K, V> mapCursor) {
585-
this.mapCursor = mapCursor;
586-
}
587-
588-
@TruffleBoundary
589-
public boolean advance() {
590-
return mapCursor.advance();
591-
}
592-
593-
@TruffleBoundary
594-
public K getKey() {
595-
return mapCursor.getKey();
596-
}
597-
598-
@TruffleBoundary
599-
public V getValue() {
600-
return mapCursor.getValue();
601-
}
602-
603-
@TruffleBoundary
604-
public void remove() {
605-
mapCursor.remove();
606-
}
607-
}
608-
}

0 commit comments

Comments
 (0)