Skip to content

Commit a7f9f73

Browse files
committed
Cleanup temporary shape allocation
1 parent d2e19bb commit a7f9f73

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ conda install -c conda-forge xtensor-python
2828

2929
xtensor-python offers two container types wrapping numpy arrays inplace to provide an xtensor semantics
3030

31-
- ``pytensor``
32-
- ``pyarray``.
31+
- `pytensor`
32+
- `pyarray`.
3333

3434
Both containers enable the numpy-style APIs of xtensor (see [the numpy to xtensor cheat sheet](http://xtensor.readthedocs.io/en/latest/numpy.html)).
3535

36-
- On the one hand, ``pyarray`` has a dynamic number of dimensions. Just like numpy arrays, it can be reshaped with a shape of a different length (and the new shape is reflected on the python side).
36+
- On the one hand, `pyarray` has a dynamic number of dimensions. Just like numpy arrays, it can be reshaped with a shape of a different length (and the new shape is reflected on the python side).
3737

38-
- On the other hand ``pytensor`` has a compile time number of dimensions, specified with a template parameter. Shapes of ``pytensor`` instances are stack allocated, making ``pytensor`` a significantly
39-
faster expression than ``pyarray``.
38+
- On the other hand `pytensor` has a compile time number of dimensions, specified with a template parameter. Shapes of `pytensor` instances are stack allocated, making `pytensor` a significantly faster expression than `pyarray`.
4039

4140
### Example 1: Use an algorithm of the C++ standard library on a numpy array inplace.
4241

include/xtensor-python/pyarray.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ namespace xt
397397

398398
this->m_ptr = tmp.release().ptr();
399399
init_from_python();
400-
std::copy(rhs.data().begin(), rhs.data().end(), this->data().begin());
400+
std::copy(rhs.data().cbegin(), rhs.data().cend(), this->data().begin());
401401
}
402402

403403
/**
@@ -425,6 +425,7 @@ namespace xt
425425
inline pyarray<T>::pyarray(const xexpression<E>& e)
426426
: base_type()
427427
{
428+
// TODO: prevent intermediary shape allocation
428429
shape_type shape = forward_sequence<shape_type>(e.derived_cast().shape());
429430
strides_type strides = make_sequence<strides_type>(shape.size(), size_type(0));
430431
compute_strides(shape, layout_type::row_major, strides);

include/xtensor-python/pytensor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ namespace xt
306306
: base_type()
307307
{
308308
init_tensor(rhs.shape(), rhs.strides());
309-
std::copy(rhs.data().begin(), rhs.data().end(), this->data().begin());
309+
std::copy(rhs.data().cbegin(), rhs.data().cend(), this->data().begin());
310310
}
311311

312312
/**

include/xtensor-python/pyvectorize.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef PY_VECTORIZE_HPP
1010
#define PY_VECTORIZE_HPP
1111

12+
#include <type_traits>
13+
1214
#include "pyarray.hpp"
1315
#include "xtensor/xvectorize.hpp"
1416

@@ -20,13 +22,13 @@ namespace xt
2022
{
2123
xvectorizer<Func, R> m_vectorizer;
2224

23-
template <class F>
25+
template <class F, class = std::enable_if_t<!std::is_same<std::decay_t<F>, pyvectorizer>::value>>
2426
pyvectorizer(F&& func)
2527
: m_vectorizer(std::forward<F>(func))
2628
{
2729
}
2830

29-
inline pyarray<R> operator()(const pyarray<Args>&... args)
31+
inline pyarray<R> operator()(const pyarray<Args>&... args) const
3032
{
3133
pyarray<R> res = m_vectorizer(args...);
3234
return res;

0 commit comments

Comments
 (0)