]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Avoid `hasattr` in hot loop in Brotli decoder (#906)
authorDavid Vo <auscompgeek@users.noreply.github.com>
Sun, 19 Apr 2020 07:03:56 +0000 (17:03 +1000)
committerGitHub <noreply@github.com>
Sun, 19 Apr 2020 07:03:56 +0000 (09:03 +0200)
httpx/_decoders.py

index cecd65da7f62889487299e75fc43e50b927da284..2a2e703d17a8c85cf4c6e62b437be78ae86fd293 100644 (file)
@@ -105,15 +105,17 @@ class BrotliDecoder(Decoder):
         ), "The 'brotlipy' or 'brotli' library must be installed to use 'BrotliDecoder'"
         self.decompressor = brotli.Decompressor()
         self.seen_data = False
+        if hasattr(self.decompressor, "decompress"):
+            self._decompress = self.decompressor.decompress
+        else:
+            self._decompress = self.decompressor.process  # pragma: nocover
 
     def decode(self, data: bytes) -> bytes:
         if not data:
             return b""
         self.seen_data = True
         try:
-            if hasattr(self.decompressor, "decompress"):
-                return self.decompressor.decompress(data)
-            return self.decompressor.process(data)  # pragma: nocover
+            return self._decompress(data)
         except brotli.error as exc:
             raise DecodingError from exc