Author: Richard Plangger <planri...@gmail.com>
Branch: new-jit-log
Changeset: r83848:02a45c61ac94
Date: 2016-04-25 09:13 +0200
http://bitbucket.org/pypy/pypy/changeset/02a45c61ac94/

Log:    reenabled the prefix compression and added a new test to check
        commonprefix

diff --git a/rpython/rlib/jitlog.py b/rpython/rlib/jitlog.py
--- a/rpython/rlib/jitlog.py
+++ b/rpython/rlib/jitlog.py
@@ -13,15 +13,19 @@
 from rpython.rlib.objectmodel import we_are_translated, specialize
 from rpython.rlib.unroll import unrolling_iterable
 
-def commonprefix(m):
+def commonprefix(a,b):
     "Given a list of pathnames, returns the longest common leading component"
-    if not m: return ''
-    s1 = min(m)
-    s2 = max(m)
-    for i, c in enumerate(s1):
-        if c != s2[i]:
-            return s1[:i]
-    return s1
+    assert a is not None
+    assert b is not None
+    la = len(a)
+    lb = len(b)
+    c = min(la,lb)
+    if c == 0:
+        return ""
+    for i in range(c):
+        if a[i] != b[i]:
+            return a[:i] # partly matching
+    return a # full match
 
 @always_inline
 def encode_str(string):
@@ -89,31 +93,30 @@
         self.value = value
 
     def encode(self, log, i, prefixes):
-        return encode_str(self.value)
-        #str_value = self.value
-        #if len(str_value) < 5:
-        #    enc_value = encode_str(chr(0xff) + str_value)
-        #else:
-        #    cp = commonprefix([prefixes[i], str_value])
-        #    if cp != prefixes[i]:
-        #        if len(cp) == 0:
-        #            # they are fully different!
-        #            prefixes[i] = str_value
-        #            enc_value = encode_str(chr(0xff) + str_value)
-        #        else:
-        #            # the prefix changed
-        #            prefixes[i] = cp
-        #            # common prefix of field i
-        #            assert i != 0xff
-        #            log._write_marked(MARK_COMMON_PREFIX, chr(i) \
-        #                                              + encode_str(cp))
-        #            enc_value = encode_str(chr(i) + str_value)
-        #    else:
-        #        enc_value = encode_str(chr(i) + str_value)
-        ##
-        #if prefixes[i] is None:
-        #    prefixes[i] = str_value
-        #return enc_value
+        str_value = self.value
+        if len(str_value) < 5:
+            enc_value = encode_str(chr(0xff) + str_value)
+        else:
+            cp = commonprefix([prefixes[i], str_value])
+            if cp != prefixes[i]:
+                if len(cp) == 0:
+                    # they are fully different!
+                    prefixes[i] = str_value
+                    enc_value = encode_str(chr(0xff) + str_value)
+                else:
+                    # the prefix changed
+                    prefixes[i] = cp
+                    # common prefix of field i
+                    assert i != 0xff
+                    log._write_marked(MARK_COMMON_PREFIX, chr(i) \
+                                                      + encode_str(cp))
+                    enc_value = encode_str(chr(i) + str_value)
+            else:
+                enc_value = encode_str(chr(i) + str_value)
+        #
+        if prefixes[i] is None:
+            prefixes[i] = str_value
+        return enc_value
 
 class IntValue(WrappedValue):
     def __init__(self, sem_type, gen_type, value):
diff --git a/rpython/rlib/test/test_jitlog.py b/rpython/rlib/test/test_jitlog.py
--- a/rpython/rlib/test/test_jitlog.py
+++ b/rpython/rlib/test/test_jitlog.py
@@ -1,3 +1,4 @@
+import py
 from rpython.jit.tool.oparser import pure_parse
 from rpython.jit.metainterp.optimizeopt.util import equaloplists
 from rpython.jit.metainterp.resoperation import ResOperation, rop
@@ -5,7 +6,6 @@
 from rpython.jit.metainterp.history import ConstInt, ConstPtr
 from rpython.rlib.jitlog import (encode_str, encode_le_16bit, encode_le_64bit)
 from rpython.rlib import jitlog as jl
-import tempfile
 
 class TestLogger(object):
 
@@ -54,3 +54,12 @@
     def test_common_prefix(self):
         fakelog = FakeLog()
         logger = jitlog.LogTrace(0x0, {}, None, None, fakelog)
+
+    def test_common_prefix_func(self):
+        assert jl.commonprefix("","") == ""
+        assert jl.commonprefix("/hello/world","/path/to") == "/"
+        assert jl.commonprefix("pyramid","python") == "py"
+        assert jl.commonprefix("0"*100,"0"*100) == "0"*100
+        with py.test.raises(AssertionError):
+            jl.commonprefix(None,None)
+
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to