-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Only mark unaligned types in buffers #505
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
Conversation
cc @aldanor |
@@ -727,7 +728,9 @@ inline PYBIND11_NOINLINE void register_structured_dtype( | |||
if (field.offset > offset) | |||
oss << (field.offset - offset) << 'x'; | |||
// note that '=' is required to cover the case of unaligned fields |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, but maybe this comment line should be updated to reflect what's happening below
This looks ok, I guess, and doesn't seem to break anything (I don't see much value since comparing format strings directly is a bad idea, but it doesn't harm to have aligned fields mark as aligned). |
@@ -787,6 +790,7 @@ struct npy_format_descriptor<T, enable_if_t<is_pod_struct<T>::value>> { | |||
#define PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, Name) \ | |||
::pybind11::detail::field_descriptor { \ | |||
Name, offsetof(T, Field), sizeof(decltype(std::declval<T>().Field)), \ | |||
std::alignment_of<decltype(std::declval<T>().Field)>::value, \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason not to use the alignof
keyword? (AFAIK the template version only exists for historical reasons.)
Minor formatting note: see the position of the continuation \
in comparison to the lines above and below.
Previously all types are marked unaligned in buffer format strings, now we test for alignment before adding the '=' marker.
25eb980
to
2f197c9
Compare
Previously all types are marked unaligned in buffer format strings, now we test for alignment before adding the '=' marker.
It's not a problem to mark all types as unaligned per-se, it's equivalent because of the explicit padding, but this goes partway to allowing comparison to format strings from other sources, e.g. numpy. It's much easier to test for alignment here rather than parsing the string and trying to work it out. In combination with my other PR #488 , it will be possible to construct a
vector<struct>
from a structured array without casting in some cases (namely where the member names are identical and there's no multibyte padding).