Don't use exception handling for normal control flow
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 30 Jun 2016 08:03:40 +0000 (10:03 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 30 Jun 2016 08:05:33 +0000 (10:05 +0200)
-> 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

index 4cf0e69..220f09f 100644 (file)
@@ -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