10
How Python internals affect your code Martijn Pieters

Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

Embed Size (px)

Citation preview

Page 1: Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

How Python internals

affect your code

Martijn Pieters

Page 2: Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

Abstractions galore

Programming is all about abstractions.

All non-trivial abstractions, to some

degree, are leaky.Joel Spolsky’s Law of Leaky Abstractions

CPython translates your Python code into

machine instructions.

Page 3: Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

Small integers are singletons

● Everything is an object

● Integers are used all over the place

● Small integers are singletons, there is only

one copy of each small number

● -5 through to 256 are all cached like this

Page 4: Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

Small integers are singletons

Beginners confuse is with ==Identity is not the same as equality

Use is only for objects that you know to be

singletons, always, like None.

Page 5: Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

Some strings are interned

Interning: reusing a singleton copy on demand

● All identifiers are interned

● Many string literals are interned

Comparing pointers in C is so much faster than

comparing the contents

Everything is a dictionary -> lots of comparing

Page 6: Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

When to intern too

Python code can use the intern() function

Use this together with is identity testing

When:

● Large numbers of strings

● Lots of dictionary access or other equality

tests

Page 7: Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

Peephole optimisations

Peephole optimizer is part of the compiler

● Expressions are simplified

● Some mutable objects are replaced with

immutables

Page 8: Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

Python will precalculate expressions:

● Numeric calculationsten_days = 10 * 24 * 60 * 60

● Sequencesdefault_retval = (None,) * 5taunt = 'neener' * 3

Expressions are simplified

Page 9: Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

Mutables replaced by immutables

Membership tests against a literal:if foo in {'ham', 'spam', 'eggs'}

The set() becomes a frozenset()

Page 10: Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

Introspection and Dissasembly

Demo of object attributes and the dis module