More consistent use of TREE_* macros in AVL comparators#18259
More consistent use of TREE_* macros in AVL comparators#18259robn wants to merge 1 commit intoopenzfs:masterfrom
TREE_* macros in AVL comparators#18259Conversation
|
IIRC the trick behind |
523d01e to
82a2479
Compare
|
Here's source-annotated disassembly for GCC 14.2, production builds (which is why I didn't check tl;dr: literally the same assembly.
|
|
Same for diff --git module/zfs/dsl_crypt.c module/zfs/dsl_crypt.c
index f519b937ed..9cb1536642 100644
--- module/zfs/dsl_crypt.c
+++ module/zfs/dsl_crypt.c
@@ -306,12 +301,7 @@ spa_key_mapping_compare(const void *a, const void *b)
{
const dsl_key_mapping_t *kma = a;
const dsl_key_mapping_t *kmb = b;
-
- if (kma->km_dsobj < kmb->km_dsobj)
- return (-1);
- if (kma->km_dsobj > kmb->km_dsobj)
- return (1);
- return (0);
+ return (TREE_CMP(kma->km_dsobj, kmb->km_dsobj));
}
static intThis one is much nicer: no branches at all.
|
With Clang 19.1.7 I do see only one branching with current code, which should always be true and predicted as such, though I haven't benchmarked it to see the difference: |
I see for |
behlendorf
left a comment
There was a problem hiding this comment.
Quite a nice bit of cleanup.
Where is it appropriate and obvious, use TREE_CMP(), TREE_ISIGN() and TREE_PCMP() instead or direct comparisons. It can make the code a lot smaller, less error prone, and easier to read. Sponsored-by: TrueNAS Signed-off-by: Rob Norris <rob.norris@truenas.com>
82a2479 to
707ae0d
Compare
|
Last push rebases to master, and changes that Again on GCC, While So, trading a second branch opportunity for a load, though for I haven't measured it at all, and its not quite the point of this PR, but I'm fine with from a consistency point of view. I've been pondering whether it would be good to be able to provide a different comparator for search vs insertion, but I'll need to play more to see if that makes any sense, certainly not for this PR. |




[Sponsors: TrueNAS]
Motivation and Context
Just some drive-by code uplift.
Description
Where is it appropriate and obvious, use
TREE_CMP(),TREE_ISIGN()andTREE_PCMP()instead of direct comparisons. It can make the code a lot smaller, less error prone, and easier to read.Commit should be fairly self-explanatory, however there are three functions that have at least caused me to raise an eyebrow:
vdev_queue_to_compare()has a curious construction at the end, where it will always consider two IOs with the same timestamp and offset to be the same if the first one is not queued. At least, I think that is the intent. The same construction is present inzfs_refcount_compare(), but there it considered two references with same var and size to be the same during a search. the difference is,vdev_queue_to_compare()uses a bitwise OR|, whilezfs_refcount_compare()uses a conditional OR||. I don't love this shorthand either way; I'd prefer it written out, but I think||is (marginally) more correct for the intent. I didn't want to just change anything, though, in case its load-bearing in ways I'm not seeing.spa_error_entry_compare()usesmemcmp()to compare two bookmarks, which isn't wrong, I guess, but compare tozio_bookmark_compare()or the bottom half ofrecent_events_compare(), where we spell it out. Those read a lot better, I think.zbookmark_compare()is not directly an AVL comparator, but is wrapped byscan_prefetch_queue_compare(),redact_node_compare_start()andredact_node_compare_end(), which are. It has a set of comparisons near the end that are begging to be converted toTREE_CMP(), however note this subtlety:The
zbNlevelcomparison at the end actually has opposite direction to the other two. I can't obviously tell if this is intentional or not.How Has This Been Tested?
ZTS run completed on Linux.
Types of changes
Checklist:
Signed-off-by.