Skip to content

Micro-optimize zrl_remove(). #14200

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

Merged
merged 1 commit into from
Nov 29, 2022
Merged

Micro-optimize zrl_remove(). #14200

merged 1 commit into from
Nov 29, 2022

Conversation

amotin
Copy link
Member

@amotin amotin commented Nov 19, 2022

atomic_dec_32() should be a bit lighter than atomic_dec_32_nv().

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Performance enhancement (non-breaking change which improves efficiency)
  • Code cleanup (non-breaking change which makes code smaller or more readable)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Library ABI change (libzfs, libzfs_core, libnvpair, libuutil and libzfsbootenv)
  • Documentation (a change to man pages or other documentation)

Checklist:

@amotin amotin requested a review from behlendorf November 19, 2022 05:05
@amotin amotin added the Status: Code Review Needed Ready for review and testing label Nov 19, 2022
Copy link
Contributor

@ryao ryao left a comment

Choose a reason for hiding this comment

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

Is the micro-optimization here just reduced register pressure from the compiler not having to worry about a register being clobbered with the result, or is there some micro-architectural detail of how these operations work that make one more expensive than the other beyond the register pressure from one version clobbering a register?

For reference, in the FreeBSD source code on amd64, atomic_dec_32_nv() becomes atomic_fetchadd_int() and atomic_dec_32() becomes atomic_subtract_int(). The relevant lines are:

https://github.com/freebsd/freebsd-src/blob/424bf1d5ca5bdba0597b546ec3931eb05bfabcab/sys/amd64/include/atomic.h#L192-L203

https://github.com/freebsd/freebsd-src/blob/424bf1d5ca5bdba0597b546ec3931eb05bfabcab/sys/amd64/include/atomic.h#L370

https://github.com/freebsd/freebsd-src/blob/424bf1d5ca5bdba0597b546ec3931eb05bfabcab/sys/amd64/include/atomic.h#L117-L123

@amotin
Copy link
Member Author

amotin commented Nov 20, 2022

@ryao It was MOV + LOCK XADD, now it is just LOCK SUB. Minus register, extra instruction and extra micro-operation. Not much, but it is a primitive to be used by other code, so better be optimal.

@behlendorf
Copy link
Contributor

@amotin would you mind rebasing and force updating this PR. That should hopefully clear up the checkstyle failure at least.

@behlendorf behlendorf added Status: Accepted Ready to integrate (reviewed, tested) and removed Status: Code Review Needed Ready for review and testing labels Nov 28, 2022
atomic_dec_32() should be a bit lighter than atomic_dec_32_nv().

Signed-off-by:  Alexander Motin <[email protected]>
@amotin
Copy link
Member Author

amotin commented Nov 28, 2022

@behlendorf done.

@behlendorf behlendorf merged commit f0a76fb into openzfs:master Nov 29, 2022
andrewc12 pushed a commit to andrewc12/openzfs that referenced this pull request Dec 16, 2022
atomic_dec_32() should be a bit lighter than atomic_dec_32_nv().

Reviewed-by: Tino Reichardt <[email protected]>
Reviewed-by: Richard Yao <[email protected]>
Signed-off-by: Ryan Moeller <[email protected]>
Signed-off-by: Alexander Motin <[email protected]>
Closes openzfs#14200
andrewc12 pushed a commit to andrewc12/openzfs that referenced this pull request Dec 17, 2022
atomic_dec_32() should be a bit lighter than atomic_dec_32_nv().

Reviewed-by: Tino Reichardt <[email protected]>
Reviewed-by: Richard Yao <[email protected]>
Signed-off-by: Ryan Moeller <[email protected]>
Signed-off-by: Alexander Motin <[email protected]>
Closes openzfs#14200
amotin added a commit to amotin/zfs that referenced this pull request Mar 2, 2023
atomic_dec_32() should be a bit lighter than atomic_dec_32_nv().

Reviewed-by: Tino Reichardt <[email protected]>
Reviewed-by: Richard Yao <[email protected]>
Signed-off-by: Ryan Moeller <[email protected]>
Signed-off-by: Alexander Motin <[email protected]>
Closes openzfs#14200
behlendorf pushed a commit that referenced this pull request Mar 2, 2023
atomic_dec_32() should be a bit lighter than atomic_dec_32_nv().

Reviewed-by: Tino Reichardt <[email protected]>
Reviewed-by: Richard Yao <[email protected]>
Signed-off-by: Ryan Moeller <[email protected]>
Signed-off-by: Alexander Motin <[email protected]>
Closes #14200
@amotin amotin deleted the zrl_remove branch October 9, 2023 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Accepted Ready to integrate (reviewed, tested)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants