Skip to content

Commit 9b60a76

Browse files
committed
replaced dict.get/setDictStorage() calls with Get/SetDictStorageNode in DictBuiltins and FrozenSetBuiltins
1 parent a617ad9 commit 9b60a76

File tree

2 files changed

+170
-114
lines changed

2 files changed

+170
-114
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictBuiltins.java

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,16 @@ private HashingStorage.InitNode getInitNode() {
135135
}
136136

137137
@Specialization(guards = "args.length == 1")
138-
Object doVarargs(VirtualFrame frame, PDict self, Object[] args, PKeyword[] kwargs) {
139-
self.setDictStorage(getInitNode().execute(frame, args[0], kwargs));
138+
Object doVarargs(VirtualFrame frame, PDict self, Object[] args, PKeyword[] kwargs,
139+
@Cached SetDictStorageNode setStorage) {
140+
setStorage.execute(self, getInitNode().execute(frame, args[0], kwargs));
140141
return PNone.NONE;
141142
}
142143

143144
@Specialization(guards = "args.length == 0")
144-
Object doKeywords(VirtualFrame frame, PDict self, @SuppressWarnings("unused") Object[] args, PKeyword[] kwargs) {
145-
self.setDictStorage(getInitNode().execute(frame, NO_VALUE, kwargs));
145+
Object doKeywords(VirtualFrame frame, PDict self, @SuppressWarnings("unused") Object[] args, PKeyword[] kwargs,
146+
@Cached SetDictStorageNode setStorage) {
147+
setStorage.execute(self, getInitNode().execute(frame, NO_VALUE, kwargs));
146148
return PNone.NONE;
147149
}
148150

@@ -157,17 +159,19 @@ Object doGeneric(@SuppressWarnings("unused") PDict self, Object[] args, @Suppres
157159
@GenerateNodeFactory
158160
public abstract static class SetDefaultNode extends PythonBuiltinNode {
159161

160-
@Specialization(guards = "lib.hasKeyWithFrame(dict.getDictStorage(), key, hasFrame, frame)", limit = "3")
162+
@Specialization(guards = "lib.hasKeyWithFrame(getStorage.execute(dict), key, hasFrame, frame)", limit = "3")
161163
public Object setDefault(VirtualFrame frame, PDict dict, Object key, @SuppressWarnings("unused") Object defaultValue,
162164
@SuppressWarnings("unused") @Cached("createBinaryProfile()") ConditionProfile hasFrame,
163-
@CachedLibrary("dict.getDictStorage()") HashingStorageLibrary lib) {
164-
return lib.getItemWithFrame(dict.getDictStorage(), key, hasFrame, frame);
165+
@Cached GetDictStorageNode getStorage,
166+
@CachedLibrary("getStorage.execute(dict)") HashingStorageLibrary lib) {
167+
return lib.getItemWithFrame(getStorage.execute(dict), key, hasFrame, frame);
165168
}
166169

167-
@Specialization(guards = "!lib.hasKeyWithFrame(dict.getDictStorage(), key, hasFrame, frame)", limit = "3")
170+
@Specialization(guards = "!lib.hasKeyWithFrame(getStorage.execute(dict), key, hasFrame, frame)", limit = "3")
168171
public Object setDefault(VirtualFrame frame, PDict dict, Object key, Object defaultValue,
169172
@Cached("create()") HashingCollectionNodes.SetItemNode setItemNode,
170-
@SuppressWarnings("unused") @CachedLibrary("dict.getDictStorage()") HashingStorageLibrary lib,
173+
@SuppressWarnings("unused") @Cached GetDictStorageNode getStorage,
174+
@SuppressWarnings("unused") @CachedLibrary("getStorage.execute(dict)") HashingStorageLibrary lib,
171175
@SuppressWarnings("unused") @Cached("createBinaryProfile()") ConditionProfile hasFrame,
172176
@Cached("createBinaryProfile()") ConditionProfile defaultValProfile) {
173177
Object value = defaultValue;
@@ -184,9 +188,8 @@ public Object setDefault(VirtualFrame frame, PDict dict, Object key, Object defa
184188
@GenerateNodeFactory
185189
public abstract static class PopNode extends PythonTernaryBuiltinNode {
186190

187-
protected void removeItem(VirtualFrame frame, PDict dict, Object key,
188-
HashingStorageLibrary lib, ConditionProfile hasFrame, BranchProfile updatedStorage) {
189-
HashingStorage storage = dict.getDictStorage();
191+
protected void removeItem(VirtualFrame frame, PDict dict, Object key, HashingStorage storage,
192+
HashingStorageLibrary lib, ConditionProfile hasFrame, BranchProfile updatedStorage, SetDictStorageNode setStorage) {
190193
HashingStorage newStore = null;
191194
// TODO: FIXME: this might call __hash__ twice
192195
boolean hasKey = lib.hasKeyWithFrame(storage, key, hasFrame, frame);
@@ -197,7 +200,7 @@ protected void removeItem(VirtualFrame frame, PDict dict, Object key,
197200
if (hasKey) {
198201
if (newStore != storage) {
199202
updatedStorage.enter();
200-
dict.setDictStorage(newStore);
203+
setStorage.execute(dict, newStore);
201204
}
202205
}
203206
}
@@ -208,10 +211,13 @@ public Object popDefault(VirtualFrame frame, PDict dict, Object key, Object defa
208211
@Cached ConditionProfile hasKey,
209212
@Cached ConditionProfile hasDefault,
210213
@Cached ConditionProfile hasFrame,
211-
@CachedLibrary("dict.getDictStorage()") HashingStorageLibrary lib) {
212-
Object retVal = lib.getItemWithFrame(dict.getDictStorage(), key, hasFrame, frame);
214+
@Cached GetDictStorageNode getStorage,
215+
@Cached SetDictStorageNode setStorage,
216+
@CachedLibrary("getStorage.execute(dict)") HashingStorageLibrary lib) {
217+
HashingStorage dictStorage = getStorage.execute(dict);
218+
Object retVal = lib.getItemWithFrame(dictStorage, key, hasFrame, frame);
213219
if (hasKey.profile(retVal != null)) {
214-
removeItem(frame, dict, key, lib, hasFrame, updatedStorage);
220+
removeItem(frame, dict, key, dictStorage, lib, hasFrame, updatedStorage, setStorage);
215221
return retVal;
216222
} else if (hasDefault.profile(defaultValue != PNone.NO_VALUE)) {
217223
return defaultValue;
@@ -228,8 +234,9 @@ public abstract static class PopItemNode extends PythonUnaryBuiltinNode {
228234

229235
@Specialization(limit = "3")
230236
public Object popItem(PDict dict,
231-
@CachedLibrary("dict.getDictStorage()") HashingStorageLibrary lib) {
232-
HashingStorage storage = dict.getDictStorage();
237+
@Cached GetDictStorageNode getStorage,
238+
@CachedLibrary("getStorage.execute(dict)") HashingStorageLibrary lib) {
239+
HashingStorage storage = getStorage.execute(dict);
233240
for (DictEntry entry : lib.entries(storage)) {
234241
PTuple result = factory().createTuple(new Object[]{entry.getKey(), entry.getValue()});
235242
lib.delItem(storage, entry.getKey());
@@ -267,9 +274,10 @@ public PDictView items(PDict self) {
267274
public abstract static class GetNode extends PythonTernaryBuiltinNode {
268275
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
269276
public Object doWithDefault(VirtualFrame frame, PDict self, Object key, Object defaultValue,
270-
@CachedLibrary(value = "self.getDictStorage()") HashingStorageLibrary hlib,
277+
@Cached GetDictStorageNode getStorage,
278+
@CachedLibrary(value = "getStorage.execute(self)") HashingStorageLibrary hlib,
271279
@Cached ConditionProfile profile) {
272-
final Object value = hlib.getItemWithFrame(self.getDictStorage(), key, profile, frame);
280+
final Object value = hlib.getItemWithFrame(getStorage.execute(self), key, profile, frame);
273281
return value != null ? value : (defaultValue == PNone.NO_VALUE ? PNone.NONE : defaultValue);
274282
}
275283
}
@@ -279,10 +287,11 @@ public Object doWithDefault(VirtualFrame frame, PDict self, Object key, Object d
279287
public abstract static class GetItemNode extends PythonBinaryBuiltinNode {
280288
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
281289
Object getItem(VirtualFrame frame, PDict self, Object key,
282-
@CachedLibrary(value = "self.getDictStorage()") HashingStorageLibrary hlib,
290+
@Cached GetDictStorageNode getStorage,
291+
@CachedLibrary(value = "getStorage.execute(self)") HashingStorageLibrary hlib,
283292
@Exclusive @Cached("createBinaryProfile()") ConditionProfile profile,
284293
@Cached DispatchMissingNode missing) {
285-
final Object result = hlib.getItemWithFrame(self.getDictStorage(), key, profile, frame);
294+
final Object result = hlib.getItemWithFrame(getStorage.execute(self), key, profile, frame);
286295
if (result == null) {
287296
return missing.execute(frame, self, key);
288297
}
@@ -353,9 +362,11 @@ public abstract static class DelItemNode extends PythonBinaryBuiltinNode {
353362
@Specialization
354363
Object run(VirtualFrame frame, PDict self, Object key,
355364
@Cached BranchProfile updatedStorage,
365+
@Cached SetDictStorageNode setStorage,
366+
@Cached GetDictStorageNode getStorage,
356367
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
357368
@CachedLibrary(limit = "3") HashingStorageLibrary lib) {
358-
HashingStorage storage = self.getDictStorage();
369+
HashingStorage storage = getStorage.execute(self);
359370
HashingStorage newStore = null;
360371
// TODO: FIXME: this might call __hash__ twice
361372
boolean hasKey = lib.hasKeyWithFrame(storage, key, hasFrame, frame);
@@ -366,7 +377,7 @@ Object run(VirtualFrame frame, PDict self, Object key,
366377
if (hasKey) {
367378
if (newStore != storage) {
368379
updatedStorage.enter();
369-
self.setDictStorage(newStore);
380+
setStorage.execute(self, newStore);
370381
}
371382
return PNone.NONE;
372383
}
@@ -405,22 +416,24 @@ public abstract static class EqNode extends PythonBinaryBuiltinNode {
405416
@Specialization(limit = "1")
406417
Object doDictDict(VirtualFrame frame, PDict self, PDict other,
407418
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
408-
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary lib) {
419+
@Cached GetDictStorageNode getStorage,
420+
@CachedLibrary("getStorage.execute(self)") HashingStorageLibrary lib) {
409421
if (hasFrame.profile(frame != null)) {
410-
return lib.equalsWithState(self.getDictStorage(), other.getDictStorage(), PArguments.getThreadState(frame));
422+
return lib.equalsWithState(getStorage.execute(self), getStorage.execute(other), PArguments.getThreadState(frame));
411423
} else {
412-
return lib.equals(self.getDictStorage(), other.getDictStorage());
424+
return lib.equals(getStorage.execute(self), getStorage.execute(other));
413425
}
414426
}
415427

416428
@Specialization(limit = "1")
417429
Object doDictProxy(VirtualFrame frame, PDict self, PMappingproxy other,
418430
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
419-
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary lib) {
431+
@Cached GetDictStorageNode getStorage,
432+
@CachedLibrary("getStorage.execute(self)") HashingStorageLibrary lib) {
420433
if (hasFrame.profile(frame != null)) {
421-
return lib.equalsWithState(self.getDictStorage(), other.getDictStorage(), PArguments.getThreadState(frame));
434+
return lib.equalsWithState(getStorage.execute(self), getStorage.execute(other), PArguments.getThreadState(frame));
422435
} else {
423-
return lib.equals(self.getDictStorage(), other.getDictStorage());
436+
return lib.equals(getStorage.execute(self), getStorage.execute(other));
424437
}
425438
}
426439

@@ -438,8 +451,9 @@ public abstract static class ContainsNode extends PythonBinaryBuiltinNode {
438451
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
439452
boolean run(VirtualFrame frame, PDict self, Object key,
440453
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
441-
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary lib) {
442-
return lib.hasKeyWithFrame(self.getDictStorage(), key, hasFrame, frame);
454+
@Cached GetDictStorageNode getStorage,
455+
@CachedLibrary("getStorage.execute(self)") HashingStorageLibrary lib) {
456+
return lib.hasKeyWithFrame(getStorage.execute(self), key, hasFrame, frame);
443457
}
444458
}
445459

@@ -458,8 +472,9 @@ public boolean repr(PDict self,
458472
public abstract static class LenNode extends PythonUnaryBuiltinNode {
459473
@Specialization(limit = "1")
460474
public int len(PDict self,
461-
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary lib) {
462-
return lib.length(self.getDictStorage());
475+
@Cached GetDictStorageNode getStorage,
476+
@CachedLibrary("getStorage.execute(self)") HashingStorageLibrary lib) {
477+
return lib.length(getStorage.execute(self));
463478
}
464479
}
465480

@@ -470,8 +485,9 @@ public abstract static class CopyNode extends PythonUnaryBuiltinNode {
470485

471486
@Specialization(limit = "1")
472487
public PDict copy(@SuppressWarnings("unused") VirtualFrame frame, PDict dict,
473-
@CachedLibrary("dict.getDictStorage()") HashingStorageLibrary lib) {
474-
return factory().createDict(lib.copy(dict.getDictStorage()));
488+
@Cached GetDictStorageNode getStorage,
489+
@CachedLibrary("getStorage.execute(dict)") HashingStorageLibrary lib) {
490+
return factory().createDict(lib.copy(getStorage.execute(dict)));
475491
}
476492
}
477493

@@ -482,9 +498,10 @@ public abstract static class ClearNode extends PythonUnaryBuiltinNode {
482498

483499
@Specialization(limit = "3")
484500
public PDict clear(PDict dict,
485-
@CachedLibrary("dict.getDictStorage()") HashingStorageLibrary lib,
501+
@Cached GetDictStorageNode getStorage,
502+
@CachedLibrary("getStorage.execute(dict)") HashingStorageLibrary lib,
486503
@Cached SetDictStorageNode setStorage) {
487-
HashingStorage newStorage = lib.clear(dict.getDictStorage());
504+
HashingStorage newStorage = lib.clear(getStorage.execute(dict));
488505
setStorage.execute(dict, newStorage);
489506
return dict;
490507
}

0 commit comments

Comments
 (0)