100100import com .oracle .truffle .api .dsl .Specialization ;
101101import com .oracle .truffle .api .frame .VirtualFrame ;
102102import com .oracle .truffle .api .nodes .Node ;
103+ import com .oracle .truffle .api .profiles .ConditionProfile ;
103104
104105@ CoreFunctions (extendClasses = PythonBuiltinClassType .PLruCacheWrapper )
105106public final class LruCacheWrapperBuiltins extends PythonBuiltins {
@@ -332,20 +333,13 @@ Object lruCacheMakeKey(Object kwdMark, Object[] args, PKeyword[] kwds, int typed
332333 }
333334
334335 // infinite_lru_cache_wrapper
335- @ Specialization (guards = "self.isInfinite()" )
336336 Object infiniteLruCacheWrapper (VirtualFrame frame , LruCacheObject self , Object [] args , PKeyword [] kwds ,
337- @ Bind ("this" ) Node inliningTarget ,
338- @ Shared @ Cached PyObjectHashNode hashNode ,
339- @ Shared @ Cached ObjectHashMap .GetNode getItem ,
340- @ Shared @ Cached ObjectHashMap .PutNode setItem ,
341- @ Shared @ Cached InlinedGetClassNode getClassNode ,
342- @ Shared @ Cached PyUnicodeCheckExactNode unicodeCheckExact ,
343- @ Shared @ Cached PyLongCheckExactNode longCheckExact ,
344- @ Shared @ Cached CallVarargsMethodNode callNode ) {
345- Object key = lruCacheMakeKey (self .kwdMark , args , kwds , self .typed ,
346- inliningTarget , getClassNode , unicodeCheckExact , longCheckExact );
347- long hash = hashNode .execute (frame , key );
348- Object result = getItem .get (frame , self .cache , key , hash );
337+ Object key ,
338+ long hash ,
339+ Object cachedItem ,
340+ ObjectHashMap .PutNode setItem ,
341+ CallVarargsMethodNode callNode ) {
342+ Object result = cachedItem ;
349343 if (result != null ) {
350344 self .hits ++;
351345 return result ;
@@ -411,22 +405,17 @@ static void lruCacheExtractLink(LruListElemObject link) {
411405 */
412406
413407 // bounded_lru_cache_wrapper
414- @ Specialization (guards = "self.isBounded()" )
415408 Object boundedLruCacheWrapper (VirtualFrame frame , LruCacheObject self , Object [] args , PKeyword [] kwds ,
416- @ Bind ("this" ) Node inliningTarget ,
417- @ Shared @ Cached PyObjectHashNode hashNode ,
418- @ Shared @ Cached ObjectHashMap .GetNode getItem ,
419- @ Shared @ Cached ObjectHashMap .PutNode setItem ,
420- @ Shared @ Cached InlinedGetClassNode getClassNode ,
421- @ Shared @ Cached PyUnicodeCheckExactNode unicodeCheckExact ,
422- @ Shared @ Cached PyLongCheckExactNode longCheckExact ,
423- @ Cached ObjectHashMap .RemoveNode popItem ,
424- @ Shared @ Cached CallVarargsMethodNode callNode ) {
425- Object key = lruCacheMakeKey (self .kwdMark , args , kwds , self .typed ,
426- inliningTarget , getClassNode , unicodeCheckExact , longCheckExact );
427- long hash = hashNode .execute (frame , key );
428- LruListElemObject link = (LruListElemObject ) getItem .get (frame , self .cache , key , hash );
429- if (link != null ) {
409+ Object key ,
410+ long hash ,
411+ Object cachedItem ,
412+ ObjectHashMap .GetNode getItem ,
413+ ObjectHashMap .PutNode setItem ,
414+ ObjectHashMap .RemoveNode popItem ,
415+ CallVarargsMethodNode callNode ) {
416+ if (cachedItem != null ) {
417+ assert cachedItem instanceof LruListElemObject : "cachedItem should be an LruListElemObject" ;
418+ LruListElemObject link = (LruListElemObject ) cachedItem ;
430419 lruCacheExtractLink (link );
431420 lruCacheAppendLink (self , link );
432421 self .hits ++;
@@ -451,7 +440,7 @@ Object boundedLruCacheWrapper(VirtualFrame frame, LruCacheObject self, Object[]
451440 assert (self .maxsize > 0 );
452441 if (self .cache .size () < self .maxsize || self .root .next == self .root ) {
453442 /* Cache is not full, so put the result in a new link */
454- link = new LruListElemObject ();
443+ LruListElemObject link = new LruListElemObject ();
455444
456445 link .hash = hash ;
457446 link .key = key ;
@@ -464,7 +453,6 @@ Object boundedLruCacheWrapper(VirtualFrame frame, LruCacheObject self, Object[]
464453 */
465454 setItem .put (frame , self .cache , key , hash , link );
466455 lruCacheAppendLink (self , link );
467-
468456 return result ;
469457 }
470458 /*
@@ -480,7 +468,7 @@ Object boundedLruCacheWrapper(VirtualFrame frame, LruCacheObject self, Object[]
480468
481469 /* Extract the oldest item. */
482470 // assert (self.next != self);
483- link = self .root .next ;
471+ LruListElemObject link = self .root .next ;
484472 lruCacheExtractLink (link );
485473 /*
486474 * Remove it from the cache. The cache dict holds one reference to the link. We created
@@ -522,6 +510,27 @@ Object boundedLruCacheWrapper(VirtualFrame frame, LruCacheObject self, Object[]
522510 return result ;
523511 }
524512
513+ @ Specialization (guards = "!self.isUncached()" )
514+ Object cachedLruCacheWrapper (VirtualFrame frame , LruCacheObject self , Object [] args , PKeyword [] kwds ,
515+ @ Bind ("this" ) Node inliningTarget ,
516+ @ Shared @ Cached CallVarargsMethodNode callNode ,
517+ @ Cached PyObjectHashNode hashNode ,
518+ @ Cached ObjectHashMap .GetNode getItem ,
519+ @ Cached ObjectHashMap .PutNode setItem ,
520+ @ Cached InlinedGetClassNode getClassNode ,
521+ @ Cached PyUnicodeCheckExactNode unicodeCheckExact ,
522+ @ Cached PyLongCheckExactNode longCheckExact ,
523+ @ Cached ObjectHashMap .RemoveNode popItem ,
524+ @ Cached ConditionProfile profile ) {
525+ Object key = lruCacheMakeKey (self .kwdMark , args , kwds , self .typed ,
526+ inliningTarget , getClassNode , unicodeCheckExact , longCheckExact );
527+ long hash = hashNode .execute (frame , key );
528+ Object cached = getItem .get (frame , self .cache , key , hash );
529+ if (profile .profile (self .isInfinite ())) {
530+ return infiniteLruCacheWrapper (frame , self , args , kwds , key , hash , cached , setItem , callNode );
531+ }
532+ return boundedLruCacheWrapper (frame , self , args , kwds , key , hash , cached , getItem , setItem , popItem , callNode );
533+ }
525534 }
526535
527536 @ Builtin (name = J___CLEAR__ , minNumOfPositionalArgs = 1 )
0 commit comments