Skip to content

Commit 668d508

Browse files
committed
add guard so we do not recurse in the simple import case
1 parent e77d92a commit 668d508

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/AbstractImportNode.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7777
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
7878
import com.oracle.truffle.api.TruffleLanguage.LanguageReference;
79+
import com.oracle.truffle.api.dsl.Bind;
7980
import com.oracle.truffle.api.dsl.Cached;
8081
import com.oracle.truffle.api.dsl.Specialization;
8182
import com.oracle.truffle.api.frame.MaterializedFrame;
@@ -203,8 +204,13 @@ Object levelLtZero(VirtualFrame frame, PythonContext context, String name, Objec
203204
throw PRaiseNode.raiseUncached(this, PythonBuiltinClassType.TypeError, "level must be >= 0");
204205
}
205206

206-
@Specialization(guards = {"level == 0", "fromList.length == 0"})
207+
protected static final int indexOfDot(String name) {
208+
return name.indexOf('.');
209+
}
210+
211+
@Specialization(guards = {"level == 0", "fromList.length == 0", "dotIndex < 0"})
207212
static Object levelZeroNoFromlist(VirtualFrame frame, PythonContext context, String name, @SuppressWarnings("unused") Object globals, @SuppressWarnings("unused") String[] fromList, @SuppressWarnings("unused") int level,
213+
@SuppressWarnings("unused") @Bind("indexOfDot(name)") int dotIndex,
208214
@Cached PRaiseNode raiseNode,
209215
@Cached PyDictGetItem getModuleNode,
210216
@Cached EnsureInitializedNode ensureInitialized,
@@ -220,17 +226,10 @@ static Object levelZeroNoFromlist(VirtualFrame frame, PythonContext context, Str
220226
} else {
221227
mod = findAndLoad.execute(frame, context, absName);
222228
}
223-
int dotIndex = name.indexOf('.');
224-
if (dotIndex == -1) {
225-
return mod;
226-
}
227-
String front = name.substring(0, dotIndex);
228-
// recursion up number of dots in the name
229-
return levelZeroNoFromlist(frame, context, front, null, null, 0,
230-
raiseNode, // raiseNode only needed if front.length() == 0 at this point
231-
getModuleNode, // used multiple times to get the 'front' module
232-
ensureInitialized, // used multiple times on the 'front' module
233-
findAndLoad); // used multiple times, but always to call the exact same function
229+
// Here CPython has a check for fromlist being empty, the level being 0, and the index
230+
// of the first dot. All these are guards in this specialization, so we can just
231+
// return.
232+
return mod;
234233
}
235234

236235
@Specialization(guards = "level >= 0", replaces = "levelZeroNoFromlist")

0 commit comments

Comments
 (0)