Skip to content
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

Only mark unaligned types in buffers #505

Merged
merged 1 commit into from
Nov 22, 2016
Merged

Conversation

patstew
Copy link
Contributor

@patstew patstew commented Nov 16, 2016

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).

@wjakob
Copy link
Member

wjakob commented Nov 16, 2016

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
Copy link
Member

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

@aldanor
Copy link
Member

aldanor commented Nov 21, 2016

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, \
Copy link
Member

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.
@wjakob wjakob merged commit 47681c1 into pybind:master Nov 22, 2016
@patstew patstew deleted the buffer_alignment branch November 22, 2016 14:21
@rwgk rwgk mentioned this pull request Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants