[pypy-commit] pypy vmprof: Tweaks, including one which is a fix for find_codemap_at_addr() I think

arigo noreply at buildbot.pypy.org
Tue Feb 10 14:38:49 CET 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: vmprof
Changeset: r75799:9d2347008c52
Date: 2015-02-10 14:38 +0100
http://bitbucket.org/pypy/pypy/changeset/9d2347008c52/

Log:	Tweaks, including one which is a fix for find_codemap_at_addr() I
	think

diff --git a/rpython/jit/backend/llsupport/asmmemmgr.py b/rpython/jit/backend/llsupport/asmmemmgr.py
--- a/rpython/jit/backend/llsupport/asmmemmgr.py
+++ b/rpython/jit/backend/llsupport/asmmemmgr.py
@@ -5,7 +5,7 @@
 from rpython.rlib.debug import debug_start, debug_print, debug_stop
 from rpython.rlib.debug import have_debug_prints
 from rpython.rtyper.lltypesystem import lltype, rffi
-from rpython.rlib.rbisect import bisect, bisect_tuple
+from rpython.rlib.rbisect import bisect_left, bisect_left_tuple
 
 _memmngr = None # global reference so we can use @entrypoint :/
 
@@ -55,19 +55,16 @@
             self.total_mallocs -= r_uint(stop - start)
         self._add_free_block(start, stop)
         # fix up jit_addr_map
-        jit_adr_start = bisect(self.jit_addr_map, start)
-        jit_adr_stop = bisect(self.jit_addr_map, stop)
-        self.jit_addr_map = (self.jit_addr_map[:jit_adr_start] +
-                             self.jit_addr_map[jit_adr_stop:])
-        self.jit_frame_depth_map = (self.jit_frame_depth_map[:jit_adr_start] +
-                                    self.jit_frame_depth_map[jit_adr_stop:])
+        jit_adr_start = bisect_left(self.jit_addr_map, start)
+        jit_adr_stop = bisect_left(self.jit_addr_map, stop)
+        del self.jit_addr_map[jit_adr_start:jit_adr_stop]
+        del self.jit_frame_depth_map[jit_adr_start:jit_adr_stop]
         # fix up codemap
         # (there should only be zero or one codemap entry in that range,
         # but still we use a range to distinguish between zero and one)
-        codemap_adr_start = bisect_tuple(self.jit_codemap, start)
-        codemap_adr_stop = bisect_tuple(self.jit_codemap, stop)
-        self.jit_codemap = (self.jit_codemap[:codemap_adr_start] +
-                            self.jit_codemap[codemap_adr_stop:])
+        codemap_adr_start = bisect_left_tuple(self.jit_codemap, start)
+        codemap_adr_stop = bisect_left_tuple(self.jit_codemap, stop)
+        del self.jit_codemap[codemap_adr_start:codemap_adr_stop]
 
     def open_malloc(self, minsize):
         """Allocate at least minsize bytes.  Returns (start, stop)."""
@@ -183,7 +180,7 @@
             self.jit_addr_map += [0] * len(frame_positions)
             self.jit_frame_depth_map += [0] * len(frame_positions)
         else:
-            start = bisect(self.jit_addr_map, rawstart)
+            start = bisect_left(self.jit_addr_map, rawstart)
             self.jit_addr_map = (self.jit_addr_map[:start] +
                                  [0] * len(frame_positions) +
                                  self.jit_addr_map[start:])
@@ -196,12 +193,8 @@
 
     def register_codemap(self, codemap):
         start = codemap[0]
-        pos = bisect_tuple(self.jit_codemap, start)
-        if pos == len(self.jit_codemap): # common case
-            self.jit_codemap.append(codemap)
-        else:
-            self.jit_codemap = (self.jit_codemap[:pos] + [codemap] +
-                                self.jit_codemap[pos:])
+        pos = bisect_left_tuple(self.jit_codemap, start)
+        self.jit_codemap.insert(pos, codemap)
 
     def _delete(self):
         "NOT_RPYTHON"
diff --git a/rpython/jit/backend/llsupport/codemap.py b/rpython/jit/backend/llsupport/codemap.py
--- a/rpython/jit/backend/llsupport/codemap.py
+++ b/rpython/jit/backend/llsupport/codemap.py
@@ -12,7 +12,7 @@
 from rpython.rlib import rgc
 from rpython.rlib.entrypoint import jit_entrypoint
 from rpython.jit.backend.llsupport import asmmemmgr
-from rpython.rlib.rbisect import bisect, bisect_tuple
+from rpython.rlib.rbisect import bisect_right, bisect_right_tuple
 from rpython.rtyper.lltypesystem import lltype, rffi
 
 @jit_entrypoint([lltype.Signed], lltype.Signed,
@@ -21,7 +21,7 @@
 def stack_depth_at_loc(loc):
     _memmngr = asmmemmgr._memmngr
 
-    pos = bisect(_memmngr.jit_addr_map, loc + 1)
+    pos = bisect_right(_memmngr.jit_addr_map, loc)
     if pos == 0 or pos == len(_memmngr.jit_addr_map):
         return -1
     return _memmngr.jit_frame_depth_map[pos - 1]
@@ -43,9 +43,7 @@
 def find_codemap_at_addr(addr):
     _memmngr = asmmemmgr._memmngr
 
-    res = bisect_tuple(_memmngr.jit_codemap, addr) - 1
-    if res == len(_memmngr.jit_codemap):
-        return -1
+    res = bisect_right_tuple(_memmngr.jit_codemap, addr) - 1
     return res
 
 @jit_entrypoint([lltype.Signed, lltype.Signed,
diff --git a/rpython/rlib/rbisect.py b/rpython/rlib/rbisect.py
--- a/rpython/rlib/rbisect.py
+++ b/rpython/rlib/rbisect.py
@@ -1,21 +1,39 @@
 
-def bisect(a, x):
+def bisect_left(a, x):
     """Return the index in the sorted list 'a' of 'x'.  If 'x' is not in 'a',
     return the index where it can be inserted."""
     lo = 0
     hi = len(a)
     while lo < hi:
         mid = (lo+hi)//2
-        if x <= a[mid]: hi = mid
+        if a[mid] < x: lo = mid+1
+        else: hi = mid
+    return lo
+
+def bisect_right(a, x):
+    lo = 0
+    hi = len(a)
+    while lo < hi:
+        mid = (lo+hi)//2
+        if x < a[mid]: hi = mid
         else: lo = mid+1
     return lo
 
 # a copy of the above, but compares the first item of a tuple only
-def bisect_tuple(a, x):
+def bisect_left_tuple(a, x):
     lo = 0
     hi = len(a)
     while lo < hi:
         mid = (lo+hi)//2
-        if x <= a[mid][0]: hi = mid
+        if a[mid][0] < x: lo = mid+1
+        else: hi = mid
+    return lo
+
+def bisect_right_tuple(a, x):
+    lo = 0
+    hi = len(a)
+    while lo < hi:
+        mid = (lo+hi)//2
+        if x < a[mid][0]: hi = mid
         else: lo = mid+1
     return lo
diff --git a/rpython/rlib/test/test_rbisect.py b/rpython/rlib/test/test_rbisect.py
--- a/rpython/rlib/test/test_rbisect.py
+++ b/rpython/rlib/test/test_rbisect.py
@@ -1,7 +1,7 @@
 
-from rpython.rlib.rbisect import bisect
+from rpython.rlib.rbisect import bisect_left, bisect_right
 
-def test_bisect():
+def test_bisect_left():
     cases = [
             ([], 1, 0),
             ([1], 0, 0),
@@ -44,4 +44,50 @@
             ([1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 5, 10),
         ]
     for lst, elem, exp in cases:
-        assert bisect(lst, elem) == exp
+        assert bisect_left(lst, elem) == exp
+
+def test_bisect_right():
+    cases = [
+
+            ([], 1, 0),
+            ([1], 0, 0),
+            ([1], 1, 1),
+            ([1], 2, 1),
+            ([1, 1], 0, 0),
+            ([1, 1], 1, 2),
+            ([1, 1], 2, 2),
+            ([1, 1, 1], 0, 0),
+            ([1, 1, 1], 1, 3),
+            ([1, 1, 1], 2, 3),
+            ([1, 1, 1, 1], 0, 0),
+            ([1, 1, 1, 1], 1, 4),
+            ([1, 1, 1, 1], 2, 4),
+            ([1, 2], 0, 0),
+            ([1, 2], 1, 1),
+            ([1, 2], 1.5, 1),
+            ([1, 2], 2, 2),
+            ([1, 2], 3, 2),
+            ([1, 1, 2, 2], 0, 0),
+            ([1, 1, 2, 2], 1, 2),
+            ([1, 1, 2, 2], 1.5, 2),
+            ([1, 1, 2, 2], 2, 4),
+            ([1, 1, 2, 2], 3, 4),
+            ([1, 2, 3], 0, 0),
+            ([1, 2, 3], 1, 1),
+            ([1, 2, 3], 1.5, 1),
+            ([1, 2, 3], 2, 2),
+            ([1, 2, 3], 2.5, 2),
+            ([1, 2, 3], 3, 3),
+            ([1, 2, 3], 4, 3),
+            ([1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 0, 0),
+            ([1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1, 1),
+            ([1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1.5, 1),
+            ([1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2, 3),
+            ([1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2.5, 3),
+            ([1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3, 6),
+            ([1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3.5, 6),
+            ([1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 4, 10),
+            ([1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 5, 10),
+        ]
+    for lst, elem, exp in cases:
+        assert bisect_right(lst, elem) == exp


More information about the pypy-commit mailing list