]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
perf(array): introduce mockup of optimized array loading functions
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 10 Sep 2022 17:53:20 +0000 (18:53 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 10 Dec 2022 13:01:55 +0000 (13:01 +0000)
psycopg/psycopg/types/array.py
psycopg_c/psycopg_c/_psycopg.pyi
psycopg_c/psycopg_c/_psycopg.pyx
psycopg_c/psycopg_c/types/array.pyx [new file with mode: 0644]

index 47290cdf448190db7ac27d93a6df2c61d9f9839f..cefb8fa2f16852235b258a399059da0c9b3e30f4 100644 (file)
@@ -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
index 0da2cab503195b20443bf2538bd504d4114018c0..86b3977df168cbca553f280cf58da96d68225cba 100644 (file)
@@ -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:
index 45c281e123048a3887710dd507fa00406a6ca74a..e4faa808846fe12be28ab623ce6c4659847671e4 100644 (file)
@@ -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 (file)
index 0000000..7c4b47b
--- /dev/null
@@ -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