-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Use std::vector instead of dynamic C-Array #6744
Conversation
src/mapgen/mapgen.h
Outdated
@@ -194,7 +194,8 @@ class Mapgen { | |||
s16 findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax); | |||
void updateHeightmap(v3s16 nmin, v3s16 nmax); | |||
void getSurfaces(v2s16 p2d, s16 ymin, s16 ymax, | |||
s16 *floors, s16 *ceilings, u16 *num_floors, u16 *num_ceilings); | |||
std::vector<s16> &floors, std::vector<s16> &ceilings, | |||
u16 * num_floors, u16 * num_ceilings); |
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.
Pointer misplaced. u16 *num_floors
, same for num_ceilings
I suppose the C-style arrays were used for speed reasons, as the maximal size of the data amount is known. (ask @paramat ?) |
Yes that's my code, i was unsure whether to use a vector instead of an array. I don't know of any advantage to an array, and vectors are used in most other places. Will test. |
Thanks, I know. Its just my suggestion to fix #6740 . I don't know whether this function is time critical or not. A vector is just a more secure variant (and not necessary slower at all). Feel free to fix #6740 with another way. Every variant would fine for me as long as it fixes #6740. The merge process might be faster if a coredev is doing this. |
I've seen vector used in many places in mapgen so i doubt it's slower, or significantly slower. I probably should have used vector in my PR anyway. |
I'm no expert concerning the relative advantages of one against the other. |
Thanks. A probably interesting article of C-Array vs std::array vs std::vector might be this by Bjarne Stroustrup: http:https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon1-prefer-using-stl-array-or-vector-instead-of-a-c-array |
Good, vector is fine for me. |
@paramat I added some improvements that got possible due to the usage of vectors. Could you please test it again? |
Interesting, i'll rereview to understand and then retest. Removed my +1 for now. |
Ceiling decorations are not working, see line comment. My fix works. |
src/mapgen/mapgen.cpp
Outdated
} else if (!is_walkable && walkable_above) { | ||
ceilings[ceiling_i] = y + 1; | ||
ceiling_i++; | ||
ceilings.push_back(y); |
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.
ceilings.push_back(y + 1);
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.
it's slower than C style array or std::array if no resize is done before passing them, please resize before sending the vectors
Don't forget std::vector push_back (or emplace_back) triggers a memory copy if it's full because std::vector is memory adjacent
Thanks for reviewing. Done.
|
Will retest then merge. |
Tested and fine 👍 |
Fixes #6740
I'm wondering, if it would possible/better to use a dynamic vector and
floors.size()
instead ofnum_floors
andnum_ceilings
and a fixed vector.But I does not want to break existing code and this PR is only meant to fix the failing build not for doing improvements.