[pypy-commit] pypy json-decoder-maps: remove get_terminator

cfbolz pypy.commits at gmail.com
Wed Jun 5 09:49:55 EDT 2019


Author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
Branch: json-decoder-maps
Changeset: r96757:5b4720a4e5fc
Date: 2019-06-05 15:13 +0200
http://bitbucket.org/pypy/pypy/changeset/5b4720a4e5fc/

Log:	remove get_terminator

diff --git a/pypy/module/_pypyjson/interp_decoder.py b/pypy/module/_pypyjson/interp_decoder.py
--- a/pypy/module/_pypyjson/interp_decoder.py
+++ b/pypy/module/_pypyjson/interp_decoder.py
@@ -610,7 +610,7 @@
             self._raise("Key name must be string at char %d", i)
         i += 1
         w_key = self._decode_key_string(i)
-        return currmap.get_next(w_key, self.s, start, self.pos)
+        return currmap.get_next(w_key, self.s, start, self.pos, self.startmap)
 
     def _decode_key_string(self, i):
         ll_chars = self.ll_chars
@@ -732,12 +732,6 @@
         self.instantiation_count = 0
         self.number_of_leaves = 1
 
-    def get_terminator(self):
-        while isinstance(self, JSONMap):
-            self = self.prev
-        assert isinstance(self, Terminator)
-        return self
-
     def _check_invariants(self):
         if self.all_next:
             for next in self.all_next.itervalues():
@@ -745,7 +739,7 @@
         elif self.single_nextmap:
             self.single_nextmap._check_invariants()
 
-    def get_next(self, w_key, string, start, stop):
+    def get_next(self, w_key, string, start, stop, terminator):
         from pypy.objspace.std.dictmultiobject import unicode_hash, unicode_eq
         if isinstance(self, JSONMap):
             assert not self.state == MapBase.BLOCKED
@@ -776,7 +770,6 @@
             # fix number_of_leaves
             self.change_number_of_leaves(1)
 
-        terminator = self.get_terminator()
         terminator.register_potential_fringe(next)
         return next
 
@@ -804,7 +797,7 @@
         self.instantiation_count += 1
         if isinstance(self, JSONMap) and self.state == MapBase.FRINGE:
             if self.is_useful():
-                self.mark_useful()
+                self.mark_useful(self.startmap)
 
     def _make_next_map(self, w_key, key_repr):
         return JSONMap(self.space, self, w_key, key_repr)
@@ -842,23 +835,6 @@
         p.write("\n".join(r))
         graphclient.display_dot_file(str(p))
 
-    def _get_caching_stats(self):
-        caching = 0
-        num_maps = 1
-        if isinstance(self, JSONMap) and self.should_cache() and self.decoded_strings > 200:
-            caching += 1
-
-        if self.all_next:
-            children = self.all_next.values()
-        elif self.single_nextmap:
-            children = [self.single_nextmap]
-        else:
-            children = []
-        for child in children:
-            a, b = child._get_caching_stats()
-            caching += a
-            num_maps += b
-        return caching, num_maps
 
 class Terminator(MapBase):
     def __init__(self, space):
@@ -891,7 +867,7 @@
                         del self.current_fringe[f]
                 return
         assert min_fringe
-        min_fringe.mark_blocked()
+        min_fringe.mark_blocked(self)
         del self.current_fringe[min_fringe]
 
 
@@ -953,7 +929,7 @@
 
         MapBase._check_invariants(self)
 
-    def mark_useful(self):
+    def mark_useful(self, terminator):
         # mark self as useful, and also the most commonly instantiated
         # children, recursively
         assert self.state in (MapBase.FRINGE, MapBase.PRELIMINARY)
@@ -964,21 +940,20 @@
                 if child.instantiation_count > maxchild.instantiation_count:
                     maxchild = child
         if maxchild is not None:
-            maxchild.mark_useful()
+            maxchild.mark_useful(terminator)
             if self.all_next:
-                terminator = self.get_terminator()
                 for child in self.all_next.itervalues():
                     if child is not maxchild:
                         terminator.register_potential_fringe(child)
                 self.single_nextmap = maxchild
 
-    def mark_blocked(self):
+    def mark_blocked(self, terminator):
         self.state = MapBase.BLOCKED
         if self.all_next:
             for next in self.all_next.itervalues():
