|
12 | 12 | from devito.ir.support.basic import (IterationInstance, TimedAccess, Scope, |
13 | 13 | Vector, AFFINE, REGULAR, IRREGULAR, mocksym0, |
14 | 14 | mocksym1) |
| 15 | +from devito.ir.support.caching import CacheInstances |
15 | 16 | from devito.ir.support.space import (NullInterval, Interval, Forward, Backward, |
16 | 17 | IntervalGroup, IterationSpace) |
17 | 18 | from devito.ir.support.guards import GuardOverflow |
@@ -1101,3 +1102,46 @@ def test_guard_overflow(self): |
1101 | 1102 | guard = GuardOverflow(freespace, size) |
1102 | 1103 |
|
1103 | 1104 | assert ccode(guard) == 'freespace >= f_vec->size[0]*f_vec->size[1]' |
| 1105 | + |
| 1106 | + |
| 1107 | +class TestCacheInstances: |
| 1108 | + |
| 1109 | + def test_caching(self): |
| 1110 | + """ |
| 1111 | + Tests basic functionality of cached instances. |
| 1112 | + """ |
| 1113 | + class Object(CacheInstances): |
| 1114 | + def __init__(self, value: int): |
| 1115 | + self.value = value |
| 1116 | + |
| 1117 | + obj1 = Object(1) |
| 1118 | + obj2 = Object(1) |
| 1119 | + obj3 = Object(2) |
| 1120 | + |
| 1121 | + assert obj1 is obj2 |
| 1122 | + assert obj1 is not obj3 |
| 1123 | + |
| 1124 | + def test_cache_size(self): |
| 1125 | + """ |
| 1126 | + Tests specifying the size of the instance cache. |
| 1127 | + """ |
| 1128 | + class Object(CacheInstances): |
| 1129 | + _instance_cache_size = 2 |
| 1130 | + |
| 1131 | + def __init__(self, value: int): |
| 1132 | + self.value = value |
| 1133 | + |
| 1134 | + obj1 = Object(1) |
| 1135 | + obj2 = Object(2) |
| 1136 | + obj3 = Object(3) |
| 1137 | + obj4 = Object(1) |
| 1138 | + obj5 = Object(3) |
| 1139 | + |
| 1140 | + # obj1 should have been evicted before obj4 was created |
| 1141 | + assert obj1 is not obj4 |
| 1142 | + assert obj1 is not obj2 |
| 1143 | + assert obj3 is obj5 |
| 1144 | + |
| 1145 | + hits, _, _, cursize = Object._instance_cache.cache_info() |
| 1146 | + assert hits == 1 # obj5 hit the cache |
| 1147 | + assert cursize == 2 |
0 commit comments