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

fix(dev): Make sure C++ knows ustring & ustringhash are trivially copyable #4110

Merged
merged 2 commits into from
Jan 17, 2024

Conversation

lgritz
Copy link
Collaborator

@lgritz lgritz commented Jan 14, 2024

No description provided.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
Copy link
Collaborator

@ThiagoIze ThiagoIze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ustring is just doing a straight copy of its data which is just a pointer to a char*, and pointers are trivially copyable, so this should be fine.
The hash is a 64-bit int that is also just being copied as is, so that too is indeed trivially copyable.



// Force C++ to understand that ustring and ustringhash are trivially copyable
// (i.e. memcpy from one ustring to another is fine).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to mention the aspects that prevent c++ from just figuring this out. It seems that to make it trivially copyable, we'd have to = default the copy assignment operator as well as the destructor here. After that is_trivially_copyable will do the right thing by itself.

Is there any issue with just doing the following:

    /// ustring destructor.
    ~ustring() noexcept = default;

    /// Assign a ustring to another ustring.
    constexpr ustring& operator=(const ustring& str) = default;

It seems to work locally, and on a reduced repro in c++14 mode on godbolt, but it would mean changing the copy-assignment operator for both types to not return a const reference. That's not a typical copy-assignment setup and =default doesn't like that return value. Would that break ABI maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typical copy-assignment returns a mutable reference? Did I just miss this all along?

I don't mind doing this instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tink it shouldn't break ABI because these are all inline functions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in any case, any of these changes that would break ABI will not be backported to the current release branch.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't been able to make this work. Making these two default (and changing the return value of = and assign to be non-const, which I guess is the usual way) still doesn't make this trivially copyable, on any of our CI platforms. I'll noodle with it a little more and see if I can figure out why. @jessey-git can you please share your godbolt link so I can try to figure how what I'm doing is different?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I used to test: https://godbolt.org/z/sYn6jdh3G

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, my mistake.

* Use default implementations of copy ctr, destructor, and assignment
  from ustring. This allows ustring & ustringhash to be recognized as
  being a variety of flavors of trivially constructible, copyable, and
  destructible.

* It seems that the assignment operators were returning `const &`
  whereas it's more typical to return `&`, so now we do.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
@lgritz
Copy link
Collaborator Author

lgritz commented Jan 16, 2024

Take 2:

  • Use default implementations of copy ctr, destructor, and assignment
    from ustring. This allows ustring & ustringhash to be recognized as
    being a variety of flavors of trivially constructible, copyable, and
    destructible.

  • It seems that the assignment operators were returning const &
    whereas it's more typical to return &, so now we do.

@lgritz lgritz merged commit 3a5a00a into AcademySoftwareFoundation:master Jan 17, 2024
24 of 25 checks passed
@lgritz lgritz deleted the lg-ustringtrivial branch January 17, 2024 06:45
1div0 pushed a commit to 1div0/OpenImageIO that referenced this pull request Feb 24, 2024
…yable (AcademySoftwareFoundation#4110)

*  Use default implementations of copy ctr, destructor, and assignment
    from ustring. This allows ustring & ustringhash to be recognized as
    being a variety of flavors of trivially constructible, copyable, and
    destructible.

*   It seems that the assignment operators were returning const &
    whereas it's more typical to return &, so now we do.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
Signed-off-by: Peter Kovář <peter.kovar@reflexion.tv>
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.

3 participants