This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Modules/binascii.c: simplify expressions
Type: performance Stage: resolved
Components: Extension Modules Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: nikai, orsenthil, terry.reedy
Priority: normal Keywords: patch

Created on 2010-11-05 12:31 by nikai, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
python-binascii.diff nikai, 2010-11-05 12:37 patch
Messages (6)
msg120490 - (view) Author: Nicolas Kaiser (nikai) Date: 2010-11-05 12:31
Hi there!

I noticed two expressions that can be simplified like:
 (a || (!a && b)) => (a || b)

Best regards,
Nicolas Kaiser
---
--- a/Modules/binascii.c	2010-11-05 13:21:22.075303326 +0100
+++ b/Modules/binascii.c	2010-11-05 13:24:16.986174756 +0100
@@ -1337,8 +1337,7 @@ binascii_b2a_qp (PyObject *self, PyObjec
             ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
             ((data[in] < 33) &&
              (data[in] != '\r') && (data[in] != '\n') &&
-             (quotetabs ||
-            (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
+             (quotetabs || ((data[in] != '\t') && (data[in] != ' ')))))
         {
             if ((linelen + 3) >= MAXLINESIZE) {
                 linelen = 0;
@@ -1410,8 +1409,7 @@ binascii_b2a_qp (PyObject *self, PyObjec
             ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
             ((data[in] < 33) &&
              (data[in] != '\r') && (data[in] != '\n') &&
-             (quotetabs ||
-            (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
+             (quotetabs || ((data[in] != '\t') && (data[in] != ' ')))))
         {
             if ((linelen + 3 )>= MAXLINESIZE) {
                 odata[out++] = '=';
msg120551 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-11-05 22:57
As near as I can tell, since && and || are logical rather than bitwise, and since the variable reference 'quotetabs' has no side effect, you are correct. Have you run the unittest on a patched build?
msg120565 - (view) Author: Nicolas Kaiser (nikai) Date: 2010-11-06 00:30
That's ./Lib/test/test_unittest.py?

With patched builds of Python 2.6.5 and 3.1.2:

----------------------------------------------------------------------
Ran 126 tests in 0.015s

OK

----------------------------------------------------------------------
Ran 187 tests in 0.054s

OK
msg120573 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-11-06 02:56
test_binascii.py
msg120599 - (view) Author: Nicolas Kaiser (nikai) Date: 2010-11-06 09:22
Sorry, found it - with patched builds of Python 2.6.5 and 3.1.2:

python2.6 test_binascii.py
test_base64invalid (__main__.BinASCIITest) ... ok
test_base64valid (__main__.BinASCIITest) ... ok
test_crc32 (__main__.BinASCIITest) ... ok
test_empty_string (__main__.BinASCIITest) ... ok
test_exceptions (__main__.BinASCIITest) ... ok
test_functions (__main__.BinASCIITest) ... ok
test_hex (__main__.BinASCIITest) ... ok
test_qp (__main__.BinASCIITest) ... ok
test_uu (__main__.BinASCIITest) ... ok

----------------------------------------------------------------------
Ran 9 tests in 0.002s

OK

python3.1 test_binascii.py
test_base64invalid (__main__.BinASCIITest) ... ok
test_base64valid (__main__.BinASCIITest) ... ok
test_crc32 (__main__.BinASCIITest) ... ok
test_empty_string (__main__.BinASCIITest) ... ok
test_exceptions (__main__.BinASCIITest) ... ok
test_functions (__main__.BinASCIITest) ... ok
test_hex (__main__.BinASCIITest) ... ok
test_no_binary_strings (__main__.BinASCIITest) ... ok
test_qp (__main__.BinASCIITest) ... ok
test_uu (__main__.BinASCIITest) ... ok

----------------------------------------------------------------------
Ran 10 tests in 0.006s

OK
msg120853 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2010-11-09 10:02
At first, I was worried if this simplification would cause any harm to  readability of the algorithm. Fortunately, it didn't.
Committed in r86357.
History
Date User Action Args
2022-04-11 14:57:08adminsetgithub: 54533
2010-11-09 10:02:57orsenthilsetstatus: open -> closed

nosy: + orsenthil
messages: + msg120853

resolution: fixed
stage: commit review -> resolved
2010-11-06 09:22:19nikaisetmessages: + msg120599
2010-11-06 02:56:57terry.reedysetmessages: + msg120573
2010-11-06 00:30:35nikaisetmessages: + msg120565
2010-11-05 22:57:07terry.reedysetversions: + Python 3.2, - Python 3.3
nosy: + terry.reedy

messages: + msg120551

type: enhancement -> performance
stage: commit review
2010-11-05 12:37:09nikaisetfiles: + python-binascii.diff
keywords: + patch
2010-11-05 12:31:20nikaicreate