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

Constexpr-ify inplace_vector #21

Closed
wants to merge 16 commits into from
Closed

Conversation

wusatosi
Copy link
Member

@wusatosi wusatosi commented Nov 18, 2024

Make inplace_vector constexpr friendly by providing an T-based array storage alternative that avoids the needs of reinterpret_cast.

This storage alternative is only selected when std::is_trivial(T), as that's the requirement for enabling constexpr in the paper.

Adds constexpr based tests.

Related sections in paper:

constexpr support

Note: this revision brings back this section which was dropped in R8 because per LEWG discussion, LEWG thought that this whole type was implementable within constexpr for all types. The design intent of LEWG was to make it "as constexpr as possible."

The API of inplace_vector<T, Capacity> can be used in constexpr-contexts if is_trivially_copyable_v, is_default_constructible_v, and is_trivially_destructible are true. This proposal only supports using the constexpr methods in constant expressions if is_trivial_t is true.
The implementation cost of this is small. The prototye implementation specializes the storage to use a C array with value-initialized elements.
This negatively impacts the algorithmic complexity of inplace_vector constructors for these types from O(size) to O(capacity). When value-initialization takes place at run-time, this difference is significant.
Vectors with large capacity requirements are better served by vector instead.

All member functions should be constexpr.

Added a constexpr test that is not finished, I fear the code-fix resulted due to passing constexpr on all functions would result in too big of a PR. Currently [container.reqmts] related requirement is tested and fixed for int type.

Closes #17

include/beman/inplace_vector/inplace_vector.hpp Outdated Show resolved Hide resolved
include/beman/inplace_vector/inplace_vector.hpp Outdated Show resolved Hide resolved
include/beman/inplace_vector/inplace_vector.hpp Outdated Show resolved Hide resolved
@wusatosi wusatosi marked this pull request as ready for review November 21, 2024 20:14
@wusatosi wusatosi requested a review from jbab November 25, 2024 02:45
Copy link
Collaborator

@jbab jbab left a comment

Choose a reason for hiding this comment

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

I basically agree with your changes. Most of my remarks (and some serious bugs) are in the code you didn't change (some even where GitHub doesn't allow me to put a comment).
Not sure how to handle this. Maybe it's good when your PR gets to the main line and then we can work on the rest.

include/beman/inplace_vector/inplace_vector.hpp Outdated Show resolved Hide resolved
include/beman/inplace_vector/inplace_vector.hpp Outdated Show resolved Hide resolved
Comment on lines 83 to 86
T *begin() { return std::launder(reinterpret_cast<const T *>(elems)); }
const T *begin() const {
return std::launder(reinterpret_cast<const T *>(elems));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the reason you are using std::launder here?

Copy link
Member Author

@wusatosi wusatosi Nov 25, 2024

Choose a reason for hiding this comment

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

This is to be consistent with previous implementation, I am bit confused myself too. I am not in a good place to make judgement call here, though I suspect it might be needed when T is const.

I don't think we should change this.

include/beman/inplace_vector/inplace_vector.hpp Outdated Show resolved Hide resolved
internal_size_type size_{0};

// [containers.sequences.inplace.vector.cons], construct/copy/destroy
constexpr inplace_vector_destruct_base() = default;

inplace_vector_destruct_base(
constexpr inplace_vector_destruct_base(
const inplace_vector_destruct_base
&other) noexcept(std::is_nothrow_copy_constructible_v<T>)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not your change, but to me this is unconditionally noexcept, since no T's are copied at all.

Copy link
Member Author

@wusatosi wusatosi Nov 25, 2024

Choose a reason for hiding this comment

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

okay I will mark a TODO here. There's a lot of non-conformity in this implementation.

I think we should defer this to another PR, there's too much to be updated.

Comment on lines 117 to 118
std::is_nothrow_copy_constructible_v<T> &&
std::is_nothrow_copy_assignable_v<T>) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not your change, but to me this is unconditionally noexcept, since no T's are copied at all.

Comment on lines 124 to 125
std::is_nothrow_move_constructible_v<T> &&
std::is_nothrow_move_assignable_v<T>) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not your change, but to me this is unconditionally noexcept, since no T's are copied at all.

internal_size_type size_{0};

// [containers.sequences.inplace.vector.cons], construct/copy/destroy
constexpr inplace_vector_destruct_base() = default;

inplace_vector_destruct_base(
constexpr inplace_vector_destruct_base(
const inplace_vector_destruct_base
&other) noexcept(std::is_nothrow_copy_constructible_v<T>)
: elems(), size_(other.size_) {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not your change, but I need to make some remark on all constructors in inplace_vector_destruct_base.
IMO, it would be O.K. to address this in a later PR.

They all value-initialise elems, i.e. the bytes in bytes_based_Storage become 0 and all elements in type_based_storage are value initialised. I tried to argue above why I don't like this, they should be initialised only when a vector element is really constructed in the place.

Besides (superfluously) initialising elems, they all do the same thing, they set size_ to some value. That is, they avoid doing what a copy c'tor, move c'tor etc. normally does and instead manipulate size_only. This is redundant. Instead, a single constructor taking a size would do. This could be called by the all the constructors in the deriving class. size_could probably be made private.

Copy link
Member Author

@wusatosi wusatosi Nov 26, 2024

Choose a reason for hiding this comment

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

inplace_vector_destruct_base needs more work on trivially destructible requirement as well, needs to switch between a trivially destructible base and a non-trivial destructor against T.

Unfortunately intention in this repo is to support C++17, so we cannot use requires. This have to be done with template.

std::is_nothrow_copy_constructible_v<T>)
: inplace_vector_destruct_base<T, Capacity>(other.size) {
: inplace_vector_destruct_base<T, Capacity>(other.size_) {
std::copy(other.begin(), other.end(), begin());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not your change, but needs to be an uninitialised_copy, in particular for the byte based storage case.
It happens work in the type based storage case.

Copy link
Member Author

Choose a reason for hiding this comment

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

why is uninitialised_copy needed here?

std::copy(other.begin(), other.end(), begin());
}
inplace_vector_base(inplace_vector_base &&other) noexcept(
constexpr inplace_vector_base(inplace_vector_base &&other) noexcept(
Capacity == 0 || std::is_nothrow_move_constructible_v<T>)
: inplace_vector_destruct_base<T, Capacity>(other.size_) {
std::copy(other.begin(), other.end(), begin());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not your change, but must be uninitalized_move, see above.

@wusatosi
Copy link
Member Author

wusatosi commented Nov 25, 2024

I basically agree with your changes. Most of my remarks (and some serious bugs) are in the code you didn't change (some even where GitHub doesn't allow me to put a comment). Not sure how to handle this. Maybe it's good when your PR gets to the main line and then we can work on the rest.

Thank you for review jbab. I agree that there's a lot of problems in this current implementation. That's why I am not attempting to setup a full test suite for constexpr. I think it will cause too big of a change.

This is just my thoughts and I haven't really discussed this in beman. But I am reaching out to david to see if we should just adopt the reference implementation in the paper (if the original author allows). As there's a lot of work to be done here and the reference implementation will be a better place to be worked off.

Edit: okay I figured this out myself... Here's the paper allowing default trivial initialization.

I don't get why inplace_vector have that note on the constexpr support. I will see if I can shoot the authors an email.

Previous confusion

Regarding storage, mostly due to my own limited understanding, I am not sure how constexpr plays with default-initialized memory, I thought all value must be initialized to a defined state to play nice with constexpr.

e.g. I thought func in the follow code is not constexpr because a is not initialized.

constexpr int func() {
    int a[5];
    a[0] = 1;
    return a[0];
}

Which is false because this passes godbolt...

However, in the paper, it mentions this wording:

Constexpr support
...
This negatively impacts the algorithmic complexity of inplace_vector constructors for these types from O(size) to O(capacity). When value-initialization takes place at run-time, this difference is significant.

Which suggest we need to value-initialize the T based array to make it play well with constexpr (use std::array storage<T, Capacity>{}). Which would suggest my understanding is correct, which makes me more confused.

I mean compiler seem to be fine with it, I just want to make sure this is intended per standard.

I tried to check with cppreference on constexpr, but I don't see how this is specified either way.

Edit:
The same godbolt code doesn't work on 17, but we aren't supporting constexpr for C++17 (at least for now) so we should be fine.

@wusatosi
Copy link
Member Author

Btw, it is likely we will need to update this storage container to include size to ensure this fits an empty struct requirement when Capacity = 0. But again I want to keep this PR small so I will add this later.

@wusatosi
Copy link
Member Author

@jbab If you don't mind, can you post an issue regarding questionable restraints surrounding constructors. I will deal with them in another PR.

wusatosi and others added 2 commits November 25, 2024 22:41
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@wusatosi wusatosi requested a review from jbab November 26, 2024 04:23
@wusatosi
Copy link
Member Author

wusatosi commented Nov 26, 2024

Hey @jbab , I've been given the green light to adopt the reference implementation from the paper.

I will be syncing that to a new branch and do a merge next.

Let's scrutinize the reference implementation first, if we have time we can come back and make current implementation great

@wusatosi wusatosi closed this Nov 26, 2024
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.

Most of inplace_vector is not compatible with constexpr
2 participants