From 7656941d4d705ee317dacde9991e813860deb6a9 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Thu, 30 Jun 2016 10:03:40 +0200 Subject: [PATCH] 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. --- deltatar/tarfile.py | 30 +++++++++++++++--------------- 1 files changed, 15 insertions(+), 15 deletions(-) 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 -- 1.7.1