-
Notifications
You must be signed in to change notification settings - Fork 31.3k
zlib: reduce code duplication #57810
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: main
Are you sure you want to change the base?
Conversation
3bec46c
to
28bd8e7
Compare
The offset in the allocated memory was calculated in alloc and free, this makes it a single constant so it only needs to be defined once.
28bd8e7
to
1051f9e
Compare
@@ -607,9 +607,11 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { | |||
return AllocForBrotli(data, real_size); | |||
} | |||
|
|||
static constexpr size_t reserveSizeAndAlign = | |||
std::max(sizeof(size_t), alignof(max_align_t)); |
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.
Isn't this just always alignof(max_align_t)
anyway?
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, if you don't care about unaligned accesses you could set it to 1. There is no architecture actually doing that though. But this is just addressing a comment made by @Flarna.
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.
see #57727 (comment) as well, this just removes a double definition of a constant, there is nothing special about this.
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'm confused, you're saying "no" here but the comment you linked seems to unambiguously answer my question with "yes"? 🙂
Either way, I don't really care, but I'm not sure that this code is clearer because when reading it it really should raise a question of "why are we defining this constant as the maximum of two values where one is always clearly larger than the other"
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'm not aware of any platform where alignof(max_align_t)
is smaller then sizeof(size_t)
as most hardware prefers aligned data.
But to my understanding C++ standard allows alignof(max_align_t) == 1
but the space needed here is sizeof(size_t)
so take the max.
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.
@addaleax Just make a large struct or class and you can easily check that the alignment of it is less then its size. This code is simply doing the right thing, it will make sure there is enough space and it is still aligned. In theory alignof(max_align_t) can be less than sizeof(size_t). In practice it won't, but the compiler will optimize it out anyway.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #57810 +/- ##
==========================================
- Coverage 90.21% 90.20% -0.02%
==========================================
Files 630 630
Lines 185524 185522 -2
Branches 36387 36375 -12
==========================================
- Hits 167378 167357 -21
- Misses 11037 11044 +7
- Partials 7109 7121 +12
🚀 New features to boost your workflow:
|
@@ -607,9 +607,11 @@ class CompressionStream : public AsyncWrap, public ThreadPoolWork { | |||
return AllocForBrotli(data, real_size); | |||
} | |||
|
|||
static constexpr size_t reserveSizeAndAlign = |
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.
static constexpr size_t reserveSizeAndAlign = | |
static constexpr size_t kReserveSizeAndAlign = |
Not 100% sure but I think this doesn't fit the code style, it seems inconsistent already in the codebase
Quite some places use a k
prefix, others use define style (e.g. RESERVER_SIZE_AND_ALIGN
).
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 sounds like pure evil, are you suggestion to make it a #define in a c++ project?
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.
Sorry, I didn't mean to convert it to a define, constexpr
is perfectly fine and the way to go.
It's just about nitpicking about the name. Unfortunatelly cpp-style-guide doesn't clarify this.
Looking into code I found several places using the k
prefix (e.g. here), and some using ALL_UPPER_CASE
(e.g. here).
Personally I prefer the proposed k
prefix.
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.
Understood, I guess I can rename it to reserveSizeAndAlign_, although rather ugly, that seems to comply with the code standard / general implementations. I doubt I have ever seen a k though, unless in kilo. Why would you prefer a k there?
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 don't know where the k
prefix comes from but it is used at quite some places in this repo for constants. Therefore I see it as helpful as it jumps into my mind that this is a constant. Would I go for k
prefix in my personal repo: no - but thats a different topic.
The casing you use now is used nowhere in this repo for constants.
Aynhow, all I'm asking is to try to follow the existing style8s). I guess kReserveSizeAndAlign
, RESERVE_SIZE_AND_ALIGN
, reserver_size_and_align
(and maybe more) follow exisiting styles.
The offset in the allocated memory was calculated in alloc and free, this makes it a single constant so it only needs to be defined once.
As requested by @Flarna in #57727