-
Notifications
You must be signed in to change notification settings - Fork 2.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
Rewrite check *bufStart condition #3304
Conversation
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 for the PR! I like your change related to *bufStart == NULL
, but for the other changes I prefer the original code. In many cases the compiler will take care of the optimizations you're doing manually.
If you revert those other changes, happy to merge!
lib/common/entropy_common.c
Outdated
@@ -200,7 +200,7 @@ BMI2_TARGET_ATTRIBUTE static size_t FSE_readNCount_body_bmi2( | |||
short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, | |||
const void* headerBuffer, size_t hbSize) | |||
{ | |||
return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); | |||
return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); |
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.
The original code here is actually correct :) BMI2_TARGET_ATTRIBUTE
tells the compiler to use BMI2 instructions when compiling this function. But the source code of the function doesn't change.
The reason you're seeing failures in CI is that you're defining FSE_readNCount_body_bmi2()
in terms of itself, i.e. you have an infinite recursion.
programs/util.c
Outdated
@@ -386,7 +386,7 @@ static size_t readLineFromFile(char* buf, size_t len, FILE* file) | |||
assert(!feof(file)); | |||
if ( fgets(buf, (int) len, file) == NULL ) return 0; | |||
{ size_t linelen = strlen(buf); | |||
if (strlen(buf)==0) return 0; | |||
if (buf[0]=='\0') return 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.
I think we can rely on the compiler to make this optimization, so I prefer the original code here since it's more readable.
programs/util.c
Outdated
*bufEnd = *bufStart + newListSize; | ||
if (*bufStart == NULL) { free(path); closedir(dir); return 0; } | ||
if (*bufStart != NULL) *bufEnd = *bufStart + newListSize; | ||
else { free(path); closedir(dir); return 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.
I think this is a useful change since performing arithmetic on a NULL pointer is UB. Can you please add curly braces around *bufEnd = *bufStart + newListSize;
?
programs/zstdcli.c
Outdated
(exeName[strlen(test)] == '\0' || exeName[strlen(test)] == '.'); | ||
size_t len = strlen(test); | ||
return !strncmp(exeName, test, len) && | ||
(exeName[len] == '\0' || exeName[len] == '.'); |
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.
The compiler will take care of this optimization, so I don't think we need to change this code.
programs/zstdcli.c
Outdated
@@ -1037,7 +1038,7 @@ int main(int argCount, const char* argv[]) | |||
if (longCommandWArg(&argument, "--size-hint")) { NEXT_TSIZE(srcSizeHint); continue; } | |||
if (longCommandWArg(&argument, "--output-dir-flat")) { | |||
NEXT_FIELD(outDirName); | |||
if (strlen(outDirName) == 0) { | |||
if (outDirName[0] == '\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.
Same as above :)
programs/zstdcli.c
Outdated
@@ -1053,7 +1054,7 @@ int main(int argCount, const char* argv[]) | |||
#ifdef UTIL_HAS_MIRRORFILELIST | |||
if (longCommandWArg(&argument, "--output-dir-mirror")) { | |||
NEXT_FIELD(outMirroredDirName); | |||
if (strlen(outMirroredDirName) == 0) { | |||
if (outMirroredDirName[0] == '\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.
Same as above :)
Here's a godbolt link showing that gcc won't actually call |
I followed link and made sure that there was no optimization, and readability code suffered. I will revert changes and leave only with NULL checking. |
Hi @GermanAizek! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Looks good, you need to fix two things:
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
@embg, everything is ready for PR merge. |
Thanks for the contribution! Will merge after CI passes. |
Rewriting logic
bufStart
pointer validation condition and micro-optimization withstrlen()
function. ForDYNAMIC_BMI2
macro, it was meant to use a function with the*_bmi2
postfix. After review PR code, feedback about it.