Skip to content
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

Merged
merged 1 commit into from
Nov 1, 2022
Merged

Rewrite check *bufStart condition #3304

merged 1 commit into from
Nov 1, 2022

Conversation

GermanAizek
Copy link
Contributor

Rewriting logic bufStart pointer validation condition and micro-optimization with strlen() function. For DYNAMIC_BMI2 macro, it was meant to use a function with the *_bmi2 postfix. After review PR code, feedback about it.

Copy link
Contributor

@embg embg left a 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!

@@ -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);
Copy link
Contributor

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;
Copy link
Contributor

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; }
Copy link
Contributor

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;?

(exeName[strlen(test)] == '\0' || exeName[strlen(test)] == '.');
size_t len = strlen(test);
return !strncmp(exeName, test, len) &&
(exeName[len] == '\0' || exeName[len] == '.');
Copy link
Contributor

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.

@@ -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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above :)

@@ -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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above :)

@embg
Copy link
Contributor

embg commented Oct 31, 2022

Here's a godbolt link showing that gcc won't actually call strlen for strlen(str) > 0: https://godbolt.org/z/zerrbcorr

@GermanAizek
Copy link
Contributor Author

Here's a godbolt link showing that gcc won't actually call strlen for strlen(str) > 0: https://godbolt.org/z/zerrbcorr

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.

@facebook-github-bot
Copy link

Hi @GermanAizek!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@GermanAizek
Copy link
Contributor Author

@embg, check this commit 1b7f3c4

@embg
Copy link
Contributor

embg commented Oct 31, 2022

Looks good, you need to fix two things:

if (*bufStart != NULL) {
    *bufEnd = *bufStart + newListSize;
} else {
    free(path); closedir(dir); return 0;
}

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@GermanAizek
Copy link
Contributor Author

@embg, everything is ready for PR merge.

@GermanAizek GermanAizek changed the title Rewrite check *bufStart, optimize strlen() and fixed FSE_readNCount_body_bmi2() for DYNAMIC_BMI2 Rewrite check *bufStart condition Oct 31, 2022
@embg
Copy link
Contributor

embg commented Nov 1, 2022

Thanks for the contribution! Will merge after CI passes.

@embg embg merged commit bb23f7b into facebook:dev Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants