Skip to content

Commit 95e847a

Browse files
committed
Convert LruListElemObject to internal java class
and clean up
1 parent f5fb844 commit 95e847a

File tree

4 files changed

+12
-35
lines changed

4 files changed

+12
-35
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/functools/LruCacheObject.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,18 @@
4141
package com.oracle.graal.python.builtins.modules.functools;
4242

4343
import com.oracle.graal.python.builtins.objects.common.ObjectHashMap;
44+
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
4445
import com.oracle.truffle.api.object.Shape;
4546

46-
public class LruCacheObject extends LruListElemObject {
47+
public class LruCacheObject extends PythonBuiltinObject {
4748

4849
enum WrapperType {
4950
INFINITE,
5051
UNCACHED,
5152
BOUNDED,
5253
}
5354

55+
LruListElemObject root;
5456
WrapperType wrapper;
5557
int typed;
5658
final ObjectHashMap cache;
@@ -67,6 +69,7 @@ enum WrapperType {
6769

6870
public LruCacheObject(Object cls, Shape instanceShape) {
6971
super(cls, instanceShape);
72+
this.root = new LruListElemObject();
7073
this.cache = new ObjectHashMap();
7174
}
7275

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/functools/LruCacheWrapperBuiltins.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ Object lruCacheNew(VirtualFrame frame, Object type,
172172

173173
LruCacheObject obj = factory().createLruCacheObject(type);
174174

175-
obj.prev = obj;
176-
obj.next = obj;
175+
obj.root.prev = obj.root;
176+
obj.root.next = obj.root;
177177
obj.wrapper = wrapper;
178178
obj.typed = typed;
179179
// obj.cache = new ObjectHashMap();
@@ -184,8 +184,6 @@ Object lruCacheNew(VirtualFrame frame, Object type,
184184

185185
obj.kwdMark = getKwdMark(readAttr);
186186

187-
// obj.lru_list_elem_type = state.lru_list_elem_type;
188-
189187
obj.cacheInfoType = cache_info_type;
190188
// obj.dict = null;
191189
// obj.weakreflist = null;
@@ -370,7 +368,7 @@ static void lru_cache_prepend_link(LruCacheObject self, LruListElemObject link)
370368

371369
// lru_cache_append_link
372370
static void lruCacheAppendLink(LruCacheObject self, LruListElemObject link) {
373-
LruListElemObject root = self;
371+
LruListElemObject root = self.root;
374372
LruListElemObject last = root.prev;
375373
last.next = root.prev = link;
376374
link.prev = last;
@@ -434,10 +432,6 @@ Object boundedLruCacheWrapper(VirtualFrame frame, LruCacheObject self, Object[]
434432
self.hits++;
435433
return link.result;
436434
}
437-
// mq: this might be needed if self.cache is a dict
438-
// if (PyErr_Occurred()) {
439-
// return NULL;
440-
// }
441435
self.misses++;
442436
Object result = callNode.execute(frame, self.func, args, kwds);
443437
Object testresult = getItem.get(frame, self.cache, key, hash);
@@ -449,23 +443,15 @@ Object boundedLruCacheWrapper(VirtualFrame frame, LruCacheObject self, Object[]
449443
*/
450444
return result;
451445
}
452-
// mq: this will only occur if the cache is a dict.
453-
// if (PyErr_Occurred()) {
454-
// /* This is an unusual case since this same lookup
455-
// did not previously trigger an error during lookup.
456-
// Treat it the same as an error in user function
457-
// and return with the error set. */
458-
// return NULL;
459-
// }
460446
/*
461447
* This is the normal case. The new key wasn't found before user function call and it is
462448
* still not there. So we proceed normally and update the cache with the new result.
463449
*/
464450

465451
assert (self.maxsize > 0);
466-
if (self.cache.size() < self.maxsize || self.next == self) {
452+
if (self.cache.size() < self.maxsize || self.root.next == self.root) {
467453
/* Cache is not full, so put the result in a new link */
468-
link = factory().createLruListElemObject();
454+
link = new LruListElemObject();
469455

470456
link.hash = hash;
471457
link.key = key;
@@ -494,7 +480,7 @@ Object boundedLruCacheWrapper(VirtualFrame frame, LruCacheObject self, Object[]
494480

495481
/* Extract the oldest item. */
496482
// assert (self.next != self);
497-
link = self.next;
483+
link = self.root.next;
498484
lruCacheExtractLink(link);
499485
/*
500486
* Remove it from the cache. The cache dict holds one reference to the link. We created
@@ -544,7 +530,7 @@ public abstract static class ClearNode extends PythonUnaryBuiltinNode {
544530

545531
// lru_cache_unlink_list
546532
static LruListElemObject lruCacheUnlinkList(LruCacheObject self) {
547-
LruListElemObject root = self;
533+
LruListElemObject root = self.root;
548534
LruListElemObject link = root.next;
549535
if (link == root) {
550536
return null;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/functools/LruListElemObject.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,10 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules.functools;
4242

43-
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
44-
import com.oracle.truffle.api.object.Shape;
45-
46-
public class LruListElemObject extends PythonBuiltinObject {
43+
final class LruListElemObject {
4744

4845
LruListElemObject prev, next; /* borrowed links */
4946
long hash;
5047
Object key, result;
5148

52-
public LruListElemObject(Object cls, Shape instanceShape) {
53-
super(cls, instanceShape);
54-
}
55-
5649
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import com.oracle.graal.python.builtins.modules.ctypes.StructParamObject;
5959
import com.oracle.graal.python.builtins.modules.ctypes.memory.Pointer;
6060
import com.oracle.graal.python.builtins.modules.functools.LruCacheObject;
61-
import com.oracle.graal.python.builtins.modules.functools.LruListElemObject;
6261
import com.oracle.graal.python.builtins.modules.functools.PKeyWrapper;
6362
import com.oracle.graal.python.builtins.modules.functools.PPartial;
6463
import com.oracle.graal.python.builtins.modules.hashlib.DigestObject;
@@ -835,10 +834,6 @@ public final PPartial createPartial(Object cls, Object function, Object[] args,
835834
return trace(new PPartial(cls, getShape(cls), function, args, kwDict));
836835
}
837836

838-
public LruListElemObject createLruListElemObject() {
839-
return trace(new LruListElemObject(PythonBuiltinClassType.PLruListElem, getShape(PythonBuiltinClassType.PLruListElem)));
840-
}
841-
842837
public LruCacheObject createLruCacheObject(Object cls) {
843838
return trace(new LruCacheObject(cls, getShape(cls)));
844839
}

0 commit comments

Comments
 (0)