Skip to content

Commit 1c14069

Browse files
committed
add TruffleBoundaries around many calls to Java standard library methods
1 parent 9c01462 commit 1c14069

27 files changed

+140
-21
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ public Object createInt(PythonClass cls, String arg, Object keywordArg) {
11221122
return factory().createInt(cls, (int) value);
11231123
}
11241124
} else {
1125+
CompilerDirectives.transferToInterpreter();
11251126
throw new RuntimeException("Not implemented integer with base: " + keywordArg);
11261127
}
11271128
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ public abstract static class BinNode extends PythonUnaryBuiltinNode {
242242

243243
public abstract String executeObject(Object x);
244244

245+
@TruffleBoundary
245246
private static String buildString(boolean isNegative, String number) {
246247
StringBuilder sb = new StringBuilder();
247248
if (isNegative) {
@@ -254,7 +255,12 @@ private static String buildString(boolean isNegative, String number) {
254255

255256
@Specialization
256257
public String doL(long x) {
257-
return buildString(x < 0, Long.toBinaryString(Math.abs(x)));
258+
return buildString(x < 0, longToBinaryString(x));
259+
}
260+
261+
@TruffleBoundary
262+
private static String longToBinaryString(long x) {
263+
return Long.toBinaryString(Math.abs(x));
258264
}
259265

260266
@Specialization
@@ -1287,7 +1293,7 @@ public Object print(PTuple values, String sep, String end, Object file, boolean
12871293
append(sb, " ");
12881294
}
12891295
append(sb, callStr.executeObject(getItemNode.execute(store, store.length() - 1)));
1290-
sb.append(end);
1296+
append(sb, end);
12911297
write(context, sb.toString());
12921298
}
12931299
} catch (IOException e) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PosixModuleBuiltins.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ Object doStatDefault(String path, @SuppressWarnings("unused") PNone followSymlin
378378
return stat(path, true);
379379
}
380380

381+
@TruffleBoundary
382+
long fileTimeToSeconds(FileTime t) {
383+
return t.to(TimeUnit.SECONDS);
384+
}
385+
381386
Object stat(String path, boolean followSymlinks) {
382387
TruffleFile f = getContext().getEnv().getTruffleFile(path);
383388
LinkOption[] linkOptions = followSymlinks ? new LinkOption[0] : new LinkOption[]{LinkOption.NOFOLLOW_LINKS};
@@ -402,34 +407,34 @@ Object stat(String path, boolean followSymlinks) {
402407
mode |= S_IFSOCK | S_IFBLK | S_IFCHR | S_IFIFO;
403408
}
404409
try {
405-
mtime = f.getLastModifiedTime(linkOptions).to(TimeUnit.SECONDS);
410+
mtime = fileTimeToSeconds(f.getLastModifiedTime(linkOptions));
406411
} catch (IOException e1) {
407412
mtime = 0;
408413
}
409414
try {
410-
ctime = f.getCreationTime(linkOptions).to(TimeUnit.SECONDS);
415+
ctime = fileTimeToSeconds(f.getCreationTime(linkOptions));
411416
} catch (IOException e1) {
412417
ctime = 0;
413418
}
414419
try {
415-
atime = f.getLastAccessTime(linkOptions).to(TimeUnit.SECONDS);
420+
atime = fileTimeToSeconds(f.getLastAccessTime(linkOptions));
416421
} catch (IOException e1) {
417422
atime = 0;
418423
}
419424
UserPrincipal owner;
420425
try {
421426
owner = f.getOwner(linkOptions);
422427
if (owner instanceof UnixNumericUserPrincipal) {
423-
uid = new Long(((UnixNumericUserPrincipal) owner).getName()).longValue();
428+
uid = strToLong(((UnixNumericUserPrincipal) owner).getName());
424429
}
425-
} catch (IOException | UnsupportedOperationException | SecurityException e2) {
430+
} catch (NumberFormatException | IOException | UnsupportedOperationException | SecurityException e2) {
426431
}
427432
try {
428433
GroupPrincipal group = f.getGroup(linkOptions);
429434
if (group instanceof UnixNumericGroupPrincipal) {
430-
gid = new Long(((UnixNumericGroupPrincipal) group).getName()).longValue();
435+
gid = strToLong(((UnixNumericGroupPrincipal) group).getName());
431436
}
432-
} catch (IOException | UnsupportedOperationException | SecurityException e2) {
437+
} catch (NumberFormatException | IOException | UnsupportedOperationException | SecurityException e2) {
433438
}
434439
try {
435440
final Set<PosixFilePermission> posixFilePermissions = f.getPosixPermissions(linkOptions);
@@ -470,6 +475,11 @@ Object stat(String path, boolean followSymlinks) {
470475
});
471476
}
472477

478+
@TruffleBoundary(allowInlining = true, transferToInterpreterOnException = false)
479+
private static long strToLong(String name) throws NumberFormatException {
480+
return new Long(name).longValue();
481+
}
482+
473483
@TruffleBoundary(allowInlining = true)
474484
private static int posixPermissionsToMode(int inputMode, final Set<PosixFilePermission> posixFilePermissions) {
475485
int mode = inputMode;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ReadlineModuleBuiltins.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ PNone setCompleter(PythonModule self, String path,
296296
@GenerateNodeFactory
297297
abstract static class ClearNode extends PythonUnaryBuiltinNode {
298298
@Specialization
299+
@TruffleBoundary
299300
PNone setCompleter(PythonModule self,
300301
@Cached("create()") ReadAttributeFromObjectNode readNode) {
301302
LocalData data = (LocalData) readNode.execute(self, DATA);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cell/PCell.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
4848
import com.oracle.truffle.api.Assumption;
4949
import com.oracle.truffle.api.CompilerAsserts;
50+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5051
import com.oracle.truffle.api.Truffle;
5152

5253
public class PCell extends PythonBuiltinObject {
@@ -79,6 +80,7 @@ public Assumption isEffectivelyFinalAssumption() {
7980
}
8081

8182
@Override
83+
@TruffleBoundary
8284
public List<String> getAttributeNames() {
8385
ArrayList<String> arrayList = new ArrayList<>();
8486
arrayList.add("cell_contents");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ private static String extractFileName(RootNode rootNode) {
184184
}
185185
}
186186

187+
@TruffleBoundary
187188
private static int extractFirstLineno(RootNode rootNode) {
188189
RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode) ? ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode() : rootNode;
189190
SourceSection sourceSection = funcRootNode.getSourceSection();

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ public boolean hasKey(Object key, Equivalence eq) {
209209
if (super.hasKey(key, DEFAULT_EQIVALENCE)) {
210210
return true;
211211
}
212+
return hasKeyNonAttr(key, eq);
213+
}
214+
215+
@TruffleBoundary
216+
private boolean hasKeyNonAttr(Object key, Equivalence eq) {
212217
return nonAttributesStorage.hasKey(key, eq);
213218
}
214219

@@ -226,19 +231,30 @@ public void setItem(Object key, Object value, Equivalence eq) {
226231
if (key instanceof String) {
227232
super.setItem(key, value, DEFAULT_EQIVALENCE);
228233
} else {
229-
this.nonAttributesStorage.setItem(key, value, eq);
234+
setItemNonAttr(key, value, eq);
230235
}
231236
}
232237

238+
@TruffleBoundary
239+
private void setItemNonAttr(Object key, Object value, Equivalence eq) {
240+
this.nonAttributesStorage.setItem(key, value, eq);
241+
}
242+
233243
@Override
234244
public boolean remove(Object key, Equivalence eq) {
235245
if (super.remove(key, DEFAULT_EQIVALENCE)) {
236246
return true;
237247
}
248+
return removeNonAttr(key, eq);
249+
}
250+
251+
@TruffleBoundary
252+
private boolean removeNonAttr(Object key, Equivalence eq) {
238253
return this.nonAttributesStorage.remove(key, eq);
239254
}
240255

241256
@Override
257+
@TruffleBoundary
242258
public Iterable<Object> keys() {
243259
if (this.nonAttributesStorage.length() == 0) {
244260
return super.keys();
@@ -255,6 +271,7 @@ public Iterable<Object> keys() {
255271
}
256272

257273
@Override
274+
@TruffleBoundary
258275
public Iterable<Object> values() {
259276
if (this.nonAttributesStorage.length() == 0) {
260277
return super.values();
@@ -271,6 +288,7 @@ public Iterable<Object> values() {
271288
}
272289

273290
@Override
291+
@TruffleBoundary
274292
public Iterable<DictEntry> entries() {
275293
if (this.nonAttributesStorage.length() == 0) {
276294
return super.entries();
@@ -289,12 +307,22 @@ public Iterable<DictEntry> entries() {
289307
@Override
290308
public void clear() {
291309
super.clear();
310+
clearNonAttrs();
311+
}
312+
313+
@TruffleBoundary
314+
private void clearNonAttrs() {
292315
this.nonAttributesStorage.clear();
293316
}
294317

295318
@Override
296319
public HashingStorage copy(Equivalence eq) {
297-
return new PythonObjectHybridDictStorage(getStore().copy(getStore().getShape()), (EconomicMapStorage) nonAttributesStorage.copy(eq));
320+
return new PythonObjectHybridDictStorage(getStore().copy(getStore().getShape()), (EconomicMapStorage) copyNonAttrs(eq));
321+
}
322+
323+
@TruffleBoundary
324+
private HashingStorage copyNonAttrs(Equivalence eq) {
325+
return nonAttributesStorage.copy(eq);
298326
}
299327
}
300328
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.Objects;
4646

4747
import com.oracle.truffle.api.CompilerAsserts;
48+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4849

4950
/**
5051
* Implementation of a map with a memory-efficient structure that always preserves insertion order
@@ -156,6 +157,7 @@ private EconomicMapStorage(EconomicMapStorage other, boolean isSet, Equivalence
156157
}
157158
}
158159

160+
@TruffleBoundary
159161
private boolean initFrom(Object o) {
160162
if (o instanceof EconomicMapStorage) {
161163
EconomicMapStorage otherMap = (EconomicMapStorage) o;
@@ -761,6 +763,7 @@ public Iterable<Object> keys() {
761763
}
762764

763765
@Override
766+
@TruffleBoundary
764767
public Iterable<Object> values() {
765768
return new Iterable<Object>() {
766769
@Override
@@ -797,6 +800,7 @@ public Iterator<DictEntry> iterator() {
797800
}
798801

799802
@Override
803+
@TruffleBoundary
800804
public HashingStorage copy(Equivalence eq) {
801805
return new EconomicMapStorage(this, this.isSet, eq);
802806
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
import java.util.Collections;
4444

45+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
46+
4547
public class EmptyStorage extends HashingStorage {
4648

4749
@Override
@@ -80,6 +82,7 @@ public Iterable<Object> keys() {
8082
}
8183

8284
@Override
85+
@TruffleBoundary
8386
public Iterable<Object> values() {
8487
return Collections.emptyList();
8588
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public Iterable<Object> items() {
104104
}
105105

106106
@Override
107+
@TruffleBoundary
107108
public Iterable<Object> keys() {
108109
ArrayList<Object> keys = new ArrayList<>(map.size());
109110
for (Object key : map.keySet()) {

0 commit comments

Comments
 (0)