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
String Compression - resolve MSVC warnings for stack memory allocation. #5503
base: master
Are you sure you want to change the base?
Conversation
Draft pull request has been created and is ready for review. |
@@ -12,7 +12,6 @@ | |||
|
|||
|
|||
|
|||
|
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.
Please don't erase empty lines between functions (style: there are to be 5 empty lines between function definitions). Make sure you run the src/CheckBasicStyle.lua
script before committing.
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.
Ok, I will check the pre-commit hook.
I am not sure why it did not run but I will run the pre-commit hook manually next time before the next commit.
The deleted line will be re-added to allow for 5 lines between function definitions.
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.
You need to explicitly enable the pre-commit hook on your local machine (and install Lua, if you haven't already). This is a security measure in git, so that foreign repositories can't run arbitrary code on your computer by putting it into a hook. CONTRIBUTING.md has more info on how to do this.
If you need a Lua interpreter for Windows, my LunaPaak project could help you - it's a single executable that you download and run once, it will register itself as Lua interpreter, and comes with several libraries built in, so it's pretty good for scripting.
src/StringCompression.cpp
Outdated
|
||
auto DynamicCapacity = Result::StaticCapacity * 2; | ||
while (true) | ||
{ | ||
size_t BytesWrittenOut; | ||
size_t BytesWrittenOut {}; |
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.
This is more readable, if MSVC really requires initialization:
size_t BytesWrittenOut {}; | |
size_t BytesWrittenOut = 0; |
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.
That is useful to know.
I will use that syntax to initialize instead.
src/StringCompression.h
Outdated
/** A store allocated on the heap. */ | ||
/** Note that this remains a variant for now. */ | ||
std::variant<Dynamic> Storage; |
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.
Since there's now only one type, it no longer needs to be a a variant
. In fact, it could very well be a std::vector<byte>
for simplification. (Note that this will cause a lot of other changes needed)
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.
Yes, this previously needed the std::variant
because of needing to accommodate either a std::array<std::byte>
or std::unique_ptr<std::byte[]>
for type safety.
To change Storage
to a std::vectorstd::byte would need some refactoring in ./src/StringCompression.cpp
, but it would be good to simplify and to boost performance.
Would this be raised as an additional issue on git after this current issue?
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.
No, I think you should do this in this PR.
Hello. Good first try. I think this whole thing has been really over-engineered and could use some simplification. So using a simple Also, it would be great if you added some documentation to the classes and functions, regarding how to use them. I know it's not part of the original task, but since you're already pretty much rewriting the code, it makes sense to document stuff as long as it's fresh in your head. Please check the contributing guidelines in CONTRIBUTING.md. There are the code style guidelines, as well as some requirements. You need to add yourself to the CONTRIBUTORS file to indicate your agreement with publishing your under our code license. |
Thank you for the feedback. Should I first make the minor revisions requested for restoring the deleted line and changing the initializer for Perhaps I can make a pull request for these two changes to address the current issue and then proceed with the additional changes later? I will review the |
You should just commit the changes to your repo under the same branch, this will update this PR with the new code. |
Hello, I just pushed another commit to my remote. |
Looks like the commit has not gone through correctly. |
Commit |
return Buffer.get(); | ||
} | ||
}, Storage), Size | ||
&(this->Storage.get()->front()), |
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.
&(this->Storage.get()->front())
Is this a safe method to use?
I suppose that if Storage
is not empty then this would work, but if it is then would this cause issues?
std::variant<Static, Dynamic> Storage; | ||
/** A store allocated on the heap. | ||
This uses a smart pointer. */ | ||
std::unique_ptr <std::vector <std::byte>> Storage; |
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.
std::unique_ptr <std::vector <std::byte>> Storage;
As suggested, I have used a std::vector<std::byte>
here.
To allocate on the heap, this has been declared using a smart pointer, specifically std::unique_ptr
.
This should fix the errors thrown by the script in `continuous-integration/jenkins/pr-merge`: > /home/debian/workspace/cuberite_PR-5503/src/StringCompression.cpp:89:5: error: moving a temporary object prevents copy elision [-Werror,-Wpessimizing-move] std::move (std::make_unique <std::vector <std::byte> > (Dynamic.get(), (Dynamic.get() + DynamicCapacity))), ^ /home/debian/workspace/cuberite_PR-5503/src/StringCompression.cpp:89:5: note: remove std::move call here std::move (std::make_unique <std::vector <std::byte> > (Dynamic.get(), (Dynamic.get() + DynamicCapacity))), ^~~~~~~~~~~ ~ /home/debian/workspace/cuberite_PR-5503/src/StringCompression.cpp:205:6: error: moving a temporary object prevents copy elision [-Werror,-Wpessimizing-move] std::move (std::make_unique <std::vector <std::byte> > (Dynamic.get(), (Dynamic.get() + DynamicCapacity))), ^ /home/debian/workspace/cuberite_PR-5503/src/StringCompression.cpp:205:6: note: remove std::move call here std::move (std::make_unique <std::vector <std::byte> > (Dynamic.get(), (Dynamic.get() + DynamicCapacity))), ^~~~~~~~~~~ ~ /home/debian/workspace/cuberite_PR-5503/src/StringCompression.cpp:246:4: error: moving a temporary object prevents copy elision [-Werror,-Wpessimizing-move] std::move (std::make_unique <std::vector <std::byte> > (Dynamic.get(), (Dynamic.get() + UncompressedSize))), ^ /home/debian/workspace/cuberite_PR-5503/src/StringCompression.cpp:246:4: note: remove std::move call here std::move (std::make_unique <std::vector <std::byte> > (Dynamic.get(), (Dynamic.get() + UncompressedSize))), ^~~~~~~~~~~ ~ 3 errors generated.
Missing line should now have been re-added. Let me know if there is anything else to refactor/clean up. Thanks. |
Changed the variable
Compresion::Result::Storage
of typestd::variant
to only useDynamic
for memory allocation in./src/StringCompression.h
.Changed some code that previously defaulted to assign data to the stack in
./src/StringCompression.cpp
.Initialized
BytesWrittenOut
after MSVC threw another warning:in
./src/StringCompression.cpp
.This PR should resolve issue #5495.