100
100
import com .oracle .truffle .api .dsl .Specialization ;
101
101
import com .oracle .truffle .api .frame .VirtualFrame ;
102
102
import com .oracle .truffle .api .nodes .Node ;
103
+ import com .oracle .truffle .api .profiles .ConditionProfile ;
103
104
104
105
@ CoreFunctions (extendClasses = PythonBuiltinClassType .PLruCacheWrapper )
105
106
public final class LruCacheWrapperBuiltins extends PythonBuiltins {
@@ -332,20 +333,13 @@ Object lruCacheMakeKey(Object kwdMark, Object[] args, PKeyword[] kwds, int typed
332
333
}
333
334
334
335
// infinite_lru_cache_wrapper
335
- @ Specialization (guards = "self.isInfinite()" )
336
336
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 ;
349
343
if (result != null ) {
350
344
self .hits ++;
351
345
return result ;
@@ -411,22 +405,17 @@ static void lruCacheExtractLink(LruListElemObject link) {
411
405
*/
412
406
413
407
// bounded_lru_cache_wrapper
414
- @ Specialization (guards = "self.isBounded()" )
415
408
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 ;
430
419
lruCacheExtractLink (link );
431
420
lruCacheAppendLink (self , link );
432
421
self .hits ++;
@@ -451,7 +440,7 @@ Object boundedLruCacheWrapper(VirtualFrame frame, LruCacheObject self, Object[]
451
440
assert (self .maxsize > 0 );
452
441
if (self .cache .size () < self .maxsize || self .root .next == self .root ) {
453
442
/* Cache is not full, so put the result in a new link */
454
- link = new LruListElemObject ();
443
+ LruListElemObject link = new LruListElemObject ();
455
444
456
445
link .hash = hash ;
457
446
link .key = key ;
@@ -464,7 +453,6 @@ Object boundedLruCacheWrapper(VirtualFrame frame, LruCacheObject self, Object[]
464
453
*/
465
454
setItem .put (frame , self .cache , key , hash , link );
466
455
lruCacheAppendLink (self , link );
467
-
468
456
return result ;
469
457
}
470
458
/*
@@ -480,7 +468,7 @@ Object boundedLruCacheWrapper(VirtualFrame frame, LruCacheObject self, Object[]
480
468
481
469
/* Extract the oldest item. */
482
470
// assert (self.next != self);
483
- link = self .root .next ;
471
+ LruListElemObject link = self .root .next ;
484
472
lruCacheExtractLink (link );
485
473
/*
486
474
* 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[]
522
510
return result ;
523
511
}
524
512
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
+ }
525
534
}
526
535
527
536
@ Builtin (name = J___CLEAR__ , minNumOfPositionalArgs = 1 )
0 commit comments