[pypy-commit] pypy faster-rstruct-2: polish things a bit: rename rlist.make_LIST into LIST_OF, and use it everywhere instead of the ugly _ResizableListSupportingRawPtr._get_lltype()

antocuni pypy.commits at gmail.com
Tue May 9 10:18:01 EDT 2017


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: faster-rstruct-2
Changeset: r91207:bc449c2983bb
Date: 2017-05-09 16:17 +0200
http://bitbucket.org/pypy/pypy/changeset/bc449c2983bb/

Log:	polish things a bit: rename rlist.make_LIST into LIST_OF, and use it
	everywhere instead of the ugly
	_ResizableListSupportingRawPtr._get_lltype()

diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -1256,10 +1256,9 @@
     def typed_read(self, TP, byte_offset):
         from rpython.rtyper.lltypesystem import lltype, llmemory
         from rpython.rtyper.lltypesystem.lloperation import llop
-        from rpython.rlib.rgc import get_LIST_OF_CHAR
-        LIST = get_LIST_OF_CHAR()
         ll_data = ll_for_resizable_list(self.data)
         ll_items = ll_data.items
+        LIST = lltype.typeOf(ll_data).TO # rlist.LIST_OF(lltype.Char)
         base_ofs = llmemory.itemoffsetof(LIST.items.TO, 0)
         scale_factor = llmemory.sizeof(lltype.Char)
         return llop.gc_load_indexed(TP, ll_items, byte_offset,
diff --git a/rpython/rlib/rgc.py b/rpython/rlib/rgc.py
--- a/rpython/rlib/rgc.py
+++ b/rpython/rlib/rgc.py
@@ -1051,15 +1051,7 @@
     rgc.nonmoving_raw_ptr_for_resizable_list() might be
     used if needed.  For now, only supports lists of chars.
     """
-    __slots__ = ('_ll_list',)   # either None or a struct of TYPE=_get_lltype()
-
-    _LLTYPE = None
-    @classmethod
-    def _get_lltype(cls):
-        from rpython.rtyper.lltypesystem.rlist import make_LIST
-        if cls._LLTYPE is None:
-            cls._LLTYPE = make_LIST(lltype.Char)
-        return cls._LLTYPE
+    __slots__ = ('_ll_list',)   # either None or a struct of TYPE=LIST_OF(Char)
 
     def __init__(self, lst):
         self._ll_list = None
@@ -1227,10 +1219,11 @@
         self.__from_list(lst)
 
     def _get_ll_list(self):
+        from rpython.rtyper.lltypesystem import rffi
+        from rpython.rtyper.lltypesystem.rlist import LIST_OF
         if self._ll_list is None:
+            LIST = LIST_OF(lltype.Char)
             existing_items = list(self)
-            from rpython.rtyper.lltypesystem import lltype, rffi
-            LIST = self._get_lltype()
             n = len(self)
             self._ll_list = lltype.malloc(LIST, immortal=True)
             self._ll_list.length = n
@@ -1263,10 +1256,6 @@
     assert isinstance(lst, _ResizableListSupportingRawPtr)
     return lst._get_ll_list()
 
- at specialize.memo()
-def get_LIST_OF_CHAR():
-    return _ResizableListSupportingRawPtr._get_lltype()
-
 def _check_resizable_list_of_chars(s_list):
     from rpython.annotator import model as annmodel
     from rpython.rlib import debug
@@ -1287,7 +1276,8 @@
         return s_list
 
     def specialize_call(self, hop):
-        if hop.args_r[0].LIST != _ResizableListSupportingRawPtr._get_lltype():
+        from rpython.rtyper.lltypesystem.rlist import LIST_OF
+        if hop.args_r[0].LIST != LIST_OF(lltype.Char):
             raise ValueError('Resizable list of chars does not have the '
                              'expected low-level type')
         hop.exception_cannot_occur()
@@ -1312,9 +1302,10 @@
     _about_ = ll_for_resizable_list
 
     def compute_result_annotation(self, s_list):
+        from rpython.rtyper.lltypesystem.rlist import LIST_OF
         from rpython.rtyper.llannotation import lltype_to_annotation
         _check_resizable_list_of_chars(s_list)
-        LIST = _ResizableListSupportingRawPtr._get_lltype()
+        LIST = LIST_OF(lltype.Char)
         return lltype_to_annotation(lltype.Ptr(LIST))
 
     def specialize_call(self, hop):
diff --git a/rpython/rtyper/lltypesystem/rlist.py b/rpython/rtyper/lltypesystem/rlist.py
--- a/rpython/rtyper/lltypesystem/rlist.py
+++ b/rpython/rtyper/lltypesystem/rlist.py
@@ -30,16 +30,23 @@
 #    item_t list_items[]
 #
 
-def make_LIST(ITEMTYPE):
+def LIST_OF(ITEMTYPE, cache={}):
     """
     Return the low-level type for a resizable list of ITEMTYPE
     """
+    try:
+        return cache[ITEMTYPE]
+    except KeyError:
+        pass
+    
     from rpython.rtyper.lltypesystem.rstr import CharRepr
     assert ITEMTYPE is Char, 'only Char is supported for now'
     # XXX: maybe we should think of a better way to build the type?
     list_of_char_repr = ListRepr(None, CharRepr())
     list_of_char_repr._setup_repr()
-    return list_of_char_repr.LIST
+    LIST = list_of_char_repr.LIST
+    cache[ITEMTYPE] = LIST
+    return LIST
 
 
 class BaseListRepr(AbstractBaseListRepr):


More information about the pypy-commit mailing list