Skip to contentSkip to conversation

A need for a "yesno"-function?

RFC Richard Knutsson 4 participants 8 messages Mar 15, 2007 – Mar 17, 2007 Kernel janitors

lore
Share & tools
lore
#0 lore raw

Hi

Was just checking up the 'sparse' when I saw something like "abc"[value] and thought: what about the (statement) ? "yes" : "no" I have seen in the kernel.

Ran:
grep -Enr "\?.*y.*\:.*n" *
and to my surprise, it was not so false-positive-prone and there are many who does it. (Piping it to "grep yes" resulted in 153 hits) So I thought, if we could standardize this and eliminate some jmp-commands while doing it (the compiler should make the functions below inline), it might be interesting.

char yesno_chr(const bool value)

{
	return "ny"[value];
}

char *yesno_str(const bool value)

{
	return &"no\0yes"[3 * value];
}

(there may be better names for them)
I believe this should be slightly faster. I wrote two programs (one for each approach) and used 'time' while running them, and in a loop of 1000,000 I found the above to be slightly faster, but the variations between runs were larger.
So maybe it is as well to write: return value ? "yes" : "no"; but i think there is a need for this kind of functions at least.

Thoughts?

Richard Knutsson

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#1 lore raw
Quoted 33 lines · Richard Knutsson

Hi

Was just checking up the 'sparse' when I saw something like "abc"[value] and thought: what about the (statement) ? "yes" : "no" I have seen in the kernel.

Ran:
grep -Enr "\?.*y.*\:.*n" *
and to my surprise, it was not so false-positive-prone and there are many who does it. (Piping it to "grep yes" resulted in 153 hits) So I thought, if we could standardize this and eliminate some jmp-commands while doing it (the compiler should make the functions below inline), it might be interesting.

char yesno_chr(const bool value)

{
        return "ny"[value];
}

char *yesno_str(const bool value)

{
        return &"no\0yes"[3 * value];
}

(there may be better names for them)
I believe this should be slightly faster. I wrote two programs (one for each approach) and used 'time' while running them, and in a loop of 1000,000 I found the above to be slightly faster, but the variations between runs were larger.
So maybe it is as well to write: return value ? "yes" : "no"; but i think there is a need for this kind of functions at least.

Quoted 1 line

Thoughts?

The function makes sense, how you implement it? Something simple as this is hardly on a fast path 8)

- Arnaldo
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#2 lore raw

[RFC] A need for "yesno"-function? (and "cleanup" of kernel.h) (was: Re: [KJ] [RFC] A need for a "yesno"-function?)

Added LKML to the Cc: to see if there is someone there who also have any comments...

Quoted 34 lines · Arnaldo Carvalho de Melo
Quoted 33 lines · Richard Knutsson

Hi

Was just checking up the 'sparse' when I saw something like "abc"[value] and thought: what about the (statement) ? "yes" : "no" I have seen in the kernel.

Ran:
grep -Enr "\?.*y.*\:.*n" *
and to my surprise, it was not so false-positive-prone and there are many who does it. (Piping it to "grep yes" resulted in 153 hits) So I thought, if we could standardize this and eliminate some jmp-commands while doing it (the compiler should make the functions below inline), it might be interesting.

char yesno_chr(const bool value)

{
        return "ny"[value];
}

char *yesno_str(const bool value)

{
        return &"no\0yes"[3 * value];
}

(there may be better names for them)
I believe this should be slightly faster. I wrote two programs (one for each approach) and used 'time' while running them, and in a loop of 1000,000 I found the above to be slightly faster, but the variations between runs were larger.
So maybe it is as well to write: return value ? "yes" : "no"; but i think there is a need for this kind of functions at least.

Quoted 4 lines
Quoted 1 line

Thoughts?

The function makes sense, how you implement it? Something simple as this is hardly on a fast path 8)

True, but every cycled wasted... ;)
But a (condition) ? "yes" : "no" is preferable here, I guess (as it is more readable). The big problem is, where to put it? Seems wrong to put in <linux/string.h> since it appear to be a replica of userspace's <string.h> (otherwise, why put mem*-functions in there?).
A new file? What would it be then, "string_generic.h" maybe, and perhaps put in the pr_(info/debug), KERN_(WARN/...) and v?printk() (well, all those functions) in there. This means it will have to be included by <linux/kernel.h> but I think it would be a good thing to split out a few functions from it.

