[pypy-commit] pypy default: create rsre_core.BufMatchContext, use to avoid copying

bdkearns noreply at buildbot.pypy.org
Wed Apr 23 23:42:28 CEST 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r70895:42dcc26ee1d1
Date: 2014-04-23 16:11 -0400
http://bitbucket.org/pypy/pypy/changeset/42dcc26ee1d1/

Log:	create rsre_core.BufMatchContext, use to avoid copying

diff --git a/pypy/module/_sre/interp_sre.py b/pypy/module/_sre/interp_sre.py
--- a/pypy/module/_sre/interp_sre.py
+++ b/pypy/module/_sre/interp_sre.py
@@ -34,8 +34,8 @@
 
 def slice_w(space, ctx, start, end, w_default):
     if 0 <= start <= end:
-        if isinstance(ctx, rsre_core.StrMatchContext):
-            return space.wrap(ctx._string[start:end])
+        if isinstance(ctx, rsre_core.BufMatchContext):
+            return space.wrap(ctx._buffer.getslice(start, end, 1, end-start))
         elif isinstance(ctx, rsre_core.UnicodeMatchContext):
             return space.wrap(ctx._unicodestr[start:end])
         else:
@@ -98,7 +98,7 @@
                              space.wrap("cannot copy this pattern object"))
 
     def make_ctx(self, w_string, pos=0, endpos=sys.maxint):
-        """Make a StrMatchContext or a UnicodeMatchContext for searching
+        """Make a BufMatchContext or a UnicodeMatchContext for searching
         in the given w_string object."""
         space = self.space
         if pos < 0:
@@ -114,12 +114,14 @@
             return rsre_core.UnicodeMatchContext(self.code, unicodestr,
                                                  pos, endpos, self.flags)
         else:
-            str = space.bufferstr_w(w_string)
-            if pos > len(str):
-                pos = len(str)
-            if endpos > len(str):
-                endpos = len(str)
-            return rsre_core.StrMatchContext(self.code, str,
+            buf = space.buffer_w(w_string)
+            size = buf.getlength()
+            assert size >= 0
+            if pos > size:
+                pos = size
+            if endpos > size:
+                endpos = size
+            return rsre_core.BufMatchContext(self.code, buf,
                                              pos, endpos, self.flags)
 
     def getmatch(self, ctx, found):
@@ -477,8 +479,8 @@
 
     def fget_string(self, space):
         ctx = self.ctx
-        if isinstance(ctx, rsre_core.StrMatchContext):
-            return space.wrap(ctx._string)
+        if isinstance(ctx, rsre_core.BufMatchContext):
+            return space.wrap(ctx._buffer.as_str())
         elif isinstance(ctx, rsre_core.UnicodeMatchContext):
             return space.wrap(ctx._unicodestr)
         else:
diff --git a/rpython/rlib/rsre/rsre_core.py b/rpython/rlib/rsre/rsre_core.py
--- a/rpython/rlib/rsre/rsre_core.py
+++ b/rpython/rlib/rsre/rsre_core.py
@@ -62,7 +62,8 @@
     # Install a copy of the function under the name '_spec_funcname' in each
     # concrete subclass
     specialized_methods = []
-    for prefix, concreteclass in [('str', StrMatchContext),
+    for prefix, concreteclass in [('buf', BufMatchContext),
+                                  ('str', StrMatchContext),
                                   ('uni', UnicodeMatchContext)]:
         newfunc = func_with_new_name(func, prefix + specname)
         assert not hasattr(concreteclass, specname)
@@ -170,6 +171,27 @@
     def fresh_copy(self, start):
         raise NotImplementedError
 
+class BufMatchContext(AbstractMatchContext):
+    """Concrete subclass for matching in a buffer."""
+
+    _immutable_fields_ = ["_buffer"]
+
+    def __init__(self, pattern, buf, match_start, end, flags):
+        AbstractMatchContext.__init__(self, pattern, match_start, end, flags)
+        self._buffer = buf
+
+    def str(self, index):
+        check_nonneg(index)
+        return ord(self._buffer.getitem(index))
+
+    def lowstr(self, index):
+        c = self.str(index)
+        return rsre_char.getlower(c, self.flags)
+
+    def fresh_copy(self, start):
+        return BufMatchContext(self.pattern, self._buffer, start,
+                               self.end, self.flags)
+
 class StrMatchContext(AbstractMatchContext):
     """Concrete subclass for matching in a plain string."""
 
diff --git a/rpython/rlib/rsre/rsre_jit.py b/rpython/rlib/rsre/rsre_jit.py
--- a/rpython/rlib/rsre/rsre_jit.py
+++ b/rpython/rlib/rsre/rsre_jit.py
@@ -33,9 +33,11 @@
     setattr(AbstractMatchContext, 'jitdriver_' + name, jitdriver)
 
 def install_jitdriver_spec(name, **kwds):
+    from rpython.rlib.rsre.rsre_core import BufMatchContext
     from rpython.rlib.rsre.rsre_core import StrMatchContext
     from rpython.rlib.rsre.rsre_core import UnicodeMatchContext
-    for prefix, concreteclass in [('Str', StrMatchContext),
+    for prefix, concreteclass in [('Buf', BufMatchContext),
+                                  ('Str', StrMatchContext),
                                   ('Uni', UnicodeMatchContext)]:
         jitdriver = RSreJitDriver(prefix + name, **kwds)
         setattr(concreteclass, 'jitdriver_' + name, jitdriver)


More information about the pypy-commit mailing list