-
Notifications
You must be signed in to change notification settings - Fork 8
Fix empty reusable maps in ThreadPool #687
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
base: master
Are you sure you want to change the base?
Conversation
| explicit FixedVector(const size_t capacity) | ||
| : _max_size(capacity), | ||
| _size(0), | ||
| FixedVector static create_empty(const size_t capacity) { return FixedVector(capacity); } |
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.
| FixedVector static create_empty(const size_t capacity) { return FixedVector(capacity); } | |
| static FixedVector create_empty(const size_t capacity) { return FixedVector(capacity); } |
But honestly why did you need to add a static initializer function when you could've just added FixedVector(const size_t, T const&), its not like there is anything ambiguous about explicit FixedVector(const size_t)'s use, it also violates the expectations with std::vector.
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.
Creating an empty FixedVector is an edge case. We only need that for the IndexedFlatMap so it can validate keys before filling the vector.
The IndexedFlatMap has a constructor that takes in the keys only and generates the values for you.
The code in ThreadPool used to work like that but was rewritten to directly use the FixedVector. The similarity between the constructors allowed the bug to happen.
It's best to make a special create_empty method to avoid such issues.
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.
Then this isn't a drop-in replacement for std::vector and can't be used in the same place as vector, in which case it shouldn't be called a vector, the constructor follows the same behavior as std::vector, you have to worry about this exact same issue with std::vector because its about capacity, not size, that should be expected, it doesn't even make sense really to supply a capacity for a constructor without a value since that would be encouraging the use of UB anyway, also its not empty, its pre-allocated.
b689d6d to
2130060
Compare
The ThreadPool created empty vectors:
memory::FixedVector<fixed_point_t> reusable_country_map_0 { country_keys.size() };They have a fixed capacity but a size of 0. Subsequent indexing was out of bounds.
This PR fixes that by forcing you to either use
FixedVector::create_empty(capacity)or specify a way to initialise the values.The name
create_emptyis chosen to avoid accidental empty vector creation.