staging: comedi: comedi_fops: Optimize code with logical short-circuiting
A message that is not in the archive (sent off-list or not yet received): 20160916200129.GA7265@namrata-HP-Pavilion-g6-Notebook-PC
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 30 lines · Greg Kroah-Hartman
Quoted 15 lines · Namrata A Shettar
Optimize code using logical short-circuiting. Two 'if' statements with same body is same as a single 'if' statement with same body and condition being union of prior two.
Signed-off-by Namrata A Shettar <[email protected]>--- drivers/staging/comedi/comedi_fops.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)drivers/staging/comedi/comedi_fops.c +0
-0@@ -1452,13 +1452,8 @@ static int do_insnlist_ioctl(struct comedi_device *dev,return -EFAULT ;drivers/staging/comedi/comedi_fops.c static int do_insnlist_ioctl(struct comedi_device *dev, · line 1456
Quoted 10 lines
drivers/staging/comedi/comedi_fops.c +1
-6data =kmalloc_array (MAX_SAMPLES ,sizeof (unsigned int ),GFP_KERNEL ); -if (!data) { - ret = -ENOMEM ; - goto error; - } - insns =kcalloc (insnlist.n_insns,sizeof (*insns),GFP_KERNEL ); -if (!insns) { +if (!data || !insns) {That's a lot harder to read and maintain, don't you think? The original code was just fine.
Quoted 3 lines
sorry,
greg k-h
I agree that it is harder to read and maintain but I went by the argument that in doing so the 'if' condition need not be evaluated twice
, if the first statement (!data) is true then the body is evaluated and need not check for (!insns). Also, reduces a few lines of code (the body is written once as compared to being written twice).
Thanks for the input!
-Namrata
Quoted 31 lines · Namrata A Shettar
Quoted 30 lines · Greg Kroah-Hartman
Quoted 15 lines · Namrata A Shettar
Optimize code using logical short-circuiting. Two 'if' statements with same body is same as a single 'if' statement with same body and condition being union of prior two.
Signed-off-by Namrata A Shettar <[email protected]>--- drivers/staging/comedi/comedi_fops.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)drivers/staging/comedi/comedi_fops.c +0
-0@@ -1452,13 +1452,8 @@ static int do_insnlist_ioctl(struct comedi_device *dev,return -EFAULT ;drivers/staging/comedi/comedi_fops.c static int do_insnlist_ioctl(struct comedi_device *dev, · line 1456
Quoted 10 lines
drivers/staging/comedi/comedi_fops.c +1
-6data =kmalloc_array (MAX_SAMPLES ,sizeof (unsigned int ),GFP_KERNEL ); -if (!data) { - ret = -ENOMEM ; - goto error; - } - insns =kcalloc (insnlist.n_insns,sizeof (*insns),GFP_KERNEL ); -if (!insns) { +if (!data || !insns) {That's a lot harder to read and maintain, don't you think? The original code was just fine.
Quoted 6 lines
Quoted 3 lines
sorry,
greg k-h
I agree that it is harder to read and maintain but I went by the argument that in doing so the 'if' condition need not be evaluated twice
, if the first statement (!data) is true then the body is evaluated and need not check for (!insns). Also, reduces a few lines of code (the body is written once as compared to being written twice).
The kind of code you produced does exist in the kernel. But one could wonder why you bother calling kcalloc if you are just going to free its result. That doesn't seem very efficient. Also, if the first one fails, then it will probably be because of insufficient memory, in which case the second one will probably fail too. I guess that a mostly redundant crash dump will be produced.
It's OK to make the change when the tests are right next to each other, but not when there is a side-effecting operation between them.
julia
Quoted 11 lines
Thanks for the input!
-Namrata--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20160916203912.GB7471%40namrata-HP-Pavilion-g6-Notebook-PC.
For more options, visit https://groups.google.com/d/optout.
Quoted 49 lines · Julia Lawall
Quoted 31 lines · Namrata A Shettar
Quoted 30 lines · Greg Kroah-Hartman
Quoted 15 lines · Namrata A Shettar
Optimize code using logical short-circuiting. Two 'if' statements with same body is same as a single 'if' statement with same body and condition being union of prior two.
Signed-off-by Namrata A Shettar <[email protected]>--- drivers/staging/comedi/comedi_fops.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)drivers/staging/comedi/comedi_fops.c +0
-0@@ -1452,13 +1452,8 @@ static int do_insnlist_ioctl(struct comedi_device *dev,return -EFAULT ;drivers/staging/comedi/comedi_fops.c static int do_insnlist_ioctl(struct comedi_device *dev, · line 1456
Quoted 10 lines
drivers/staging/comedi/comedi_fops.c +1
-6data =kmalloc_array (MAX_SAMPLES ,sizeof (unsigned int ),GFP_KERNEL ); -if (!data) { - ret = -ENOMEM ; - goto error; - } - insns =kcalloc (insnlist.n_insns,sizeof (*insns),GFP_KERNEL ); -if (!insns) { +if (!data || !insns) {That's a lot harder to read and maintain, don't you think? The original code was just fine.
Quoted 6 lines
Quoted 3 lines
sorry,
greg k-h
I agree that it is harder to read and maintain but I went by the argument that in doing so the 'if' condition need not be evaluated twice
, if the first statement (!data) is true then the body is evaluated and need not check for (!insns). Also, reduces a few lines of code (the body is written once as compared to being written twice).The kind of code you produced does exist in the kernel. But one could wonder why you bother calling kcalloc if you are just going to free its result. That doesn't seem very efficient. Also, if the first one fails, then it will probably be because of insufficient memory, in which case the second one will probably fail too. I guess that a mostly redundant crash dump will be produced.
Quoted 6 lines
It's OK to make the change when the tests are right next to each other, but not when there is a side-effecting operation between them.
julia
Thanks for breaking it down for me! seems more clear now. I will keep that in mind the next time I think of making such a change. However in a
similar patch ( [PATCH] staging: comedi: Optimize code using logical short-circuiting ) the tests are one after the other with no side-effecting operation between them, so the change would have been OK there I suppose? but is it trivial and hence the change doesn't make much of a difference?
Thanks,
Namrata
Quoted 12 lines
Quoted 12 lines
Thanks for the input!
-Namrata--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20160916203912.GB7471%40namrata-HP-Pavilion-g6-Notebook-PC.
For more options, visit https://groups.google.com/d/optout.
Quoted 50 lines · Namrata A Shettar
Quoted 49 lines · Julia Lawall
Quoted 31 lines · Namrata A Shettar
Quoted 30 lines · Greg Kroah-Hartman
Quoted 15 lines · Namrata A Shettar
Optimize code using logical short-circuiting. Two 'if' statements with same body is same as a single 'if' statement with same body and condition being union of prior two.
Signed-off-by Namrata A Shettar <[email protected]>--- drivers/staging/comedi/comedi_fops.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)drivers/staging/comedi/comedi_fops.c +0
-0@@ -1452,13 +1452,8 @@ static int do_insnlist_ioctl(struct comedi_device *dev,return -EFAULT ;drivers/staging/comedi/comedi_fops.c static int do_insnlist_ioctl(struct comedi_device *dev, · line 1456
Quoted 10 lines
drivers/staging/comedi/comedi_fops.c +1
-6data =kmalloc_array (MAX_SAMPLES ,sizeof (unsigned int ),GFP_KERNEL ); -if (!data) { - ret = -ENOMEM ; - goto error; - } - insns =kcalloc (insnlist.n_insns,sizeof (*insns),GFP_KERNEL ); -if (!insns) { +if (!data || !insns) {That's a lot harder to read and maintain, don't you think? The original code was just fine.
Quoted 6 lines
Quoted 3 lines
sorry,
greg k-h
I agree that it is harder to read and maintain but I went by the argument that in doing so the 'if' condition need not be evaluated twice
, if the first statement (!data) is true then the body is evaluated and need not check for (!insns). Also, reduces a few lines of code (the body is written once as compared to being written twice).The kind of code you produced does exist in the kernel. But one could wonder why you bother calling kcalloc if you are just going to free its result. That doesn't seem very efficient. Also, if the first one fails, then it will probably be because of insufficient memory, in which case the second one will probably fail too. I guess that a mostly redundant crash dump will be produced.
Quoted 8 lines
Quoted 6 lines
It's OK to make the change when the tests are right next to each other, but not when there is a side-effecting operation between them.
julia
Thanks for breaking it down for me! seems more clear now. I will keep that in mind the next time I think of making such a change. However in a
similar patch ( [PATCH] staging: comedi: Optimize code using logical short-circuiting ) the tests are one after the other with no side-effecting operation between them, so the change would have been OK there I suppose? but is it trivial and hence the change doesn't make much of a difference?
In that one, the change is correct (ie it doesn't change the behavior). I'm not sure whether it is an improvement. The condition becomes a bit complex. It seems like a matter of opinion.
julia