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: SUGGESTION: Optimize code in PYO
Type: enhancement Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Devyn Johnson, Rosuav, abarry, r.david.murray
Priority: normal Keywords:

Created on 2015-12-18 12:18 by Devyn Johnson, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg256676 - (view) Author: Devyn Johnson (Devyn Johnson) Date: 2015-12-18 12:18
I have a suggestion. When Python code is byte-compiled to *.pyo files, what if byte-compiler were to be made to enhance some of the code. For instance, "if type(OBJECT) == int:" would be changed to "if isinstance(OBJECT, int):". Python is used in numerous software, so why not add a feature like this that could increase performance?
msg256685 - (view) Author: Chris Angelico (Rosuav) * Date: 2015-12-18 14:56
The trouble with that example is that the semantics aren't the same. The isinstance check will also be true for subclasses of int (for instance, isinstance(True,int) is True), but the equality check will catch only exact matches. And that's even before considering that anything can be overridden - the name 'type' or 'int' might have been rebound, or the type object might have an __eq__ method, etc, etc.

If you want to look into CPython optimization, hang out on the python-dev mailing list (and read its recent archives); there's a current project called "FAT Python" which does a lot of changes of this nature, and it has to cope with certain questions (like "what if this has been rebound?"). This isn't a simple problem, but it's one people are looking into.
msg256686 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2015-12-18 15:01
*.pyo files have been removed from the language as of 3.5; instead, the optimization is done directly to the *.pyc files. Are you suggesting re-introducing *.pyo files or changing this behaviour in the current model?

Also, 'if type(obj) is int' is *not* the same as 'if isinstance(obj, int)' -- the first one checks explicitely that 'obj' is an int. The second allows for any arbitrary subclass of int; most people using the first idiom want ints specifically, disallowing subclasses.

What other optimizations do you have in mind?
msg256692 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-12-18 16:34
pyo files weren't removed, they were just renamed, and -O differentiated from -OO.  The provides the opportunity to introduce additional "optimized byte code" versions that do other optimizations, which several people are working on.  There are also other aspects of python optimization, and again a number of people are working on related projects.  So, find one that interests you and chip in :)
History
Date User Action Args
2022-04-11 14:58:25adminsetgithub: 70091
2015-12-18 16:34:04r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg256692

resolution: not a bug
stage: resolved
2015-12-18 15:01:15abarrysetnosy: + abarry
messages: + msg256686
2015-12-18 14:56:44Rosuavsetnosy: + Rosuav
messages: + msg256685
2015-12-18 12:18:30Devyn Johnsoncreate