-                next.mark_blocked()
+                next.mark_blocked(terminator)
         elif self.single_nextmap:
-            self.single_nextmap.mark_blocked()
+            self.single_nextmap.mark_blocked(terminator)
         self.single_nextmap = None
         self.all_next = None
         self.change_number_of_leaves(-self.number_of_leaves + 1)
diff --git a/pypy/module/_pypyjson/test/test__pypyjson.py b/pypy/module/_pypyjson/test/test__pypyjson.py
--- a/pypy/module/_pypyjson/test/test__pypyjson.py
+++ b/pypy/module/_pypyjson/test/test__pypyjson.py
@@ -19,7 +19,7 @@
         w_a = self.space.newutf8("a", 1)
         w_b = self.space.newutf8("b", 1)
         w_c = self.space.newutf8("c", 1)
-        m1 = m.get_next(w_a, '"a"', 0, 3)
+        m1 = m.get_next(w_a, '"a"', 0, 3, m)
         assert m1.w_key == w_a
         assert m1.single_nextmap is None
         assert m1.key_repr == '"a"'
@@ -27,16 +27,16 @@
         assert not m1.key_repr_cmp('b": 123', 0)
         assert m.single_nextmap.w_key == w_a
 
-        m2 = m.get_next(w_a, '"a"', 0, 3)
+        m2 = m.get_next(w_a, '"a"', 0, 3, m)
         assert m2 is m1
 
-        m3 = m.get_next(w_b, '"b"', 0, 3)
+        m3 = m.get_next(w_b, '"b"', 0, 3, m)
         assert m3.w_key == w_b
         assert m3.single_nextmap is None
         assert m3.key_repr == '"b"'
         assert m.single_nextmap is m1
 
-        m4 = m3.get_next(w_c, '"c"', 0, 3)
+        m4 = m3.get_next(w_c, '"c"', 0, 3, m)
         assert m4.w_key == w_c
         assert m4.single_nextmap is None
         assert m4.key_repr == '"c"'
@@ -47,15 +47,15 @@
         w_a = self.space.newutf8("a", 1)
         w_b = self.space.newutf8("b", 1)
         w_c = self.space.newutf8("c", 1)
-        m1 = m.get_next(w_a, 'a"', 0, 2)
+        m1 = m.get_next(w_a, 'a"', 0, 2, m)
         assert m1.get_index(w_a) == 0
         assert m1.get_index(w_b) == -1
 
-        m2 = m.get_next(w_b, 'b"', 0, 2)
+        m2 = m.get_next(w_b, 'b"', 0, 2, m)
         assert m2.get_index(w_b) == 0
         assert m2.get_index(w_a) == -1
 
-        m3 = m2.get_next(w_c, 'c"', 0, 2)
+        m3 = m2.get_next(w_c, 'c"', 0, 2, m)
         assert m3.get_index(w_b) == 0
         assert m3.get_index(w_c) == 1
         assert m3.get_index(w_a) == -1
@@ -103,10 +103,10 @@
         w_d = self.space.newutf8("d", 1)
         base = Terminator(self.space)
         base.instantiation_count = 6
-        m1 = base.get_next(w_a, 'a"', 0, 2)
-        m2 = m1.get_next(w_b, 'b"', 0, 2)
-        m3 = m2.get_next(w_c, 'c"', 0, 2)
-        m4 = m2.get_next(w_d, 'd"', 0, 2)
+        m1 = base.get_next(w_a, 'a"', 0, 2, base)
+        m2 = m1.get_next(w_b, 'b"', 0, 2, base)
+        m3 = m2.get_next(w_c, 'c"', 0, 2, base)
+        m4 = m2.get_next(w_d, 'd"', 0, 2, base)
         return base, m1, m2, m3, m4
 
     # unit tests for map state transistions
@@ -126,7 +126,7 @@
         assert m4.state == MapBase.PRELIMINARY
         m4.instantiation_count = 4
 
-        m1.mark_useful()
+        m1.mark_useful(base)
         assert m1.state == MapBase.USEFUL
         assert m2.state == MapBase.USEFUL
         assert m3.state == MapBase.FRINGE
@@ -144,7 +144,7 @@
         assert m2.number_of_leaves == 2
         assert m3.number_of_leaves == 1
         assert m4.number_of_leaves == 1
