From: Daniele Varrazzo Date: Sat, 10 Sep 2022 17:53:20 +0000 (+0100) Subject: perf(array): introduce mockup of optimized array loading functions X-Git-Tag: 3.1.5~12^2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ccf77632cdfc1d9217c531b1c9de391a790decb9;p=thirdparty%2Fpsycopg.git perf(array): introduce mockup of optimized array loading functions --- diff --git a/psycopg/psycopg/types/array.py b/psycopg/psycopg/types/array.py index 47290cdf4..cefb8fa2f 100644 --- a/psycopg/psycopg/types/array.py +++ b/psycopg/psycopg/types/array.py @@ -15,6 +15,7 @@ from ..abc import AdaptContext, Buffer, Dumper, DumperKey, NoneType, LoadFunc from ..adapt import RecursiveDumper, RecursiveLoader, PyFormat from .._compat import cache, prod from .._struct import pack_len, unpack_len +from .._cmodule import _psycopg from ..postgres import TEXT_OID, INVALID_OID from .._typeinfo import TypeInfo @@ -371,7 +372,7 @@ def register_all_arrays(context: AdaptContext) -> None: t.register(context) -def load_text( +def _load_text( data: Buffer, load: LoadFunc, delimiter: bytes = b",", @@ -438,7 +439,7 @@ def _get_array_parse_regexp(delimiter: bytes) -> Pattern[bytes]: ) -def load_binary(data: Buffer, load: LoadFunc) -> List[Any]: +def _load_binary(data: Buffer, load: LoadFunc) -> List[Any]: ndims, hasnull, oid = _unpack_head(data) if not ndims: @@ -462,3 +463,13 @@ def load_binary(data: Buffer, load: LoadFunc) -> List[Any]: out = [out[i : i + dim] for i in range(0, len(out), dim)] return out + + +# Override functions with fast versions if available +if _psycopg: + load_text = _psycopg.array_load_text + load_binary = _psycopg.array_load_binary + +else: + load_text = _load_text + load_binary = _load_binary diff --git a/psycopg_c/psycopg_c/_psycopg.pyi b/psycopg_c/psycopg_c/_psycopg.pyi index 0da2cab50..86b3977df 100644 --- a/psycopg_c/psycopg_c/_psycopg.pyi +++ b/psycopg_c/psycopg_c/_psycopg.pyi @@ -72,4 +72,10 @@ def format_row_binary( def parse_row_text(data: abc.Buffer, tx: abc.Transformer) -> Tuple[Any, ...]: ... def parse_row_binary(data: abc.Buffer, tx: abc.Transformer) -> Tuple[Any, ...]: ... +# Arrays optimization +def array_load_text( + data: abc.Buffer, load: abc.LoadFunc, delimiter: bytes = b"," +) -> List[Any]: ... +def array_load_binary(data: abc.Buffer, load: abc.LoadFunc) -> List[Any]: ... + # vim: set syntax=python: diff --git a/psycopg_c/psycopg_c/_psycopg.pyx b/psycopg_c/psycopg_c/_psycopg.pyx index 45c281e12..e4faa8088 100644 --- a/psycopg_c/psycopg_c/_psycopg.pyx +++ b/psycopg_c/psycopg_c/_psycopg.pyx @@ -40,6 +40,7 @@ include "_psycopg/copy.pyx" include "_psycopg/generators.pyx" include "_psycopg/transform.pyx" +include "types/array.pyx" include "types/datetime.pyx" include "types/numeric.pyx" include "types/bool.pyx" diff --git a/psycopg_c/psycopg_c/types/array.pyx b/psycopg_c/psycopg_c/types/array.pyx new file mode 100644 index 000000000..7c4b47b81 --- /dev/null +++ b/psycopg_c/psycopg_c/types/array.pyx @@ -0,0 +1,14 @@ +""" +C optimized functions to manipulate arrays +""" + +# Copyright (C) 2022 The Psycopg Team + +def array_load_text( + data: Buffer, load: LoadFunc, delimiter: bytes = b"," +) -> List[Any]: + raise NotImplementedError + + +def array_load_binary(data: Buffer, load: LoadFunc) -> List[Any]: + raise NotImplementedError