Page MenuHomeFreeBSD

cred: group_is_supplementary(): Use bsearch()
ClosedPublic

Authored by olce on Oct 4 2024, 8:07 AM.
Tags
None
Referenced Files
F106373103: D46907.diff
Sun, Dec 29, 2:17 PM
Unknown Object (File)
Tue, Dec 24, 9:09 AM
Unknown Object (File)
Sun, Dec 22, 5:16 PM
Unknown Object (File)
Wed, Dec 18, 7:26 PM
Unknown Object (File)
Mon, Dec 9, 1:00 AM
Unknown Object (File)
Nov 24 2024, 9:59 AM
Unknown Object (File)
Nov 21 2024, 6:36 AM
Unknown Object (File)
Nov 18 2024, 5:01 PM
Subscribers

Details

Summary

This makes that function use a more efficient version of binary search instead,
and removes one more hand-rolled binary search code from the tree (and the
kernel binary).

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

olce requested review of this revision.Oct 4 2024, 8:07 AM
sys/kern/kern_prot.c
842

somewhat of a bikeshed comment but an explicit "if (a < b) / else if (b < a) / else" is more immediately clear to me, and a quick check on compilerexplorer showed Clang generated identical code. That said, I'm fine either way and perhaps "(a > b) - (a < b)" is idiomatic.

sys/kern/kern_prot.c
842

Yes, (a > b) - (a < b) is the usual idiom for compare functions to avoid tests.

I've tested on Compiler Explorer with latest LLVM 17, 18, 19, and GCC 12, 13 and 14, with -O and -O2, with alternatives: a > b ? 1 : -(a < b) and a full chain of if testing first for a > b and then a < b. Switching to any alternative makes GCC produce test and jump instructions. clang is better, but always generates conditional moves, whereas with the idiom it even doesn't need to (and just uses SETA).

I agree with you it's less readable than just a test. Changing it makes a difference in the produced assembly, albeit probably undetectable in practice in the current context. I'm fine either way.

sys/kern/kern_prot.c
842

I did a brief survey of existing code in the tree and find several existing examples of both forms, so let's leave it as is.

LGTM

sys/kern/kern_prot.c
839–842

For me this is slightly preferable when trying to pick apart what the return calculation is doing.

Plus, one style nit (return braces).

This revision is now accepted and ready to land.Oct 7 2024, 7:09 PM

LGTM assuming @mhorne's comments are addressed

olce marked 3 inline comments as done.Oct 8 2024, 12:57 PM

Suggested modifications staged in my tree.

olce retitled this revision from cred: is_a_supplementary_group(): Use bsearch() to cred: group_is_supplementary(): Use bsearch().
olce edited the summary of this revision. (Show Details)

Rename to group_is_supplementary().

This revision now requires review to proceed.Oct 8 2024, 1:30 PM

Re-validate to make it easier to see where we stand on this series (@mhorne and @emaste validated, and the last comments were addressed).

This revision is now accepted and ready to land.Oct 28 2024, 10:33 AM
This revision was automatically updated to reflect the committed changes.