Skip to content

Commit 3908319

Browse files
authored
Fix vector type native adapters (#1263)
`Vector.from_native` and `Vector.to_native` both used to have alternative implementations using NumPy or the Rust extensions when available to speed up the process. It turns out, that both didn't follow the reference Python implementation (internally relying on `struct.pack`/`struct.unpack`) for 32 bit float values. * Python 3.14+ preserves the signaling/quieting bit of `NaN` values whereas NumPy doesn't and Rust doesn't make strong guarantees on how f64s are cast to f32s: https://github.com/rust-lang/rfcs/blob/master/text/3514-float-semantics.md * Both NumPy and Rust turn large f64s into `inf` when cast to f32, while Python raises an `OverflowError`. To harmonize the behavior, only the Python implementation remains. It has been significantly sped up by not iterating over the elements and calling `struct.(un)pack` multiple times, but instead making use of the fact that the format string allows to specify a number of values to (un)pack. With this the performance of the Python implementation was never slower than 5 times of what Rust/NumPy offered and for low dimension vectors even faster. It's deemed not worth the effort of maintaining multiple implementations assuring their behavioral parity for such a small performance gain. The same multi-value (un)pack approach has been applied to the pure Python implementation of the byte swapping to improve it's performance by a factor of roughly 30 (still a factor of ~10 slower than using NumPy/Rust).
1 parent 3a0c89e commit 3908319

File tree

3 files changed

+389
-551
lines changed

3 files changed

+389
-551
lines changed

src/neo4j/_typing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
Mapping,
3131
Sequence,
3232
Set,
33+
Sized,
3334
ValuesView,
3435
)
3536
from importlib.util import find_spec as _find_spec
@@ -79,6 +80,7 @@
7980
"Protocol",
8081
"Sequence",
8182
"Set",
83+
"Sized",
8284
"SupportsIndex",
8385
"TextIO",
8486
"TypeAlias",

0 commit comments

Comments
 (0)