]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
perf: micro optimise attribute access to Format
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 15 May 2022 16:43:01 +0000 (18:43 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 17 May 2022 19:28:29 +0000 (21:28 +0200)
psycopg/psycopg/_transform.py
psycopg/psycopg/client_cursor.py
psycopg/psycopg/connection.py
psycopg/psycopg/connection_async.py
psycopg/psycopg/copy.py
psycopg/psycopg/cursor.py
psycopg/psycopg/server_cursor.py

index 803c3cfc700fd2050dfc3a7f2519038579d9f103..91577f92dd3d49321b4bc6e785b15dd62daa3a9d 100644 (file)
@@ -28,6 +28,8 @@ DumperCache: TypeAlias = Dict[DumperKey, "Dumper"]
 OidDumperCache: TypeAlias = Dict[int, "Dumper"]
 LoaderCache: TypeAlias = Dict[int, "Loader"]
 
+TEXT = pq.Format.TEXT
+
 
 class Transformer(AdaptContext):
     """
@@ -168,7 +170,7 @@ class Transformer(AdaptContext):
             return out
 
         types = [INVALID_OID] * nparams
-        pqformats = [pq.Format.TEXT] * nparams
+        pqformats = [TEXT] * nparams
 
         for i in range(nparams):
             param = params[i]
index 09c3430d2bcc9b105a8550279aa9d56bc0c964e5..ee0e46c814283f06e131cc951e364e67e01544f3 100644 (file)
@@ -9,9 +9,9 @@ from functools import partial
 
 from ._queries import PostgresQuery, PostgresClientQuery
 
+from . import pq
 from . import adapt
 from . import errors as e
-from .pq import Format
 from .abc import ConnectionType, Query, Params
 from .rows import Row
 from .cursor import BaseCursor, Cursor
@@ -22,6 +22,9 @@ if TYPE_CHECKING:
     from .connection import Connection  # noqa: F401
     from .connection_async import AsyncConnection  # noqa: F401
 
+TEXT = pq.Format.TEXT
+BINARY = pq.Format.BINARY
+
 
 class ClientCursorMixin(BaseCursor[ConnectionType, Row]):
     def mogrify(self, query: Query, params: Optional[Params] = None) -> str:
@@ -46,9 +49,9 @@ class ClientCursorMixin(BaseCursor[ConnectionType, Row]):
         if binary is None:
             fmt = self.format
         else:
-            fmt = Format.BINARY if binary else Format.TEXT
+            fmt = BINARY if binary else TEXT
 
-        if fmt == Format.BINARY:
+        if fmt == BINARY:
             raise e.NotSupportedError(
                 "client-side cursors don't support binary results"
             )
index bf0d71affc58bdf919d4e9f3b2bcb2f2a6cdeeb3..d7e23f8195210860f007ed2d33352954c3c68ad8 100644 (file)
@@ -19,7 +19,7 @@ from . import pq
 from . import errors as e
 from . import waiting
 from . import postgres
-from .pq import ConnStatus, ExecStatus, TransactionStatus, Format
+from .pq import ConnStatus, ExecStatus, TransactionStatus
 from .abc import AdaptContext, ConnectionType, Params, Query, RV
 from .abc import PQGen, PQGenConn
 from .sql import Composable, SQL
@@ -42,12 +42,6 @@ if TYPE_CHECKING:
     from .pq.abc import PGconn, PGresult
     from psycopg_pool.base import BasePool
 
-logger = logging.getLogger("psycopg")
-
-# Row Type variable for Cursor (when it needs to be distinguished from the
-# connection's one)
-CursorRow = TypeVar("CursorRow")
-
 if _psycopg:
     connect = _psycopg.connect
     execute = _psycopg.execute
@@ -58,6 +52,15 @@ else:
     connect = generators.connect
     execute = generators.execute
 
+logger = logging.getLogger("psycopg")
+
+# Row Type variable for Cursor (when it needs to be distinguished from the
+# connection's one)
+CursorRow = TypeVar("CursorRow")
+
+TEXT = pq.Format.TEXT
+BINARY = pq.Format.BINARY
+
 
 class Notify(NamedTuple):
     """An asynchronous notification received from the database."""
@@ -423,7 +426,7 @@ class BaseConnection(Generic[Row]):
         return conn
 
     def _exec_command(
-        self, command: Query, result_format: Format = Format.TEXT
+        self, command: Query, result_format: pq.Format = TEXT
     ) -> PQGen[Optional["PGresult"]]:
         """
         Generator to send a command and receive the result to the backend.
@@ -439,7 +442,7 @@ class BaseConnection(Generic[Row]):
             command = command.as_bytes(self)
 
         if self._pipeline:
-            if result_format == Format.TEXT:
+            if result_format == TEXT:
                 cmd = partial(self.pgconn.send_query, command)
             else:
                 cmd = partial(
@@ -452,7 +455,7 @@ class BaseConnection(Generic[Row]):
             self._pipeline.result_queue.append(None)
             return None
 
-        if result_format == Format.TEXT:
+        if result_format == TEXT:
             self.pgconn.send_query(command)
         else:
             self.pgconn.send_query_params(command, None, result_format=result_format)
@@ -837,7 +840,7 @@ class Connection(BaseConnection[Row]):
             cur = self.cursor_factory(self, row_factory=row_factory)
 
         if binary:
-            cur.format = Format.BINARY
+            cur.format = BINARY
 
         return cur
 
@@ -853,7 +856,7 @@ class Connection(BaseConnection[Row]):
         try:
             cur = self.cursor()
             if binary:
-                cur.format = Format.BINARY
+                cur.format = BINARY
 
             return cur.execute(query, params, prepare=prepare)
 
index 5741597db287797a4efc0dc51c844cfa1e696791..5d7a636f5f4883721487a0edd527670afb1bd7a4 100644 (file)
@@ -12,9 +12,10 @@ from typing import Any, AsyncGenerator, AsyncIterator, Dict, List, Optional
 from typing import Type, Union, cast, overload, TYPE_CHECKING
 from contextlib import asynccontextmanager
 
+from . import pq
 from . import errors as e
 from . import waiting
-from .pq import Format, TransactionStatus
+from .pq import TransactionStatus
 from .abc import AdaptContext, Params, PQGen, PQGenConn, Query, RV
 from ._tpc import Xid
 from .rows import Row, AsyncRowFactory, tuple_row, TupleRow, args_row
@@ -32,6 +33,8 @@ from .server_cursor import AsyncServerCursor
 if TYPE_CHECKING:
     from .pq.abc import PGconn
 
+TEXT = pq.Format.TEXT
+BINARY = pq.Format.BINARY
 
 logger = logging.getLogger("psycopg")
 
@@ -245,7 +248,7 @@ class AsyncConnection(BaseConnection[Row]):
             cur = self.cursor_factory(self, row_factory=row_factory)
 
         if binary:
-            cur.format = Format.BINARY
+            cur.format = BINARY
 
         return cur
 
@@ -260,7 +263,7 @@ class AsyncConnection(BaseConnection[Row]):
         try:
             cur = self.cursor()
             if binary:
-                cur.format = Format.BINARY
+                cur.format = BINARY
 
             return await cur.execute(query, params, prepare=prepare)
 
index abd7addaeb219d0bfed8525b82205ccc1aaf191b..ade337ac688528898014f25c8b616c08192bd961 100644 (file)
@@ -33,6 +33,8 @@ if TYPE_CHECKING:
 
 TEXT = pq.Format.TEXT
 BINARY = pq.Format.BINARY
+PY_TEXT = PyFormat.TEXT
+PY_BINARY = PyFormat.BINARY
 
 
 class BaseCopy(Generic[ConnectionType]):
@@ -79,7 +81,7 @@ class BaseCopy(Generic[ConnectionType]):
         assert tx.pgresult, "The Transformer doesn't have a PGresult set"
         self._pgresult: "PGresult" = tx.pgresult
 
-        if self._pgresult.binary_tuples == pq.Format.TEXT:
+        if self._pgresult.binary_tuples == TEXT:
             self.formatter = TextFormatter(tx, encoding=pgconn_encoding(self._pgconn))
         else:
             self.formatter = BinaryFormatter(tx)
@@ -469,7 +471,7 @@ class Formatter(ABC):
 
 class TextFormatter(Formatter):
 
-    format = pq.Format.TEXT
+    format = TEXT
 
     def __init__(self, transformer: Transformer, encoding: str = "utf-8"):
         super().__init__(transformer)
@@ -514,7 +516,7 @@ class TextFormatter(Formatter):
 
 class BinaryFormatter(Formatter):
 
-    format = pq.Format.BINARY
+    format = BINARY
 
     def __init__(self, transformer: Transformer):
         super().__init__(transformer)
@@ -597,7 +599,7 @@ def _format_row_text(
 
     for item in row:
         if item is not None:
-            dumper = tx.get_dumper(item, PyFormat.TEXT)
+            dumper = tx.get_dumper(item, PY_TEXT)
             b = dumper.dump(item)
             out += _dump_re.sub(_dump_sub, b)
         else:
@@ -616,7 +618,7 @@ def _format_row_binary(
         out = bytearray()
 
     out += _pack_int2(len(row))
-    adapted = tx.dump_sequence(row, [PyFormat.BINARY] * len(row))
+    adapted = tx.dump_sequence(row, [PY_BINARY] * len(row))
     for b in adapted:
         if b is not None:
             out += _pack_int4(len(b))
index 3b4d01325a99979271588cb7f891d67713be2ed4..3f9fe2f8be0d1bce027aa11aff93bc0e62d5a1f4 100644 (file)
@@ -14,7 +14,7 @@ from contextlib import contextmanager
 from . import pq
 from . import adapt
 from . import errors as e
-from .pq import ExecStatus, Format
+from .pq import ExecStatus
 from .abc import ConnectionType, Query, Params, PQGen
 from .copy import Copy
 from .rows import Row, RowMaker, RowFactory
@@ -44,6 +44,9 @@ else:
 
 _C = TypeVar("_C", bound="Cursor[Any]")
 
+TEXT = pq.Format.TEXT
+BINARY = pq.Format.BINARY
+
 
 class BaseCursor(Generic[ConnectionType, Row]):
     __slots__ = """
@@ -61,7 +64,7 @@ class BaseCursor(Generic[ConnectionType, Row]):
 
     def __init__(self, connection: ConnectionType):
         self._conn = connection
-        self.format = Format.TEXT
+        self.format = TEXT
         self._pgconn = connection.pgconn
         self._adapters = adapt.AdaptersMap(connection.adapters)
         self.arraysize = 1
@@ -435,10 +438,10 @@ class BaseCursor(Generic[ConnectionType, Row]):
         if binary is None:
             fmt = self.format
         else:
-            fmt = Format.BINARY if binary else Format.TEXT
+            fmt = BINARY if binary else TEXT
 
         self._query = query
-        if query.params or no_pqexec or fmt == Format.BINARY:
+        if query.params or no_pqexec or fmt == BINARY:
             if self._conn._pipeline:
                 self._conn._pipeline.command_queue.append(
                     partial(
@@ -517,7 +520,7 @@ class BaseCursor(Generic[ConnectionType, Row]):
                 f"unexpected result status from query: {ExecStatus(result.status).name}"
             )
 
-    def _set_current_result(self, i: int, format: Optional[Format] = None) -> None:
+    def _set_current_result(self, i: int, format: Optional[pq.Format] = None) -> None:
         """
         Select one of the results in the cursor as the active one.
         """
@@ -581,7 +584,7 @@ class BaseCursor(Generic[ConnectionType, Row]):
         if binary is None:
             fmt = self.format
         else:
-            fmt = Format.BINARY if binary else Format.TEXT
+            fmt = BINARY if binary else TEXT
 
         if self._conn._pipeline:
             self._conn._pipeline.command_queue.append(
index f8ebf60ad4f4036f6c4f2593e363c67da59470a2..3d443f8c3d2c445b6948bf8d669b900eeb13cf4d 100644 (file)
@@ -22,6 +22,9 @@ if TYPE_CHECKING:
 
 DEFAULT_ITERSIZE = 100
 
+TEXT = pq.Format.TEXT
+BINARY = pq.Format.BINARY
+
 
 class ServerCursorMixin(BaseCursor[ConnectionType, Row]):
     """Mixin to add ServerCursor behaviour and implementation a BaseCursor."""
@@ -39,7 +42,7 @@ class ServerCursorMixin(BaseCursor[ConnectionType, Row]):
         self._withhold = withhold
         self._described = False
         self.itersize: int = DEFAULT_ITERSIZE
-        self._format = pq.Format.TEXT
+        self._format = TEXT
 
     def __repr__(self) -> str:
         # Insert the name as the second word
@@ -95,7 +98,7 @@ class ServerCursorMixin(BaseCursor[ConnectionType, Row]):
         if binary is None:
             self._format = self.format
         else:
-            self._format = pq.Format.BINARY if binary else pq.Format.TEXT
+            self._format = BINARY if binary else TEXT
 
         # The above result only returned COMMAND_OK. Get the cursor shape
         yield from self._describe_gen()