[Python-checkins] r46455 - sandbox/trunk/hotbuffer/test_hotbuf.py

martin.blais python-checkins at python.org
Sat May 27 14:23:57 CEST 2006


Author: martin.blais
Date: Sat May 27 14:23:56 2006
New Revision: 46455

Modified:
   sandbox/trunk/hotbuffer/test_hotbuf.py
Log:
Optimized the newline delimited use case.

Modified: sandbox/trunk/hotbuffer/test_hotbuf.py
==============================================================================
--- sandbox/trunk/hotbuffer/test_hotbuf.py	(original)
+++ sandbox/trunk/hotbuffer/test_hotbuf.py	Sat May 27 14:23:56 2006
@@ -389,9 +389,7 @@
 knowledge. There are many constraint networks that
 implement the same set of constraints, and the user must
 choose from the set of mathematically equivalent networks a
-suitable network to specify a particular computation.
-
-"""
+suitable network to specify a particular computation."""
     lines1 = map(str.strip, data1.splitlines())
 
     def parse_newline_delim( self, hot, read, process_line ):
@@ -399,7 +397,7 @@
         Use case for newline-delimited data.
         """
         newline, cr = ord('\n'), ord('\r')
-        
+
         # Initiallly put some data into the buffer.
         hot.putstr(read(len(hot)))
         hot.flip()
@@ -412,48 +410,33 @@
 
             # Loop over the current buffer contents
             while hot:
-                # Save the starting position
-                start = hot.position
+                # Loop over all characters
+                # If we got to the end of the line
+                nidx = hot.find('\n')
+                if nidx != -1:
+                    # Calculate how much characters are needed to
+                    # backup to remove the EOL marker
+                    backup = 0
+
+                    # Make sure we don't look before the first char
+                    if nidx > 0:
+                        if hot.getbyterel(nidx - 1) == cr:
+                            backup = 1
 
-## FIXME we need to replace what follows by the hot.find method, the
-## overhead of funcall for every char is too high
+                    # Restrict the window to the current line
+                    hot.position = mark_position
+                    hot.limit = mark_position + nidx - backup
 
-                # Loop over all characters
-                while 1:
-                    # If we got to the end of the line
-                    if hot.getbyte() == newline:
-                        # Process the line we found
-
-                        # Calculate how much characters are needed to
-                        # backup to remove the EOL marker
-                        end = hot.position
-                        backup = 1
-
-                        # Make sure we don't look before the first char
-                        if end - start > 1:
-                            if hot.getbyterel(-2) == cr:
-                                backup = 2
-
-                        # Restrict the window to the current line
-                        hot.position = mark_position # reset
-                        hot.limit = end - backup
-
-                        # Process the line.
-                        process_line(hot)
-
-                        # Advance the buffer window to the rest of the
-                        # buffer after the line
-                        hot.limit = abslimit
-                        mark_position = hot.position = end
-
-                        break
-
-                    # If the buffer is empty, get out
-                    if not hot:
-                        break
+                    # Process the line.
+                    process_line(hot)
 
-            # Set the position to the last marker
-            hot.position = mark_position # reset
+                    # Advance the buffer window to the rest of the
+                    # buffer after the line
+                    hot.limit = abslimit
+                    hot.position += nidx + 1
+                    mark_position = hot.position
+                else:
+                    break
 
             # Read more data in the buffer.
 ## FIXME: we need to support reading from a file directly into the
@@ -461,17 +444,23 @@
             hot.compact()
             s = read(len(hot))
             if not s:
+                hot.flip()
                 break # Finished the input, exit.
             hot.putstr(s)
             hot.flip()
 
+        # Process the little bit at the end.
+        if hot:
+            process_line(hot)
+
+
     def test_newline_delim_data( self ):
         """
         Test for newline-delimited data.
         """
         inp = StringIO(self.data1)
-        hot = hotbuf(CAPACITY)
-        
+        hot = hotbuf(256)
+
         lineidx = [0]
         def assert_lines( hot ):
             "Assert the lines we process are the ones we expect."


More information about the Python-checkins mailing list