[pypy-commit] pypy optinfo-into-bridges: add a Writer class too

cfbolz pypy.commits at gmail.com
Sat Oct 15 07:02:27 EDT 2016


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: optinfo-into-bridges
Changeset: r87807:839e6546a571
Date: 2016-10-14 16:54 +0200
http://bitbucket.org/pypy/pypy/changeset/839e6546a571/

Log:	add a Writer class too

diff --git a/rpython/jit/metainterp/resume.py b/rpython/jit/metainterp/resume.py
--- a/rpython/jit/metainterp/resume.py
+++ b/rpython/jit/metainterp/resume.py
@@ -162,35 +162,13 @@
 UNINITIALIZED = tag(-2, TAGCONST)   # used for uninitialized string characters
 TAG_CONST_OFFSET = 0
 
-class NumberingState(object):
+class NumberingState(resumecode.Writer):
     def __init__(self, size):
+        resumecode.Writer.__init__(self, size)
         self.liveboxes = {}
-        self.current = []
-        self.grow(size)
-        self._pos = 0
         self.num_boxes = 0
         self.num_virtuals = 0
 
-    def append_short(self, item):
-        self.current[self._pos] = item
-        self._pos += 1
-
-    def append_int(self, item):
-        short = rffi.cast(rffi.SHORT, item)
-        assert rffi.cast(lltype.Signed, short) == item
-        return self.append_short(short)
-
-    def create_numbering(self):
-        return resumecode.create_numbering(self.current[:self._pos])
-
-    def grow(self, size):
-        self.current.extend([rffi.cast(rffi.SHORT, 0)] * size)
-
-    def patch_current_size(self, index):
-        item = self._pos
-        short = rffi.cast(rffi.SHORT, item)
-        assert rffi.cast(lltype.Signed, short) == item
-        self.current[index] = short
 
 class ResumeDataLoopMemo(object):
 
diff --git a/rpython/jit/metainterp/resumecode.py b/rpython/jit/metainterp/resumecode.py
--- a/rpython/jit/metainterp/resumecode.py
+++ b/rpython/jit/metainterp/resumecode.py
@@ -87,6 +87,33 @@
         l.append(next)
     return l
 
+class Writer(object):
+    def __init__(self, size):
+        self.current = []
+        self.grow(size)
+        self._pos = 0
+
+    def append_short(self, item):
+        self.current[self._pos] = item
+        self._pos += 1
+
+    def append_int(self, item):
+        short = rffi.cast(rffi.SHORT, item)
+        assert rffi.cast(lltype.Signed, short) == item
+        return self.append_short(short)
+
+    def create_numbering(self):
+        return create_numbering(self.current[:self._pos])
+
+    def grow(self, size):
+        self.current.extend([rffi.cast(rffi.SHORT, 0)] * size)
+
+    def patch_current_size(self, index):
+        item = self._pos
+        short = rffi.cast(rffi.SHORT, item)
+        assert rffi.cast(lltype.Signed, short) == item
+        self.current[index] = short
+
 class Reader(object):
     def __init__(self, code):
         self.code = code
diff --git a/rpython/jit/metainterp/test/test_resumecode.py b/rpython/jit/metainterp/test/test_resumecode.py
--- a/rpython/jit/metainterp/test/test_resumecode.py
+++ b/rpython/jit/metainterp/test/test_resumecode.py
@@ -1,29 +1,45 @@
-
-from rpython.jit.metainterp.resumecode import NUMBERING, NULL_NUMBER
 from rpython.jit.metainterp.resumecode import create_numbering,\
-    unpack_numbering
+    unpack_numbering, Reader, Writer
 from rpython.rtyper.lltypesystem import lltype
 
-from hypothesis import strategies, given
+from hypothesis import strategies, given, example
 
+examples = [
+    [1, 2, 3, 4, 257, 10000, 13, 15],
+    [1, 2, 3, 4],
+    range(1, 10, 2),
+    [13000, 12000, 10000, 256, 255, 254, 257, -3, -1000]
+]
 
-def test_pack_unpack():
-    examples = [
-        [1, 2, 3, 4, 257, 10000, 13, 15],
-        [1, 2, 3, 4],
-        range(1, 10, 2),
-        [13000, 12000, 10000, 256, 255, 254, 257, -3, -1000]
-    ]
-    for l in examples:
-        n = create_numbering(l)
-        assert unpack_numbering(n) == l
+def hypothesis_and_examples(func):
+    func = given(strategies.lists(strategies.integers(-2**15, 2**15-1)))(func)
+    for ex in examples:
+        func = example(ex)(func)
+    return func
 
- at given(strategies.lists(strategies.integers(-2**15, 2**15-1)))
+ at hypothesis_and_examples
 def test_roundtrip(l):
     n = create_numbering(l)
     assert unpack_numbering(n) == l
 
- at given(strategies.lists(strategies.integers(-2**15, 2**15-1)))
+ at hypothesis_and_examples
 def test_compressing(l):
     n = create_numbering(l)
     assert len(n.code) <= len(l) * 3
+
+ at hypothesis_and_examples
+def test_reader(l):
+    n = create_numbering(l)
+    r = Reader(n)
+    for i, elt in enumerate(l):
+        assert r.items_read == i
+        item = r.next_item()
+        assert elt == item
+
+ at hypothesis_and_examples
+def test_reader(l):
+    w = Writer(len(l))
+    for num in l:
+        w.append_int(num)
+    n = w.create_numbering()
+    assert unpack_numbering(n) == l


More information about the pypy-commit mailing list