Page MenuHomeFreeBSD

kernel qsort: use inlined min() implementation from libkern.h
AbandonedPublic

Authored by pfg on May 26 2017, 6:09 PM.
Tags
None
Referenced Files
Unknown Object (File)
Jan 21 2024, 12:09 PM
Unknown Object (File)
Dec 31 2023, 9:49 PM
Unknown Object (File)
Oct 7 2023, 3:42 AM
Unknown Object (File)
Aug 16 2023, 9:27 AM
Unknown Object (File)
Jun 26 2023, 12:13 PM
Unknown Object (File)
Jun 15 2023, 6:39 PM
Subscribers

Details

Reviewers
delphij
Summary

Looking at the recent changes in qsort, it looks reasonable to use the
inlined versions in libkern.

Test Plan

Passes tinderbox build. Should this be imin() on min()?

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 9509
Build 9958: arc lint + arc unit

Event Timeline

delphij requested changes to this revision.May 26 2017, 6:21 PM

What do we gain from the change?

Note that imin/imax is int, by the way, while the result is ptrdiff_t (abused size_t), and they are not always equivalent, so I think this change is technically wrong.

This revision now requires changes to proceed.May 26 2017, 6:21 PM

What do we gain from the change?

Performance.. if we do them a lot it may be noticeable.

Note that imin/imax is int, by the way, while the result is ptrdiff_t (abused size_t), and they are not always equivalent, so I think this change is technically wrong.

I though char* were supposed to by int's sometimes unsigned. I noticed a subtraction there though.
There is a bunch of functions of the same family: perhaps ummin().

In D10945#226591, @pfg wrote:

What do we gain from the change?

Performance.. if we do them a lot it may be noticeable.

Do you have data to support this claim? (e.g. I don't see why inline expansion is different from macro expansion in the resulting code).

Note that imin/imax is int, by the way, while the result is ptrdiff_t (abused size_t), and they are not always equivalent, so I think this change is technically wrong.

I though char* were supposed to by int's sometimes unsigned. I noticed a subtraction there though.
There is a bunch of functions of the same family: perhaps ummin().

In D10945#226591, @pfg wrote:

What do we gain from the change?

Performance.. if we do them a lot it may be noticeable.

Do you have data to support this claim? (e.g. I don't see why inline expansion is different from macro expansion in the resulting code).

Well, this was the idea when the functions were introduced (BSD44 I think): since the function already defined the types the comparisons can be optimized and the code will be inlined. However this really depends on the compiler, which may or may not inline it, and/or modern compilers may apply similar optimizations to the macro. So the claim is probably bogus if you try to measure it.

I don't know ... stack overflow and other forums carry such discussion extensively and are inconclusive.
FWIW, I recall Marcelo was working on a coccinelle script to find these type of replacements. Is this not an objective anymore?

After reading a bunch of pros and cons between inline vs. macros, I got to the conclusion that it doesn't really matter. at least not for this case, and not something I want to spend time benchmarking.

In D10945#226616, @pfg wrote:
In D10945#226591, @pfg wrote:

What do we gain from the change?

Performance.. if we do them a lot it may be noticeable.

Do you have data to support this claim? (e.g. I don't see why inline expansion is different from macro expansion in the resulting code).

Well, this was the idea when the functions were introduced (BSD44 I think): since the function already defined the types the comparisons can be optimized and the code will be inlined. However this really depends on the compiler, which may or may not inline it, and/or modern compilers may apply similar optimizations to the macro. So the claim is probably bogus if you try to measure it.

I don't know ... stack overflow and other forums carry such discussion extensively and are inconclusive.
FWIW, I recall Marcelo was working on a coccinelle script to find these type of replacements. Is this not an objective anymore?

I know you have abandoned this patch already, but I'd like to share my point of view.
Replace inline by macros has some benefits that is not only related with performance, of course each case need to be analysed, but one big benefit for me is 'code consistency' and also sometimes it makes easier to read the code as well.

So, performance is not the only and main point for these changes.

Best,

...

I know you have abandoned this patch already, but I'd like to share my point of view.
Replace inline by macros has some benefits that is not only related with performance, of course each case need to be analysed, but one big benefit for me is 'code consistency' and also sometimes it makes easier to read the code as well.

So, performance is not the only and main point for these changes.

OK, let's assume the performance is the same. Although I prefer the lowercase, syntactically using MIN() or ummin() is not huge difference.

I think the point is in the type: the macro doesn't care about the types but with an inline a compiler may do some extra checking. If we get the types right then using the inline function is worthy as a replacement. I don't know this code well enough to guess which type would be best (the subtraction may be reducing the range, or it may become negative?). Perhaps coccinelle may figure it out for us ;).