76
76
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
77
77
import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
78
78
import com .oracle .truffle .api .TruffleLanguage .LanguageReference ;
79
+ import com .oracle .truffle .api .dsl .Bind ;
79
80
import com .oracle .truffle .api .dsl .Cached ;
80
81
import com .oracle .truffle .api .dsl .Specialization ;
81
82
import com .oracle .truffle .api .frame .MaterializedFrame ;
@@ -203,8 +204,13 @@ Object levelLtZero(VirtualFrame frame, PythonContext context, String name, Objec
203
204
throw PRaiseNode .raiseUncached (this , PythonBuiltinClassType .TypeError , "level must be >= 0" );
204
205
}
205
206
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" })
207
212
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 ,
208
214
@ Cached PRaiseNode raiseNode ,
209
215
@ Cached PyDictGetItem getModuleNode ,
210
216
@ Cached EnsureInitializedNode ensureInitialized ,
@@ -220,17 +226,10 @@ static Object levelZeroNoFromlist(VirtualFrame frame, PythonContext context, Str
220
226
} else {
221
227
mod = findAndLoad .execute (frame , context , absName );
222
228
}
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 ;
234
233
}
235
234
236
235
@ Specialization (guards = "level >= 0" , replaces = "levelZeroNoFromlist" )
0 commit comments