From: Daniele Varrazzo Date: Sun, 15 May 2022 16:43:01 +0000 (+0200) Subject: perf: micro optimise attribute access to Format X-Git-Tag: 3.1~89 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=090d4c09a759a97543b1d09f5f7a8fbef2d99a28;p=thirdparty%2Fpsycopg.git perf: micro optimise attribute access to Format --- diff --git a/psycopg/psycopg/_transform.py b/psycopg/psycopg/_transform.py index 803c3cfc7..91577f92d 100644 --- a/psycopg/psycopg/_transform.py +++ b/psycopg/psycopg/_transform.py @@ -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] diff --git a/psycopg/psycopg/client_cursor.py b/psycopg/psycopg/client_cursor.py index 09c3430d2..ee0e46c81 100644 --- a/psycopg/psycopg/client_cursor.py +++ b/psycopg/psycopg/client_cursor.py @@ -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" ) diff --git a/psycopg/psycopg/connection.py b/psycopg/psycopg/connection.py index bf0d71aff..d7e23f819 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -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) diff --git a/psycopg/psycopg/connection_async.py b/psycopg/psycopg/connection_async.py index 5741597db..5d7a636f5 100644 --- a/psycopg/psycopg/connection_async.py +++ b/psycopg/psycopg/connection_async.py @@ -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) diff --git a/psycopg/psycopg/copy.py b/psycopg/psycopg/copy.py index abd7addae..ade337ac6 100644 --- a/psycopg/psycopg/copy.py +++ b/psycopg/psycopg/copy.py @@ -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)) diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index 3b4d01325..3f9fe2f8b 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -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( diff --git a/psycopg/psycopg/server_cursor.py b/psycopg/psycopg/server_cursor.py index f8ebf60ad..3d443f8c3 100644 --- a/psycopg/psycopg/server_cursor.py +++ b/psycopg/psycopg/server_cursor.py @@ -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()