Skip to content

Commit 661b2ac

Browse files
committed
Add branch profiles and reduce @TruffleBoundries
1 parent 4425379 commit 661b2ac

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
lines changed

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,11 @@ public EconomicMapStorage(EconomicMap<? extends Object, ? extends Object> map) {
115115
this(map.size());
116116
final PythonObjectLibrary lib = PythonObjectLibrary.getUncached();
117117
MapCursor<? extends Object, ? extends Object> c = map.getEntries();
118+
ConditionProfile findProfile = ConditionProfile.createBinaryProfile();
118119
while (c.advance()) {
119120
Object key = c.getKey();
120121
assert key instanceof Integer || key instanceof String;
121-
this.map.put(new DictKey(key, key.hashCode()), c.getValue(), lib, lib);
122+
this.map.put(new DictKey(key, key.hashCode()), c.getValue(), lib, lib, findProfile);
122123
}
123124
}
124125

@@ -149,30 +150,33 @@ static class GetItemWithState {
149150

150151
@Specialization
151152
static Object getItemString(EconomicMapStorage self, String key, @SuppressWarnings("unused") ThreadState state,
153+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
152154
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
153155
DictKey newKey = new DictKey(key, key.hashCode());
154-
return self.map.get(newKey, lib, lib);
156+
return self.map.get(newKey, lib, lib, findProfile);
155157
}
156158

157159
@SuppressWarnings("unused")
158160
@Specialization(guards = {"!isNativeString(key)", "isBuiltinString(key, isBuiltinClassProfile, getClassNode)"})
159161
static Object getItemPString(EconomicMapStorage self, PString key, @SuppressWarnings("unused") ThreadState state,
162+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
160163
@Exclusive @Cached("createClassProfile()") ValueProfile profile,
161164
@Exclusive @Cached IsBuiltinClassProfile isBuiltinClassProfile,
162165
@Exclusive @Cached GetLazyClassNode getClassNode,
163166
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
164167
final String k = EconomicMapStorage.toString(key, profile);
165-
return getItemString(self, k, state, lib);
168+
return getItemString(self, k, state, findProfile, lib);
166169
}
167170

168171
@Specialization(replaces = "getItemString", limit = "3")
169172
static Object getItemGeneric(EconomicMapStorage self, Object key, ThreadState state,
170173
@CachedLibrary("key") PythonObjectLibrary lib,
171174
@CachedLibrary(limit = "2") PythonObjectLibrary otherlib,
175+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
172176
@Exclusive @Cached("createBinaryProfile()") ConditionProfile gotState) {
173177
final long h = self.getHashWithState(key, lib, state, gotState);
174178
DictKey newKey = new DictKey(key, h);
175-
return self.map.get(newKey, lib, otherlib);
179+
return self.map.get(newKey, lib, otherlib, findProfile);
176180
}
177181
}
178182

@@ -183,29 +187,32 @@ static class SetItemWithState {
183187

184188
@Specialization
185189
static HashingStorage setItemString(EconomicMapStorage self, String key, Object value, ThreadState state,
190+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
186191
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
187192
DictKey newKey = new DictKey(key, key.hashCode());
188-
self.map.put(newKey, value, lib, lib);
193+
self.map.put(newKey, value, lib, lib, findProfile);
189194
return self;
190195
}
191196

192197
@Specialization(guards = {"!isNativeString(key)", "isBuiltinString(key, isBuiltinClassProfile, getClassNode)"})
193198
static HashingStorage setItemPString(EconomicMapStorage self, PString key, Object value, ThreadState state,
194199
@Exclusive @Cached("createClassProfile()") ValueProfile profile,
200+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
195201
@Exclusive @Cached IsBuiltinClassProfile isBuiltinClassProfile,
196202
@Exclusive @Cached GetLazyClassNode getClassNode,
197203
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
198204
final String k = EconomicMapStorage.toString(key, profile);
199-
return setItemString(self, k, value, state, lib);
205+
return setItemString(self, k, value, state, findProfile, lib);
200206
}
201207

202208
@Specialization(replaces = "setItemString", limit = "3")
203209
static HashingStorage setItemGeneric(EconomicMapStorage self, Object key, Object value, ThreadState state,
204210
@CachedLibrary("key") PythonObjectLibrary lib,
205211
@CachedLibrary(limit = "2") PythonObjectLibrary otherlib,
212+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
206213
@Exclusive @Cached("createBinaryProfile()") ConditionProfile gotState) {
207214
DictKey newKey = new DictKey(key, self.getHashWithState(key, lib, state, gotState));
208-
self.map.put(newKey, value, lib, otherlib);
215+
self.map.put(newKey, value, lib, otherlib, findProfile);
209216
return self;
210217
}
211218
}
@@ -272,14 +279,15 @@ public static class EqualsWithState {
272279
@TruffleBoundary
273280
@Specialization
274281
static boolean equalSameType(EconomicMapStorage self, EconomicMapStorage other, ThreadState state,
282+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
275283
@CachedLibrary(limit = "2") PythonObjectLibrary compareLib1,
276284
@CachedLibrary(limit = "2") PythonObjectLibrary compareLib2) {
277285
if (self.map.size() != other.map.size()) {
278286
return false;
279287
}
280288
MapCursor<DictKey, Object> cursor = self.map.getEntries();
281289
while (cursor.advance()) {
282-
Object otherValue = other.map.get(cursor.getKey(), compareLib1, compareLib2);
290+
Object otherValue = other.map.get(cursor.getKey(), compareLib1, compareLib2, findProfile);
283291
if (otherValue != null && !compareLib1.equalsWithState(otherValue, cursor.getValue(), compareLib2, state)) {
284292
return false;
285293
}
@@ -313,6 +321,7 @@ public static class CompareKeys {
313321
@TruffleBoundary
314322
@Specialization
315323
static int compareSameType(EconomicMapStorage self, EconomicMapStorage other,
324+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
316325
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
317326
int size = self.map.size();
318327
int size2 = other.map.size();
@@ -321,7 +330,7 @@ static int compareSameType(EconomicMapStorage self, EconomicMapStorage other,
321330
}
322331
MapCursor<DictKey, Object> cursor = self.map.getEntries();
323332
while (cursor.advance()) {
324-
if (!other.map.containsKey(cursor.getKey(), lib, lib)) {
333+
if (!other.map.containsKey(cursor.getKey(), lib, lib, findProfile)) {
325334
return 1;
326335
}
327336
}
@@ -360,12 +369,13 @@ public static class Intersect {
360369
@TruffleBoundary
361370
@Specialization
362371
static HashingStorage intersectSameType(EconomicMapStorage self, EconomicMapStorage other,
372+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
363373
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
364374
EconomicMapStorage result = EconomicMapStorage.create();
365375
MapCursor<DictKey, Object> cursor = self.map.getEntries();
366376
while (cursor.advance()) {
367-
if (other.map.containsKey(cursor.getKey(), lib, lib)) {
368-
result.map.put(cursor.getKey(), cursor.getValue(), lib, lib);
377+
if (other.map.containsKey(cursor.getKey(), lib, lib, findProfile)) {
378+
result.map.put(cursor.getKey(), cursor.getValue(), lib, lib, findProfile);
369379
}
370380
}
371381
return result;
@@ -374,13 +384,14 @@ static HashingStorage intersectSameType(EconomicMapStorage self, EconomicMapStor
374384
@TruffleBoundary
375385
@Specialization(limit = "4")
376386
static HashingStorage intersectGeneric(EconomicMapStorage self, HashingStorage other,
387+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
377388
@CachedLibrary("other") HashingStorageLibrary hlib,
378389
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
379390
EconomicMapStorage result = EconomicMapStorage.create();
380391
MapCursor<DictKey, Object> cursor = self.map.getEntries();
381392
while (cursor.advance()) {
382393
if (hlib.hasKey(other, cursor.getKey().value)) {
383-
result.map.put(cursor.getKey(), cursor.getValue(), lib, lib);
394+
result.map.put(cursor.getKey(), cursor.getValue(), lib, lib, findProfile);
384395
}
385396
}
386397
return result;
@@ -392,12 +403,13 @@ public static class Diff {
392403
@TruffleBoundary
393404
@Specialization
394405
static HashingStorage diffSameType(EconomicMapStorage self, EconomicMapStorage other,
406+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
395407
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
396408
EconomicMapStorage result = EconomicMapStorage.create();
397409
MapCursor<DictKey, Object> cursor = self.map.getEntries();
398410
while (cursor.advance()) {
399-
if (!other.map.containsKey(cursor.getKey(), lib, lib)) {
400-
result.map.put(cursor.getKey(), cursor.getValue(), lib, lib);
411+
if (!other.map.containsKey(cursor.getKey(), lib, lib, findProfile)) {
412+
result.map.put(cursor.getKey(), cursor.getValue(), lib, lib, findProfile);
401413
}
402414
}
403415
return result;
@@ -406,13 +418,14 @@ static HashingStorage diffSameType(EconomicMapStorage self, EconomicMapStorage o
406418
@TruffleBoundary
407419
@Specialization(limit = "4")
408420
static HashingStorage diffGeneric(EconomicMapStorage self, HashingStorage other,
421+
@Exclusive @Cached("createBinaryProfile()") ConditionProfile findProfile,
409422
@CachedLibrary("other") HashingStorageLibrary hlib,
410423
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
411424
EconomicMapStorage result = EconomicMapStorage.create();
412425
MapCursor<DictKey, Object> cursor = self.map.getEntries();
413426
while (cursor.advance()) {
414427
if (!hlib.hasKey(other, cursor.getKey().value)) {
415-
result.map.put(cursor.getKey(), cursor.getValue(), lib, lib);
428+
result.map.put(cursor.getKey(), cursor.getValue(), lib, lib, findProfile);
416429
}
417430
}
418431
return result;

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

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.oracle.graal.python.builtins.objects.common.EconomicMapStorage.DictKey;
5050
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5151
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
52+
import com.oracle.truffle.api.profiles.ConditionProfile;
5253

5354
@SuppressWarnings("javadoc")
5455
/**
@@ -120,12 +121,10 @@ private static PEMap intercept(PEMap map) {
120121
return map;
121122
}
122123

123-
@TruffleBoundary
124124
public static PEMap create(boolean isSet) {
125125
return intercept(new PEMap(isSet));
126126
}
127127

128-
@TruffleBoundary
129128
public static PEMap create(int initialCapacity, boolean isSet) {
130129
return intercept(new PEMap(initialCapacity, isSet));
131130
}
@@ -164,19 +163,18 @@ private static final class CollisionLink {
164163
final int next;
165164
}
166165

167-
@TruffleBoundary
168-
public Object get(DictKey key, PythonObjectLibrary keylib, PythonObjectLibrary otherlib) {
166+
public Object get(DictKey key, PythonObjectLibrary keylib, PythonObjectLibrary otherlib, ConditionProfile findProfile) {
169167
Objects.requireNonNull(key);
170168

171-
int index = find(key, keylib, otherlib);
169+
int index = find(key, keylib, otherlib, findProfile);
172170
if (index != -1) {
173171
return getValue(index);
174172
}
175173
return null;
176174
}
177175

178-
private int find(DictKey key, PythonObjectLibrary keylib, PythonObjectLibrary otherlib) {
179-
if (hasHashArray()) {
176+
private int find(DictKey key, PythonObjectLibrary keylib, PythonObjectLibrary otherlib, ConditionProfile findProfile) {
177+
if (findProfile.profile(hasHashArray())) {
180178
return findHash(key, keylib, otherlib);
181179
} else {
182180
return findLinear(key, keylib, otherlib);
@@ -337,12 +335,11 @@ private int getHashIndex(DictKey key) {
337335
return hash & (getHashTableSize() - 1);
338336
}
339337

340-
@TruffleBoundary
341-
public Object put(DictKey key, Object value, PythonObjectLibrary keylib, PythonObjectLibrary otherlib) {
338+
public Object put(DictKey key, Object value, PythonObjectLibrary keylib, PythonObjectLibrary otherlib, ConditionProfile findProfile) {
342339
if (key == null) {
343340
throw new UnsupportedOperationException("null not supported as key!");
344341
}
345-
int index = find(key, keylib, otherlib);
342+
int index = find(key, keylib, otherlib, findProfile);
346343
if (index != -1) {
347344
Object oldValue = getValue(index);
348345
setValue(index, value);
@@ -379,8 +376,9 @@ public Object put(DictKey key, Object value, PythonObjectLibrary keylib, PythonO
379376
public void putAll(PEMap other) {
380377
final PythonObjectLibrary lib = PythonObjectLibrary.getUncached();
381378
MapCursor<DictKey, Object> e = other.getEntries();
379+
ConditionProfile findProfile = ConditionProfile.createBinaryProfile();
382380
while (e.advance()) {
383-
put(e.getKey(), e.getValue(), lib, lib);
381+
put(e.getKey(), e.getValue(), lib, lib, findProfile);
384382
}
385383
}
386384

@@ -526,9 +524,8 @@ public int size() {
526524
return totalEntries - deletedEntries;
527525
}
528526

529-
@TruffleBoundary
530-
public boolean containsKey(DictKey key, PythonObjectLibrary keylib, PythonObjectLibrary otherlib) {
531-
return find(key, keylib, otherlib) != -1;
527+
public boolean containsKey(DictKey key, PythonObjectLibrary keylib, PythonObjectLibrary otherlib, ConditionProfile findProfile) {
528+
return find(key, keylib, otherlib, findProfile) != -1;
532529
}
533530

534531
public void clear() {
@@ -770,17 +767,4 @@ public DictKey next() {
770767
}
771768
};
772769
}
773-
774-
public boolean contains(DictKey element, PythonObjectLibrary keylib, PythonObjectLibrary otherlib) {
775-
return containsKey(element, keylib, otherlib);
776-
}
777-
778-
public boolean add(DictKey element, PythonObjectLibrary keylib, PythonObjectLibrary otherlib) {
779-
return put(element, element, keylib, otherlib) == null;
780-
}
781-
782-
public void remove(DictKey element, PythonObjectLibrary keylib, PythonObjectLibrary otherlib) {
783-
removeKey(element, keylib, otherlib);
784-
}
785-
786770
}

0 commit comments

Comments
 (0)