Skip to contentSkip to conversation

drivers/char/vt.c: remove unnecessary code

PATCH [email protected] 7 participants 10 messages Feb 28, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw
Quoted 6 lines · On Mon, Feb 28, 2005 at 01:57:59PM +0100, colbuse@xxxxxxxxxxxxxxx wrote:
Quoted 1 line

Please _don't_ apply this, but tell me what you think about it.

It's broken. 8)

Quoted 10 lines
Quoted 8 lines
drivers/char/vt.c 2005-02-28 12:53:57.933256631 +0100 renamed from drivers/char/vt.c 2004-12-24 22:35:25.000000000 +0100 +0 -0
@@ -1655,9 +1655,9 @@
 vc_state = ESnormal;
 return;

case ESsquare:
- for(npar = 0 ; npar < NPAR ; npar++)
+ for(npar = NPAR-1; npar < NPAR; npar--)

How many times do you want this for loop to run?

NPAR times :-). As I stated, npar is unsigned.

See :
$cat x.c
#include <stdio.h>
#define NPAR 5

int main(void){
        unsigned int i;
        int j=0;
        for(i=NPAR-1;i<NPAR;i--) j++;
        fprintf(stdout,"it has runned %d times :-)\n",j);
}

$gcc x.c -o x && ./x
it has runned 5 times :-)
$

--
Emmanuel Colbus
Club GNU/Linux
ENSIMAG - Departement telecoms

-------------------------------------------------
envoyé via Webmail/IMAG !

#1 lore raw
Quoted 1 line · On Mon, Feb 28, 2005 at 02:13:57PM +0100, [email protected] wrote:

NPAR times :-). As I stated, npar is unsigned.

I think that's disgusting then - it isn't obvious what's going on, which leads to mistakes.

For the sake of a micro-optimisation such as this, it's far more important that the code be readable and easily understandable.

Others may disagree.

Signature · 3 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core
#2 lore raw

Surlignage Russell King <[email protected]>:

Quoted 11 lines · On Mon, Feb 28, 2005 at 02:13:57PM +0100, [email protected] wrote:
Quoted 1 line

NPAR times :-). As I stated, npar is unsigned.

I think that's disgusting then - it isn't obvious what's going on, which leads to mistakes.

For the sake of a micro-optimisation such as this, it's far more important that the code be readable and easily understandable.

Others may disagree.

I agree :) . But, if we look to the code, we can notice that there is actually no reason for npar to be unsigned. What do you think of this version?

drivers/char/vt.c 2005-02-28 12:53:57.933256631 +0100 renamed from drivers/char/vt.c 2004-12-24 22:35:25.000000000 +0100 +2 -2
@@ -1655,9 +1655,9 @@
                        vc_state = ESnormal;
                return;
        case ESsquare:
-               for(npar = 0 ; npar < NPAR ; npar++)
+               for(npar = NPAR - 1; npar >= 0; npar--)
                        par[npar] = 0;
+               npar++;
-               npar = 0;
                vc_state = ESgetpars;
                if (c == '[') { /* Function key */
                        vc_state=ESfunckey;
include/linux/console_struct.h 2005-02-28 14:49:53.653616335 +0100 renamed from include/linux/console_struct.h 2004-12-24 22:33:48.000000000 +0100 +0 -1
@@ -44,7 +44,8 @@
        unsigned short  vc_video_erase_char;    /* Background erase character */
        /* VT terminal data */
        unsigned int    vc_state;               /* Escape sequence parser state */
-       unsigned int    vc_npar,vc_par[NPAR];   /* Parameters of current escape

sequence */
+ unsigned int vc_par[NPAR]; /* Parameters of current escape
sequence */
+ int vc_npar; /* Current position in vc_par */
struct tty_struct *vc_tty; /* TTY we are attached to */
/* mode flags */
unsigned int vc_charset : 1; /* Character set G0 / G1 */

--
Emmanuel Colbus
Club GNU/Linux
ENSIMAG - Departement telecoms

-------------------------------------------------
envoyé via Webmail/IMAG !

#3 lore raw

drivers/char/vt.c 2005-02-28 12:53:57.933256631 +0100 line 1659

Quoted 4 lines
drivers/char/vt.c 2005-02-28 12:53:57.933256631 +0100 renamed from drivers/char/vt.c 2004-12-24 22:35:25.000000000 +0100 +1 -1
-               for(npar = 0 ; npar < NPAR ; npar++)
+               for(npar = NPAR - 1; npar >= 0; npar--)
                        par[npar] = 0;

if you really want to clean this up.. why not use memset() instead ?

#4 lore raw
In reply to [email protected]

Hello!

Quoted 2 lines

I agree :) . But, if we look to the code, we can notice that there is actually no reason for npar to be unsigned. What do you think of this version?

What does it try to solve? Your version is in no way better than the previous one. The previous one was more readable and it's quite possible that both will be compiled to the same sequence of instructions.

Also, as Arjan noted, if you want to improve the code, use memset.

Have a nice fortnight

Signature · 3 lines
Martin `MJ' Mares   <[email protected]>   http://atrey.karlin.mff.cuni.cz/~mj/
Faculty of Math and Physics, Charles University, Prague, Czech Rep., Earth
American patent law: two monkeys, fourteen days.
#5 lore raw
In reply to [email protected]
Quoted 7 lines · [email protected] said:

Surlignage Russell King <[email protected]>:

Quoted 5 lines · On Mon, Feb 28, 2005 at 02:13:57PM +0100, [email protected] wrote:
Quoted 1 line

NPAR times :-). As I stated, npar is unsigned.

I think that's disgusting then - it isn't obvious what's going on, which leads to mistakes.

Quoted 6 lines
Quoted 4 lines

For the sake of a micro-optimisation such as this, it's far more important that the code be readable and easily understandable.

Others may disagree.

I agree :) .

I agree too.

Quoted 8 lines

But, if we look to the code, we can notice that there is
actually no reason for npar to be unsigned. What do you think of this version?

drivers/char/vt.c 2005-02-28 12:53:57.933256631 +0100 line 1660

Quoted 7 lines
drivers/char/vt.c 2005-02-28 12:53:57.933256631 +0100 renamed from drivers/char/vt.c 2004-12-24 22:35:25.000000000 +0100 +2 -1
@@ -1655,9 +1655,9 @@
                        vc_state = ESnormal;
                return;
        case ESsquare:
-               for(npar = 0 ; npar < NPAR ; npar++)
+               for(npar = NPAR - 1; npar >= 0; npar--)
                        par[npar] = 0;
+               npar++;

Completely unreadable. What do you think you might gain this way? I doesn't matter in which order the par[i] are set to zero, does it?

Please stick to plain, down-to-earth, colloquial C unless there is a /measurable/ advantage in not doing so, and it matters overall (i.e., the code in question runs many times a second). Readability first, traded in for efficiency only when it /really/ matters. Given today's gcc, you'll probably just confuse it into generating bad code by using cute tricks, as it has been tuned to the typical language usage. And gcc is a moving target, what might be a good idea today could be the worst possible thing to do in a year or so (or even today with a different architecture).

Signature · 4 lines
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513
#6 lore raw
In reply to [email protected]
Quoted 18 lines · On Mon, Feb 28, 2005 at 02:13:57PM +0100, [email protected] wrote:
Quoted 6 lines · On Mon, Feb 28, 2005 at 01:57:59PM +0100, colbuse@xxxxxxxxxxxxxxx wrote:
Quoted 1 line

Please _don't_ apply this, but tell me what you think about it.

It's broken. 8)

Quoted 10 lines
Quoted 8 lines
drivers/char/vt.c 2005-02-28 12:53:57.933256631 +0100 renamed from drivers/char/vt.c 2004-12-24 22:35:25.000000000 +0100 +0 -0
@@ -1655,9 +1655,9 @@
 vc_state = ESnormal;
 return;

case ESsquare:
- for(npar = 0 ; npar < NPAR ; npar++)
+ for(npar = NPAR-1; npar < NPAR; npar--)

How many times do you want this for loop to run?

Quoted 2 lines

NPAR times :-). As I stated, npar is unsigned.
...

The Linux kernel is not the obfuscated C contest.

Your code might be technically correct, but your patch changes a perfectly readable line into an unreadable line for no gain.

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#7 lore raw
In reply to [email protected]
Quoted 17 lines · On Mon, 28 Feb 2005 14:13:57 +0100, [email protected]…
Quoted 6 lines · On Mon, Feb 28, 2005 at 01:57:59PM +0100, colbuse@xxxxxxxxxxxxxxx wrote:
Quoted 1 line

Please _don't_ apply this, but tell me what you think about it.

It's broken. 8)

Quoted 10 lines
Quoted 8 lines
drivers/char/vt.c 2005-02-28 12:53:57.933256631 +0100 renamed from drivers/char/vt.c 2004-12-24 22:35:25.000000000 +0100 +0 -0
@@ -1655,9 +1655,9 @@
 vc_state = ESnormal;
 return;

case ESsquare:
- for(npar = 0 ; npar < NPAR ; npar++)
+ for(npar = NPAR-1; npar < NPAR; npar--)

How many times do you want this for loop to run?

Quoted 2 lines

NPAR times :-). As I stated, npar is unsigned.

for (npar = NPAR - 1; npar >= 0; npar--)

would be more readable and may be even faster on a dumb compiler than your variant. Still, I'd have compiler worry about such micro-optimizations.

Signature · 1 line
Dmitry
#8 lore raw

\

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

+ for(npar = NPAR-1; npar < NPAR; npar--)

How many times do you want this for loop to run?

Quoted 8 lines
Quoted 2 lines

NPAR times :-). As I stated, npar is unsigned.

for (npar = NPAR - 1; npar >= 0; npar--)

would be more readable and may be even faster on a dumb compiler than your variant. Still, I'd have compiler worry about such micro-optimizations.

actually that goes wrong for npar unsigned...

#9 lore raw
Quoted 8 lines · Arjan van de Ven

\

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

+ for(npar = NPAR-1; npar < NPAR; npar--)

How many times do you want this for loop to run?

NPAR times :-). As I stated, npar is unsigned.

Quoted 9 lines
Quoted 5 lines

for (npar = NPAR - 1; npar >= 0; npar--)

would be more readable and may be even faster on a dumb compiler than your variant. Still, I'd have compiler worry about such micro-optimizations.

actually that goes wrong for npar unsigned...

Oops, you are right...

Signature · 1 line
Dmitry