-
Notifications
You must be signed in to change notification settings - Fork 573
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
Switch to uint8_t, some optimizations #979
Conversation
* Use latest Julia version to represent current performance (#972) * Use latest Julia version to represent current performance * Use latest stable 1.X version * Add timeout to benchmark solution runs (#977) * Added formatting and removed deprecated attributes syntax (#982) Co-authored-by: Tudor Marghidanu <[email protected]> --------- Co-authored-by: Christian Guinard <[email protected]> Co-authored-by: Tudor Marghidanu <[email protected]> Co-authored-by: Tudor Marghidanu <[email protected]>
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.
I always like improvements to one of the original solutions! :)
I have two comments, the second one (array initialization) is the one I think actually matters.
PrimeCPP/solution_2/PrimeCPP_PAR.cpp
Outdated
@@ -24,80 +24,66 @@ using namespace std::chrono; | |||
const uint64_t DEFAULT_UPPER_LIMIT = 10'000'000LLU; | |||
|
|||
class BitArray { | |||
uint32_t *array; | |||
uint8_t *array; | |||
size_t arrSize; |
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.
Is it necessary to declare arrSize
as a member variable? It's private so only visible to the BitArray
member functions, and I don't think it's used anywhere outside the constructor.
PrimeCPP/solution_2/PrimeCPP_PAR.cpp
Outdated
// Bits are left at zero default, so no need to initialize them | ||
// std::memset(array, 0x00, arraySize(arrSize)); |
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.
Are you absolutely sure? When I was learning C++, it was hammered into me by my teacher that with C and C++, one has to assume garbage when allocating memory via new
.
I actually looked up the documentation in the C++ reference, and this is what it says about default initialization, which is what's done when new T
is used ("if T is an array type, every element of the array is default-initialized", as per Default-initialization):
When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value.
If no initialization is performed for an object, that object retains an indeterminate value until that value is replaced.
It may work by accident in your office, but it looks to me we can't rely on that happening elsewhere. :)
Fixed!
… On Oct 20, 2024, at 12:22 PM, Rutger van Bergen ***@***.***> wrote:
@rbergen commented on this pull request.
I always like improvements to one of the original solutions! :)
I have two comments, the second one (array initialization) is the one I think actually matters.
In PrimeCPP/solution_2/PrimeCPP_PAR.cpp <#979 (comment)>:
> @@ -24,80 +24,66 @@ using namespace std::chrono;
const uint64_t DEFAULT_UPPER_LIMIT = 10'000'000LLU;
class BitArray {
- uint32_t *array;
+ uint8_t *array;
size_t arrSize;
Is it necessary to declare arrSize as a member variable? It's private so only visible to the BitArray member functions, and I don't think it's used anywhere outside the constructor.
In PrimeCPP/solution_2/PrimeCPP_PAR.cpp <#979 (comment)>:
> + // Bits are left at zero default, so no need to initialize them
+ // std::memset(array, 0x00, arraySize(arrSize));
Are you absolutely sure? When I was learning C++, it was hammered into me by my teacher that with C and C++, one has to assume garbage when allocating memory via new.
I actually looked up the documentation in the C++ reference, and this is what it says about default initialization, which is what's done when new T is used ("if T is an array type, every element of the array is default-initialized", as per Default-initialization <https://en.cppreference.com/w/cpp/language/default_initialization>):
When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value.
If no initialization is performed for an object, that object retains an indeterminate value until that value is replaced.
It may work by accident in your office, but it looks to me we can't rely on that happening elsewhere. :)
—
Reply to this email directly, view it on GitHub <#979 (review)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AA4HCF4HHMVWOYHM4TLP3NDZ4P7GNAVCNFSM6AAAAABQHWDEHGVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDGOBQGI2TIOJZGU>.
You are receiving this because you authored the thread.
|
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.
Thanks, LGTM!
Description
Replace this text with your description.
Contributing requirements
drag-race
as the target branch.