]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
perf: micro-optimize AdaptersMap.get_dumper
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 21 Dec 2022 23:00:32 +0000 (23:00 +0000)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 21 Dec 2022 23:00:32 +0000 (23:00 +0000)
Minimal amount of opcodes in case of fast-path (there is an adapter
precisely for the class queried).

psycopg/psycopg/_adapters_map.py

index a3a6ef88943b47ec5a463453b9499b562ec59e4c..70bf4cc02a52f83128beb29fc91af79843f2cebf 100644 (file)
@@ -197,9 +197,15 @@ class AdaptersMap:
             use the last one of the dumpers registered on `!cls`.
         """
         try:
-            dmap = self._dumpers[format]
+            # Fast path: the class has a known dumper.
+            return self._dumpers[format][cls]
         except KeyError:
-            raise ValueError(f"bad dumper format: {format}")
+            if format not in self._dumpers:
+                raise ValueError(f"bad dumper format: {format}")
+
+            # If the KeyError was caused by cls missing from dmap, let's
+            # look for different cases.
+            dmap = self._dumpers[format]
 
         # Look for the right class, including looking at superclasses
         for scls in cls.__mro__: