Skip to content

[CORE] Refactor XferCRC to make it branchless and to remove winsock dependency #1228

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Mauller
Copy link

@Mauller Mauller commented Jul 5, 2025

This PR refactors the XferCRC to make the code branchless and to remove the winsock dependency within it.

There may be minimal performance improvement from this as other factors will need changing in the data being passed to the CRC to further improve the performance.

The winsock dependency has been replaced with the endian compat library instead.

@Mauller Mauller self-assigned this Jul 5, 2025
@Mauller Mauller added Minor Severity: Minor < Major < Critical < Blocker Gen Relates to Generals ZH Relates to Zero Hour Refactor Edits the code with insignificant behavior changes, is never user facing labels Jul 5, 2025

UnsignedInt val = 0;
const unsigned char *c = (const unsigned char *)uintPtr;
for (i=0; i<leftover; i++)
Copy link

@Caball009 Caball009 Jul 5, 2025

Choose a reason for hiding this comment

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

Could try a switch statement to unroll the loop, though it'd have to be measured to see whether it's an improvement:

switch(leftover)
{
case 3:
	val += (c[2] << (2 * 8));
case 2:
	val += (c[1] << (1 * 8));
case 1:
	val += (c[0] << (0 * 8));
default:
	break;
}

Copy link
Author

Choose a reason for hiding this comment

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

I will check this out, but if it works that at least gets rid of the second loop.

@Caball009
Copy link

Caball009 commented Jul 5, 2025

Perhaps we could go for even fewer branches by using the data size as a template parameter. Most of the call sites use a size that's known at compile-time.

@xezon
Copy link

xezon commented Jul 5, 2025

Replacing the winsock call is good. Removing the hibit branch also makes sense, because the branch predictor likely does not work well there (50% chance). I would like to see a performance comparison when this works without any remaining logical mismatch.

@Mauller Mauller force-pushed the refactor-xfercrc branch from fa239a4 to 9949da4 Compare July 6, 2025 09:14
@Mauller
Copy link
Author

Mauller commented Jul 6, 2025

Fixed the logic on validity handling of the leftover bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Gen Relates to Generals Minor Severity: Minor < Major < Critical < Blocker Refactor Edits the code with insignificant behavior changes, is never user facing ZH Relates to Zero Hour
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants