Skip to content

Commit 065c357

Browse files
committed
Back out of the change to PyArray::from_slice for now as we cannot make this efficient without additional API surface in PyO3.
1 parent 3b80ff5 commit 065c357

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/array.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,18 @@ impl<T: Element> PyArray<T, Ix1> {
952952
/// });
953953
/// ```
954954
pub fn from_slice<'py>(py: Python<'py>, slice: &[T]) -> &'py Self {
955-
let data = slice.to_vec();
956-
data.into_pyarray(py)
955+
unsafe {
956+
let array = PyArray::new(py, [slice.len()], false);
957+
if T::IS_COPY {
958+
ptr::copy_nonoverlapping(slice.as_ptr(), array.data(), slice.len());
959+
} else {
960+
let data_ptr = array.data();
961+
for (i, item) in slice.iter().enumerate() {
962+
data_ptr.add(i).write(item.clone());
963+
}
964+
}
965+
array
966+
}
957967
}
958968

959969
/// Construct one-dimension PyArray
@@ -969,7 +979,7 @@ impl<T: Element> PyArray<T, Ix1> {
969979
/// });
970980
/// ```
971981
pub fn from_vec<'py>(py: Python<'py>, vec: Vec<T>) -> &'py Self {
972-
IntoPyArray::into_pyarray(vec, py)
982+
vec.into_pyarray(py)
973983
}
974984

975985
/// Construct one-dimension PyArray from a type which implements

0 commit comments

Comments
 (0)