redis-py icon indicating copy to clipboard operation
redis-py copied to clipboard

Possible big speedup

Open miketheanimal opened this issue 13 years ago • 2 comments

Using redis-py 2.7.6, but the latest code looks the same in the bit I've hacked on. This might just be a Python 3 problem, not tried with Python 2.

I have some (Python3) code that extracts JSON-encoded objects from a Redis cache, and inserts corresponding records into a Postgres database (uses SQLAlchemy core). The test case I've been using has about 1500 objects (which I preload into Redis) and results in around 30000 inserts into the database, and took about 80 seconds to run. Profiling showed most time was taken in redis-py, receiving data back from Redis. The culprit seems to be calls to readline on the connection object.

The code below reduces the time to about 20 seconds! I hope showing it here is considered OK, it might be there is a better way to make the same improvement. The code is in connections.py.

BufferWrapper is a simple wrappper around the connection object (which is an instance of io.BufferedReader) which itself buffers data; it avoids calling the underlying io.BufferedReader.readline method (and uses read1 instead). It may be that io.BufferedReader.readline is the real culprit here:)

class BufferWrapper:

    def __init__(self, fp):
        self._fp = fp
        self._pending = b''

    def read(self, read_len):
        if read_len > 0:
            if len(self._pending) >= read_len:
                r = self._pending[:read_len]
                self._pending = self._pending[read_len:]
                return r
            extra = read_len - len(self._pending)
            r = self._pending
            self._pending = b''
            return r + self._fp.read(extra)
        if len(self._pending) > 0:
            r = self._pending
            self._pending = b''
            return r
        return self._fp.read()

    def readline(self):
        ls = self._pending.find(b'\r\n')
        while ls < 0:
            ex = self._fp.read1(1000000)
            if len(ex) == 0:
                r = self._pending
                self._pending = b''
                return r
            self._pending += ex
            ls = self._pending.find(b'\r\n')
        r = self._pending[:ls+2]
        self._pending = self._pending[ls+2:]
        return r

    def close(self):
        self._fp.close()

The sole other change is in PythonParser.on_connect as the above class implements the same interface (so far as I can tell:)

self._fp = BufferWrapper(connection._sock.makefile('rb'))

Tested on Ubuntu 13.04, Python 3.3.1

Regards Mike

miketheanimal avatar Oct 22 '13 08:10 miketheanimal

Hey Mike. Apologizes that this has waited for so long.

I suspect the big speedup you're seeing is because you're reading data from the socket in 1MB chunks. I recently increased the default number of bytes that are read from the socket at a time and also made is configurable so others could tweak it to their liking. I suspect if you run your benchmark again against the redis-py:master branch, you'll see much better times.

andymccurdy avatar May 07 '14 06:05 andymccurdy

This issue is marked stale. It will be closed in 30 days if it is not updated.

github-actions[bot] avatar Aug 20 '20 00:08 github-actions[bot]