-        m5 = m2.get_next(w_x, 'x"', 0, 2)
+        m5 = m2.get_next(w_x, 'x"', 0, 2, base)
         assert base.number_of_leaves == 3
         assert m1.number_of_leaves == 3
         assert m2.number_of_leaves == 3
@@ -160,7 +160,7 @@
         m4.instantiation_count = 4
         assert base.current_fringe == {m1: None}
 
-        m1.mark_useful()
+        m1.mark_useful(base)
         assert base.current_fringe == {m1: None, m3: None} # not cleaned up
         base.cleanup_fringe()
         assert base.current_fringe == {m3: None}
@@ -172,11 +172,11 @@
         w_d = self.space.newutf8("d", 1)
         base = Terminator(self.space)
         base.instantiation_count = 6
-        m1 = base.get_next(w_a, 'a"', 0, 2)
-        m2 = base.get_next(w_b, 'b"', 0, 2)
-        m3 = base.get_next(w_c, 'c"', 0, 2)
-        m4 = base.get_next(w_d, 'd"', 0, 2)
-        m5 = m4.get_next(w_a, 'a"', 0, 2)
+        m1 = base.get_next(w_a, 'a"', 0, 2, base)
+        m2 = base.get_next(w_b, 'b"', 0, 2, base)
+        m3 = base.get_next(w_c, 'c"', 0, 2, base)
+        m4 = base.get_next(w_d, 'd"', 0, 2, base)
+        m5 = m4.get_next(w_a, 'a"', 0, 2, base)
         base.instantiation_count = 7
         m1.instantiation_count = 2
         m2.instantiation_count = 2
@@ -202,9 +202,9 @@
         s = '{"a": 1, "b": 2, "c": 3}'
         dec = JSONDecoder(space, s)
         dec.startmap = base = Terminator(space)
-        m1 = base.get_next(w_a, 'a"', 0, 2)
-        m2 = m1.get_next(w_b, 'b"', 0, 2)
-        m2.mark_blocked()
+        m1 = base.get_next(w_a, 'a"', 0, 2, base)
+        m2 = m1.get_next(w_b, 'b"', 0, 2, base)
+        m2.mark_blocked(base)
         w_res = dec.decode_object(1)
         assert space.int_w(space.len(w_res)) == 3
         assert space.int_w(space.getitem(w_res, w_a)) == 1
@@ -219,35 +219,14 @@
         w_u = self.space.newutf8("u", 1)
         space = self.space
         base = Terminator(space)
-        m1 = base.get_next(w_a, 'a"', 0, 2)
-        m2 = m1.get_next(w_b, 'b"', 0, 2)
-        m2.get_next(w_x, 'x"', 0, 2)
-        m2.get_next(w_u, 'u"', 0, 2)
+        m1 = base.get_next(w_a, 'a"', 0, 2, base)
+        m2 = m1.get_next(w_b, 'b"', 0, 2, base)
+        m2.get_next(w_x, 'x"', 0, 2, base)
+        m2.get_next(w_u, 'u"', 0, 2, base)
         assert base.number_of_leaves == 2
-        m2.mark_blocked()
+        m2.mark_blocked(base)
         assert base.number_of_leaves == 1
 
-    @pytest.mark.skip()
-    def test_caching_stats(self):
-        w_a = self.space.newutf8("a", 1)
-        w_b = self.space.newutf8("b", 1)
-        w_x = self.space.newutf8("x", 1)
-        w_u = self.space.newutf8("u", 1)
-        space = self.space
-        base = Terminator(space)
-        m1 = base.get_next(w_a, 'a"', 0, 2)
-        m2 = m1.get_next(w_b, 'b"', 0, 2)
-        m2.get_next(w_x, 'x"', 0, 2)
-        m2.get_next(w_u, 'u"', 0, 2)
-        m1.decode_string = 300
-        m1.cache_hits = 0
-        m3 = base.get_next(w_b, '"b"', 0, 3)
-        m3.decode_string = 300
-        m3.cache_hits = 300
-        caching_maps, total_maps = base._get_caching_stats()
-        assert caching_maps == 5
-        assert total_maps == 6
-
 
 class AppTest(object):
     spaceconfig = {"objspace.usemodules._pypyjson": True}


More information about the pypy-commit mailing list