-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Make access unsafe #599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make access unsafe #599
Conversation
6ec5e27
to
e88d9e3
Compare
e88d9e3
to
94bfc70
Compare
Why not use As far as I know,
I think the main issue with making the default Keeping the Python behavior makes it consistent and easy to explain: "It looks like a |
(From the original issue) I agree that when exposing a feature to Python, it must be written in such a way that regardless of the values passed to it, it cannot crash the interpreter, but these accessors are not directly exposed to the Python users. Instead, I think that one of the main reasons people would right c++ extensions with pybind is because they want speed, and they might be disappointed with this behavior. #500 already improved the situation, but as we discussed there, I would prefer to completely remove the bound checking. |
An alternative proposal would be to have two functions of each kind: |
cc @aldanor |
Safe vs unsafe access could be determined with by tag dispatching through a policy. |
Can you elaborate what this would look like? |
Arrays would have an additional template parameter whose type determines whether things are checked. |
Sure, but even with unsafe access My main worry is that But I may have misinterpreted the purpose of |
Sure, but looping is still something that people will want to do reasonably fast. Checking attributes at every iteration is a good way to kill performance. |
Perhaps I'm looking at this too much from the C++ side, where we have the performance and want to extend to Python for the flexibility. So I'm mostly looking at In that case, the interface should also get an overhaul. Currently, the main way to access elements is So I guess my main point is: if it's going to be unsafe, make it consistently unsafe so that there's no false sense of security. That's also required for full c-array-level access speed. (Although I'm still not convinced that unsafe-by-default should be the purpose of |
@dean0x7d: In that case, what do you propose for the read-only version of |
Regarding the writable check, what exactly is the semantic of it in numpy? |
>>> import numpy as np
>>> a = np.array([0])
>>> a.flags.writeable = False
>>> a[0] = 1
ValueError: assignment destination is read-only |
Yeah since it is determined at runtime, this cannot really play well with the const semantics of c++ |
But computing byte offset from scratch on every iteration is not optimal regardless. If you have a tight loop, the chances are you'll fetch the data pointer once anyway and then increment the strides manually in the loops to avoid needless multiplications and stride fetching? NumPy C API has some macros ( |
I agree, xtensor has efficient iterator pairs that allow people to iterate more efficiently on arrays, but we should expect people to write quick-&-easy things with loops over indices. |
Just a random thought, another option: alongside the existing methods, add arr[{i, j, k}] (that is, if it's possible to do this so it's inlined to the same thing as |
In xtensor, we have different semantics for
|
Apologies in advance for the following rant: I think that repeatedly bringing xtensor into the discussions here at pybind11 is not conductive. If there is some clever technical insight that can be ported to this project, then that's all fine with me. However, there is really no reason why the API/style of this project should somehow be bound to that of xtensor (rather than NumPy, which we're trying to wrap) |
I apologize if you found that mentioning xtensor in the discussions on numpy bindings was not appropriate. I thought that bringing this perspective in the discussion would help since we are working on numpy-style semantics in c++. I deleted the comment where I detail the iterators that we use which was not so much related to the discussion. |
Actually, I wonder if someone would mix checked and unchecked access in their code. If that's not the case, maybe a policy-based design as mentioned by @SylvainCorlay could help. The default behavior would be the actual one, so it would remain consistent with the one of Python, and would avoid a frustrating learning curve as @dean0x7d pointed it out. And people that need pure performance could have the behavior they want with a simple template parameter change. Besides, that would keep the interface simple and intuitive, and would avoid a counter intuitive change in Would that be an acceptable change for you guys ? If so, I can start working on it. |
I think policy-based would be a nice way to go. Using Python n = a.shape[1]
x = a[3, 3] C++ auto n = a.shape(1)
auto x = a(3, 3)
Regarding |
I like the idea of the safe version check for the mutability for the |
I'm wondering what the status is with regards to this? It looks like there was a useful discussion about a potential API, but that never quite materialized in a concrete PR. Is there still a plan to implement a form of unchecked access? |
I think the main discussion has moved to #617 where there are 2 active proposals. It would be best to continue over there and close this one. |
This is the removal of the bound checks I am proposing in #584 .