PythonCall.jl icon indicating copy to clipboard operation
PythonCall.jl copied to clipboard

To do: in-place API

Open cjdoris opened this issue 3 years ago • 6 comments

Make in-place versions of all the API functions, like:

pyimport!(ans, "sys")
pyadd!(ans, x, y)
pygetattr!(ans, x, "foo")

These replace the pointer in ans::Py with the result of the computation, instead of allocating a new Py each time. This will allow writing super-fast non-allocating code where needed.

You'd use these by pre-allocating places to store the results with pynew, which can optionally by freed with pydel!.

cjdoris avatar Apr 22 '22 16:04 cjdoris

The Python in-place operators would be like:

pyiadd!(ans, x)   # like ans += x

cjdoris avatar Apr 22 '22 16:04 cjdoris

These should generally return ans, to make chaining more convenient.

cjdoris avatar Apr 22 '22 16:04 cjdoris

A function

pyhasnext!(ans, iterator)

would be useful to return true if there is a next item, and if so set ans to its value.

cjdoris avatar Apr 22 '22 16:04 cjdoris

Even for complicated functions (such as pycall!) we can put all the temporary state we need into ans (e.g. by setting it to a tuple of all the things we need). We'd need to generalise the first argument ans to be anything with getptr and setptr! methods, and have a non-allocating PyTupleRef type which stores a pointer to a tuple and an index.

cjdoris avatar Apr 22 '22 16:04 cjdoris

This issue has been marked as stale because it has been open for 30 days with no activity. If the issue is still relevant then please leave a comment, or else it will be closed in 7 days.

github-actions[bot] avatar Sep 10 '23 01:09 github-actions[bot]

This issue has been closed because it has been stale for 7 days. If it is still relevant, please re-open it.

github-actions[bot] avatar Sep 18 '23 01:09 github-actions[bot]