@@ -277,54 +277,50 @@ Object execute(Object[] arguments,
277
277
PythonModule module = factory .createPythonModule (mName );
278
278
279
279
// process HPy methods
280
- {
281
- Object moduleDefines = callGetterNode .call (context , GRAAL_HPY_MODULE_GET_DEFINES , moduleDef );
282
- if (!ptrLib .hasArrayElements (moduleDefines )) {
283
- throw raiseNode .raise (PythonBuiltinClassType .SystemError , "field 'defines' did not return an array" );
284
- }
280
+ Object moduleDefines = callGetterNode .call (context , GRAAL_HPY_MODULE_GET_DEFINES , moduleDef );
281
+ if (!ptrLib .hasArrayElements (moduleDefines )) {
282
+ throw raiseNode .raise (PythonBuiltinClassType .SystemError , "field 'defines' did not return an array" );
283
+ }
285
284
286
- long nModuleDefines = ptrLib .getArraySize (moduleDefines );
287
- for (long i = 0 ; i < nModuleDefines ; i ++) {
288
- Object moduleDefine = ptrLib .readArrayElement (moduleDefines , i );
289
- int kind = castToJavaIntNode .execute (callGetterNode .call (context , GRAAL_HPY_DEF_GET_KIND , moduleDefine ));
290
- switch (kind ) {
291
- case GraalHPyDef .HPY_DEF_KIND_METH :
292
- Object methodDef = callGetterNode .call (context , GRAAL_HPY_DEF_GET_METH , moduleDefine );
293
- PBuiltinFunction fun = addFunctionNode .execute (context , null , methodDef );
294
- PBuiltinMethod method = factory .createBuiltinMethod (module , fun );
295
- writeAttrToMethodNode .execute (method , SpecialAttributeNames .__MODULE__ , mName );
296
- writeAttrNode .execute (module , fun .getName (), method );
297
- break ;
298
- case GraalHPyDef .HPY_DEF_KIND_SLOT :
299
- case GraalHPyDef .HPY_DEF_KIND_MEMBER :
300
- case GraalHPyDef .HPY_DEF_KIND_GETSET :
301
- // silently ignore
302
- // TODO(fa): maybe we should log a warning
303
- break ;
304
- default :
305
- assert false : "unknown definition kind" ;
306
- }
285
+ long nModuleDefines = ptrLib .getArraySize (moduleDefines );
286
+ for (long i = 0 ; i < nModuleDefines ; i ++) {
287
+ Object moduleDefine = ptrLib .readArrayElement (moduleDefines , i );
288
+ int kind = castToJavaIntNode .execute (callGetterNode .call (context , GRAAL_HPY_DEF_GET_KIND , moduleDefine ));
289
+ switch (kind ) {
290
+ case GraalHPyDef .HPY_DEF_KIND_METH :
291
+ Object methodDef = callGetterNode .call (context , GRAAL_HPY_DEF_GET_METH , moduleDefine );
292
+ PBuiltinFunction fun = addFunctionNode .execute (context , null , methodDef );
293
+ PBuiltinMethod method = factory .createBuiltinMethod (module , fun );
294
+ writeAttrToMethodNode .execute (method , SpecialAttributeNames .__MODULE__ , mName );
295
+ writeAttrNode .execute (module , fun .getName (), method );
296
+ break ;
297
+ case GraalHPyDef .HPY_DEF_KIND_SLOT :
298
+ case GraalHPyDef .HPY_DEF_KIND_MEMBER :
299
+ case GraalHPyDef .HPY_DEF_KIND_GETSET :
300
+ // silently ignore
301
+ // TODO(fa): maybe we should log a warning
302
+ break ;
303
+ default :
304
+ assert false : "unknown definition kind" ;
307
305
}
308
306
}
309
307
310
308
// process legacy methods
311
- {
312
- Object legacyMethods = callGetterNode .call (context , GRAAL_HPY_MODULE_GET_LEGACY_METHODS , moduleDef );
313
- // the field 'legacy_methods' may be 'NULL'
314
- if (!ptrLib .isNull (legacyMethods )) {
315
- if (!ptrLib .hasArrayElements (legacyMethods )) {
316
- throw raiseNode .raise (PythonBuiltinClassType .SystemError , "field 'legacyMethods' did not return an array" );
317
- }
318
-
319
- long nLegacyMethods = ptrLib .getArraySize (legacyMethods );
320
- for (long i = 0 ; i < nLegacyMethods ; i ++) {
321
- Object legacyMethod = ptrLib .readArrayElement (legacyMethods , i );
322
-
323
- PBuiltinFunction fun = addLegacyMethodNode .execute (context , legacyMethod );
324
- PBuiltinMethod method = factory .createBuiltinMethod (module , fun );
325
- writeAttrToMethodNode .execute (method .getStorage (), SpecialAttributeNames .__MODULE__ , mName );
326
- writeAttrNode .execute (module , fun .getName (), method );
327
- }
309
+ Object legacyMethods = callGetterNode .call (context , GRAAL_HPY_MODULE_GET_LEGACY_METHODS , moduleDef );
310
+ // the field 'legacy_methods' may be 'NULL'
311
+ if (!ptrLib .isNull (legacyMethods )) {
312
+ if (!ptrLib .hasArrayElements (legacyMethods )) {
313
+ throw raiseNode .raise (PythonBuiltinClassType .SystemError , "field 'legacyMethods' did not return an array" );
314
+ }
315
+
316
+ long nLegacyMethods = ptrLib .getArraySize (legacyMethods );
317
+ for (long i = 0 ; i < nLegacyMethods ; i ++) {
318
+ Object legacyMethod = ptrLib .readArrayElement (legacyMethods , i );
319
+
320
+ PBuiltinFunction fun = addLegacyMethodNode .execute (context , legacyMethod );
321
+ PBuiltinMethod method = factory .createBuiltinMethod (module , fun );
322
+ writeAttrToMethodNode .execute (method .getStorage (), SpecialAttributeNames .__MODULE__ , mName );
323
+ writeAttrNode .execute (module , fun .getName (), method );
328
324
}
329
325
}
330
326
@@ -550,7 +546,7 @@ Object execute(Object[] arguments,
550
546
}
551
547
552
548
try {
553
- Object result = invokeNode .execute (getCallTarget (this , language ), pythonArguments );
549
+ Object result = invokeNode .execute (getCallTarget (language ), pythonArguments );
554
550
return asHandleNode .execute (result );
555
551
} catch (PException e ) {
556
552
transformExceptionToNativeNode .execute (e );
@@ -581,27 +577,28 @@ private void checkArguments(Object[] arguments) throws ArityException {
581
577
}
582
578
}
583
579
584
- private RootCallTarget getCallTarget (GraalHPyArithmetic receiver , PythonLanguage language ) {
585
- RootCallTarget callTarget = receiver .callTarget ;
580
+ private RootCallTarget getCallTarget (PythonLanguage language ) {
586
581
if (callTarget == null ) {
587
582
CompilerDirectives .transferToInterpreterAndInvalidate ();
588
583
callTarget = createCallTarget (language );
589
- receiver .callTarget = callTarget ;
590
584
}
591
585
return callTarget ;
592
586
}
593
587
594
588
@ TruffleBoundary
595
589
private RootCallTarget createCallTarget (PythonLanguage language ) {
596
590
Pair <PythonLanguage , Object > key = Pair .create (language , getOp ());
597
- WeakReference <RootCallTarget > ctRef = weakCallTargetMap .get (key );
598
- RootCallTarget callTarget = ctRef != null ? ctRef .get () : null ;
599
- if (callTarget == null ) {
600
- callTarget = PythonUtils .getOrCreateCallTarget (new CallArithmeticRootNode (language , unaryOperator , binaryOperator , inplaceOperator , ternaryOperator ));
601
- weakCallTargetMap .put (key , new WeakReference <>(callTarget ));
591
+ RootCallTarget cachedCallTarget ;
592
+ synchronized (weakCallTargetMap ) {
593
+ WeakReference <RootCallTarget > ctRef = weakCallTargetMap .get (key );
594
+ cachedCallTarget = ctRef != null ? ctRef .get () : null ;
595
+ if (cachedCallTarget == null ) {
596
+ cachedCallTarget = PythonUtils .getOrCreateCallTarget (new CallArithmeticRootNode (language , unaryOperator , binaryOperator , inplaceOperator , ternaryOperator ));
597
+ weakCallTargetMap .put (key , new WeakReference <>(cachedCallTarget ));
598
+ }
602
599
}
603
- assert callTarget != null ;
604
- return callTarget ;
600
+ assert cachedCallTarget != null ;
601
+ return cachedCallTarget ;
605
602
}
606
603
607
604
private Object getOp () {
0 commit comments