From: Thomas Jarosch Date: Thu, 30 Jun 2016 08:03:40 +0000 (+0200) Subject: Don't use exception handling for normal control flow X-Git-Tag: v2.2~13 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=7656941d4d705ee317dacde9991e813860deb6a9;p=python-delta-tar Don't use exception handling for normal control flow -> Replace buf.index() with buf.find(). Unwinding the stack is expensive and we were even doing it for the default code path. --- diff --git a/deltatar/tarfile.py b/deltatar/tarfile.py index 4cf0e69..220f09f 100644 --- a/deltatar/tarfile.py +++ b/deltatar/tarfile.py @@ -845,22 +845,22 @@ class _Stream: if not buf: return buf - try: - idx = buf.index(b'Salted__') - except ValueError: - buf = self.encryption.decrypt(buf, last) + idx = buf.find(b'Salted__') + if idx == -1: + return self.encryption.decrypt(buf, last) + + b1 = buf[:idx] + b2 = buf[idx:] + if b1: + buf = self.encryption.decrypt(b1, True) else: - b1 = buf[:idx] - b2 = buf[idx:] - if b1: - buf = self.encryption.decrypt(b1, True) - else: - buf = b'' + buf = b'' + + self.encryption.get_salt_str(b2) + self.encryption.init() + b2 = b2[len(self.encryption.salt_str):] + buf += self.__split_enc_file(b2, last) - self.encryption.get_salt_str(b2) - self.encryption.init() - b2 = b2[len(self.encryption.salt_str):] - buf += self.__split_enc_file(b2, last) return buf # class _Stream