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

Avoid branch by adding xfer_align to cf->buff #2036

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AtariDreams
Copy link
Contributor

@AtariDreams AtariDreams commented Dec 14, 2023

We can avoid a branch by not comparing with 0.

@jsonn
Copy link
Contributor

jsonn commented Dec 14, 2023

Are you sure?

@AtariDreams
Copy link
Contributor Author

Are you sure?

Yes

@kientzle
Copy link
Contributor

This is more efficient than comparing with 0.

Do you have any benchmark numbers to help support this claim? I'd be surprised if this produced a noticeable difference in overall libarchive performance. When I've done performance profiling in the past, the biggest performance bottlenecks were system calls.

@AtariDreams
Copy link
Contributor Author

This is more efficient than comparing with 0.

Do you have any benchmark numbers to help support this claim? I'd be surprised if this produced a noticeable difference in overall libarchive performance. When I've done performance profiling in the past, the biggest performance bottlenecks were system calls.

I applied your suggestion. Thanks! Though it may not have a noticeable effect on performance, I do think this code is better written and more straightforward to other programmers who may see this code.

@AtariDreams AtariDreams changed the title Avoid branches by adding xfer_align Avoid branch by adding xfer_align to cf->buff Feb 6, 2024
We can avoid a branch by not comparing with 0.

/*
* Set a read buffer pointer in the proper alignment of
* the current filesystem.
*/
cf->buff = cf->allocation_ptr + s;
cf->buff = cf->allocation_ptr + xfer_align - s;
Copy link
Contributor

Choose a reason for hiding this comment

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

It's possible to avoid the branch above if you assume xfer_align is a power of two. However, the change you've made here actually wastes xfer_align bytes in the case where the original allocation is already aligned (which should be the common case).

Copy link
Contributor

@kientzle kientzle Mar 24, 2024

Choose a reason for hiding this comment

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

Oh. I just realized that you can do this correctly, but you need an additional modulo operation:

cf->buff = cf->allocation_ptr + ((xfer_align - s) % xfer_align)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants