Author: Armin Rigo <ar...@tunes.org>
Branch: 
Changeset: r73208:30c2d0b47ea9
Date: 2014-08-30 16:13 +0200
http://bitbucket.org/pypy/pypy/changeset/30c2d0b47ea9/

Log:    Add change_current_fraction()

diff --git a/rpython/jit/metainterp/counter.py 
b/rpython/jit/metainterp/counter.py
--- a/rpython/jit/metainterp/counter.py
+++ b/rpython/jit/metainterp/counter.py
@@ -136,6 +136,34 @@
             return True
     tick._always_inline_ = True
 
+    def change_current_fraction(self, hash, new_fraction):
+        """Change the value stored for 'hash' to be the given 'new_fraction',
+        which should be a float equal to or slightly lower than 1.0.
+        """
+        p_entry = self.timetable[self._get_index(hash)]
+        subhash = self._get_subhash(hash)
+
+        # find in 'n' the index that will be overwritten: the first within
+        # range(5) that contains either the right subhash, or a null time
+        # (or, if there isn't any, then just n == 4 will do).
+        n = 0
+        while n < 4 and (p_entry.subhashes[n] != subhash and
+                         float(p_entry.times[n]) != 0.0):
+            n += 1
+
+        # move one step to the right all elements [n - 1, n - 2, ..., 0],
+        # (this overwrites the old item at index 'n')
+        while n > 0:
+            n -= 1
+            p_entry.subhashes[n + 1] = p_entry.subhashes[n]
+            p_entry.times[n + 1]     = p_entry.times[n]
+
+        # insert the new hash at index 0.  This is a good approximation,
+        # because change_current_fraction() should be used for
+        # new_fraction == value close to 1.0.
+        p_entry.subhashes[0] = rffi.cast(rffi.USHORT, subhash)
+        p_entry.times[0]     = r_singlefloat(new_fraction)
+
     def reset(self, hash):
         p_entry = self.timetable[self._get_index(hash)]
         subhash = self._get_subhash(hash)
diff --git a/rpython/jit/metainterp/test/test_counter.py 
b/rpython/jit/metainterp/test/test_counter.py
--- a/rpython/jit/metainterp/test/test_counter.py
+++ b/rpython/jit/metainterp/test/test_counter.py
@@ -106,3 +106,25 @@
     assert jc.lookup_chain(104) is d3
     assert d3.next is d4
     assert d4.next is None
+
+
+def test_change_current_fraction():
+    jc = JitCounter()
+    incr = jc.compute_threshold(8)
+    # change_current_fraction() with a fresh new hash
+    jc.change_current_fraction(index2hash(jc, 104), 0.95)
+    r = jc.tick(index2hash(jc, 104), incr)
+    assert r is True
+    # change_current_fraction() with an already-existing hash
+    r = jc.tick(index2hash(jc, 104), incr)
+    assert r is False
+    jc.change_current_fraction(index2hash(jc, 104), 0.95)
+    r = jc.tick(index2hash(jc, 104), incr)
+    assert r is True
+    # change_current_fraction() with a smaller incr
+    incr = jc.compute_threshold(32)
+    jc.change_current_fraction(index2hash(jc, 104), 0.95)
+    r = jc.tick(index2hash(jc, 104), incr)
+    assert r is False
+    r = jc.tick(index2hash(jc, 104), incr)
+    assert r is True
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to