[python] Do create copies of local trees
In C++, localView.tree()
hands out a reference to a single Tree
stored within the LocalView
. In Python this was different so far:
A Python LocalView
wrapper hands out a new Python Tree
wrapper
object which itself stores a copy of the C++ Tree
leading to many copies.
While these copies are in principle expensive, this is hardly
measurable due to the large overhead of the bindings. However this has
several other implications.
First, it's not guaranteed that the C++ Tree
is safely copyable.
Second, the following pattern works in C++ but not in Python:
localView = basis.localView()
tree = localView.tree()
for element in basis.gridView.elements:
localView.bind(element)
# Here we cannot use tree because it's not bound.
# Furthermore we have now tree != locaView.tree()
This patch changes the holder type in the Python wrapper for Tree
to
std::shared_ptr<Tree>
instead of the default std::unique_ptr<Tree>
and lets the LocalView
wrapper hand out
Dune::stackobject_to_shared_ptr(localView.tree())
such that the Tree
wrapper always points to the C++ Tree
within the
LocalView
.
This also makes the above pattern work which is indeed measurably
faster that calling localView.tree()
on and on within the loop.