Just putting some ideas out there, any suggestions?

Richard Knutsson

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#3 lore raw

Re: [RFC] A need for "yesno"-function? (and "cleanup" of kernel.h) (was: Re: [KJ] [RFC] A need for a "yesno"-function?)

On Fri, 2007-03-16 at 16:24 +0100, Richard Knutsson wrote:
[...]

Quoted 3 lines

more readable). The big problem is, where to put it? Seems wrong to put in <linux/string.h> since it appear to be a replica of userspace's <string.h> (otherwise, why put mem*-functions in there?).

memcpy(3) and memcmp(3) are also there in user-space.

Bernd

Signature · 8 lines
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
#4 lore raw

Re: [RFC] A need for "yesno"-function? (and "cleanup" of kernel.h) (was: Re: [KJ] [RFC] A need for a "yesno"-function?)

Quoted 10 lines · Bernd Petrovitsch

On Fri, 2007-03-16 at 16:24 +0100, Richard Knutsson wrote:
[...]

Quoted 4 lines

more readable). The big problem is, where to put it? Seems wrong to put in <linux/string.h> since it appear to be a replica of userspace's <string.h> (otherwise, why put mem*-functions in there?).

memcpy(3) and memcmp(3) are also there in user-space.

Did I miss something or did you just restate what was stated? (If it was not a replica, I think the mem*-functions would be better placed in memory.h, or such)

Richard Knutsson

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors

#5 lore raw

Re: [RFC] A need for "yesno"-function? (and "cleanup" of kernel.h)

Quoted 9 lines · Richard Knutsson
Quoted 8 lines · Bernd Petrovitsch

On Fri, 2007-03-16 at 16:24 +0100, Richard Knutsson wrote:
[...]

Quoted 4 lines

more readable). The big problem is, where to put it? Seems wrong to put in <linux/string.h> since it appear to be a replica of userspace's <string.h> (otherwise, why put mem*-functions in there?).

Quoted 5 lines
Quoted 2 lines

memcpy(3) and memcmp(3) are also there in user-space.

Did I miss something or did you just restate what was stated? (If it was not a replica, I think the mem*-functions would be better placed in memory.h, or such)

Ah, I misunderstood it as if you wonder why mem*() functions in <string.h>.
Sorry.

Bernd

Signature · 8 lines
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
#6 lore raw

Re: [RFC] A need for "yesno"-function? (and "cleanup" of kernel.h) (was: Re: [KJ] [RFC] A need for a "yesno"-function?)

In reply to Richard Knutsson
Quoted 10 lines · Richard Knutsson
Quoted 10 lines
Quoted 10 lines

char yesno_chr(const bool value)

{
       return "ny"[value];
}

char *yesno_str(const bool value)

{
       return &"no\0yes"[3 * value];
}

static/extern const char *const yesno[] = {"no", "yes"}; static inline const char *yesno_str(bool value)

{
	return yesno[value];
}

#or
#define yesno_str(value) yesno[!!(value)]

Quoted 1 line
Quoted 1 line
Quoted 1 line

Thoughts?

Jan

Signature · 4 lines
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors
#7 lore raw

Re: [RFC] A need for "yesno"-function? (and "cleanup" of kernel.h)

Quoted 7 lines · Jan Engelhardt
Quoted 5 lines · Richard Knutsson
Quoted 5 lines
Quoted 5 lines

char yesno_chr(const bool value)

{
       return "ny"[value];
}
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

char *yesno_str(const bool value)

{
       return &"no\0yes"[3 * value];
}

static/extern const char *const yesno[] = {"no", "yes"}; static inline const char *yesno_str(bool value)

Should we use "inline"? Isn't it better to leave that to the compiler? Why the "const"?

Quoted 4 lines
{
	return yesno[value];
}

That's better :)
But I think a simple

static char *yesno_str(bool value)

{
	return value ? "yes" : "no";
}

is to prefer, don't you? It is simpler and we don't need to deal with an unnecessary array (unless it may be used by itself, that is. Then I would go for your implementation).

Quoted 3 lines

#or
#define yesno_str(value) yesno[!!(value)]

Why not "(bool)value" instead? We cast all the other times we want a something to be of a different kind.

Any thoughts where to put a function like this?

Richard Knutsson

_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors