Skip to content

Commit 243eaf0

Browse files
committed
[GR-13442] Experimental splitting is now default.
PullRequest: graalpython/372
2 parents fc830bf + d2d9ee6 commit 243eaf0

File tree

6 files changed

+54
-27
lines changed

6 files changed

+54
-27
lines changed

ci.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
overlay: "3cf78c3623442ad827eed58a1780784a6eb95676",
2+
overlay: "e08ca6c848943a04c5e46d54272ff927ff3d51f7",
33

44
// ======================================================================================================
55
//

graalpython/com.oracle.graal.python.test/src/tests/test_random.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates.
1+
# Copyright (c) 2018, 2019, Oracle and/or its affiliates.
22
# Copyright (C) 1996-2017 Python Software Foundation
33
#
44
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/random/RandomBuiltins.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -51,14 +51,19 @@
5151
import com.oracle.graal.python.builtins.objects.ints.PInt;
5252
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
5353
import com.oracle.graal.python.nodes.PGuards;
54+
import com.oracle.graal.python.nodes.SpecialMethodNames;
5455
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
5556
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
57+
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
5658
import com.oracle.graal.python.runtime.exception.PythonErrorType;
59+
import com.oracle.truffle.api.CompilerDirectives;
60+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
5761
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
58-
import com.oracle.truffle.api.dsl.Cached;
62+
import com.oracle.truffle.api.dsl.Fallback;
5963
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6064
import com.oracle.truffle.api.dsl.NodeFactory;
6165
import com.oracle.truffle.api.dsl.Specialization;
66+
import com.oracle.truffle.api.dsl.TypeSystemReference;
6267
import com.oracle.truffle.api.nodes.UnexpectedResultException;
6368

6469
@CoreFunctions(extendClasses = PythonBuiltinClassType.PRandom)
@@ -70,6 +75,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinNode>> getNodeFactor
7075

7176
@Builtin(name = "seed", fixedNumOfPositionalArgs = 2)
7277
@GenerateNodeFactory
78+
@TypeSystemReference(PythonArithmeticTypes.class)
7379
public abstract static class SeedNode extends PythonBuiltinNode {
7480

7581
@Specialization
@@ -80,7 +86,7 @@ public PNone seed(PRandom random, @SuppressWarnings("unused") PNone none) {
8086
}
8187

8288
@Specialization
83-
public PNone seed(PRandom random, int inputSeed) {
89+
public PNone seed(PRandom random, long inputSeed) {
8490
random.setSeed(inputSeed);
8591
return PNone.NONE;
8692
}
@@ -97,26 +103,47 @@ public PNone seed(PRandom random, double inputSeed) {
97103
return PNone.NONE;
98104
}
99105

100-
@Specialization(rewriteOn = UnexpectedResultException.class)
101-
public PNone seedObject(PRandom random, Object inputSeed,
102-
@Cached("create(__HASH__)") LookupAndCallUnaryNode callHash) throws UnexpectedResultException {
103-
long hash = callHash.executeLong(inputSeed);
104-
random.setSeed(hash);
105-
return PNone.NONE;
106-
}
106+
@CompilationFinal boolean gotUnexpectedHashResult = false;
107+
@Child LookupAndCallUnaryNode callHash;
107108

108-
@Specialization(replaces = "seedObject")
109-
public PNone seedNonLong(PRandom random, Object inputSeed,
110-
@Cached("create(__HASH__)") LookupAndCallUnaryNode callHash) {
111-
Object object = callHash.executeObject(inputSeed);
112-
if (PGuards.isInteger(object)) {
113-
random.setSeed(((Number) object).intValue());
114-
} else if (PGuards.isPInt(object)) {
115-
random.setSeed(((PInt) object).intValue());
109+
@Fallback
110+
public PNone seedNonLong(Object random, Object inputSeed) {
111+
if (random instanceof PRandom) {
112+
if (callHash == null) {
113+
CompilerDirectives.transferToInterpreterAndInvalidate();
114+
callHash = insert(LookupAndCallUnaryNode.create(SpecialMethodNames.__HASH__));
115+
}
116+
Object hashResult = null;
117+
if (!gotUnexpectedHashResult) {
118+
try {
119+
long hash = callHash.executeLong(inputSeed);
120+
((PRandom) random).setSeed(hash);
121+
return PNone.NONE;
122+
} catch (UnexpectedResultException e) {
123+
CompilerDirectives.transferToInterpreterAndInvalidate();
124+
gotUnexpectedHashResult = true;
125+
hashResult = e.getResult();
126+
}
127+
}
128+
if (gotUnexpectedHashResult) {
129+
if (hashResult == null) {
130+
hashResult = callHash.executeObject(inputSeed);
131+
}
132+
if (PGuards.isInteger(hashResult)) {
133+
((PRandom) random).setSeed(((Number) hashResult).intValue());
134+
} else if (PGuards.isPInt(hashResult)) {
135+
((PRandom) random).setSeed(((PInt) hashResult).intValue());
136+
} else {
137+
throw raise(PythonErrorType.TypeError, "__hash__ method should return an integer");
138+
}
139+
return PNone.NONE;
140+
} else {
141+
assert false : "cannot reach here";
142+
return PNone.NONE;
143+
}
116144
} else {
117-
throw raise(PythonErrorType.TypeError, "__hash__ method should return an integer");
145+
throw raise(PythonErrorType.TypeError, "descriptor 'seed' requires a '_random.Random' object but received a '%p'", random);
118146
}
119-
return PNone.NONE;
120147
}
121148
}
122149

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/InvokeNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2014, Regents of the University of California
44
*
55
* All rights reserved.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ private PythonOptions() {
123123
@Option(category = OptionCategory.EXPERT, help = "Switch on/off using lazy strings for performance reasons. Default true.") //
124124
public static final OptionKey<Boolean> LazyStrings = new OptionKey<>(true);
125125

126-
@Option(category = OptionCategory.EXPERT, help = "Enable forced splitting (of builtins). Default true.") //
127-
public static final OptionKey<Boolean> EnableForcedSplits = new OptionKey<>(true);
126+
@Option(category = OptionCategory.EXPERT, help = "Enable forced splitting (of builtins). Default false.") //
127+
public static final OptionKey<Boolean> EnableForcedSplits = new OptionKey<>(false);
128128

129129
@Option(category = OptionCategory.EXPERT, help = "Set by the launcher if an interactive console is used to run Python.") //
130130
public static final OptionKey<Boolean> TerminalIsInteractive = new OptionKey<>(false);

mx.graalpython/suite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@
4343
},
4444
{
4545
"name": "sulong",
46-
"version": "db68412a9a704b40d65cfa45be71097ec88258d2",
46+
"version": "526f76f44f59943be852c07e6999d08401b40962",
4747
"subdir": True,
4848
"urls": [
4949
{"url": "https://github.com/oracle/graal", "kind": "git"},
5050
]
5151
},
5252
{
5353
"name": "regex",
54-
"version": "db68412a9a704b40d65cfa45be71097ec88258d2",
54+
"version": "526f76f44f59943be852c07e6999d08401b40962",
5555
"subdir": True,
5656
"urls": [
5757
{"url": "https://github.com/oracle/graal", "kind": "git"},

0 commit comments

Comments
 (0)