High Performance Python

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/13

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

14 Terms

1
New cards

Strengths of Python

  • Flexible and powerful in-built type system

    • Arbitrary data size (ints, strings, lists etc.)

    • Objects, modules, inheritance etc.

  • Fast to develop, can try scripts in REPL

  • Extensibility and wide range of libraries available

2
New cards

Downsides to Python

  • Type conversion based on context

  • Overhead as variables ‘change type’

  • Magic behind the scenes to support abstraction (arbitrary sized integers, arbitrary length strings…)

  • This costs space and time…

3
New cards

Trade-offs

  • The flexibility comes at a cost (performance)

  • Python source is translated to python bytecode (.py -> .pyc), pyc updated on first run

  • The bytecode is interpreted at runtime

4
New cards

Speeding things up

We will need to trade some of the flexibility to get more performance

  • Introduce fixed types so managing memory is leaner and more efficient

  • Get closer to the machine architecture

    • either use other program components written in a compiled language

    • Or, ‘compile’ our python in some way

5
New cards

Working with other Processes is Useful when

  1. using python to ‘script’ other things, e.g. automation of routine system tasks;

  2. running toolkits written in other languages (such as C)

6
New cards

Pros of Process Level Integration

  • verbose API designed for humans

  • simple to use

  • cross platform

  • very versatile

7
New cards

Cons of Process Level Integration

  • start-up cost of process

  • difficult to maintain state outside python script

  • low level of granularity

  • difficult to efficiently pass large amounts of data efficiently

8
New cards

Optimise for the common case of Python and C

  • Actively try to create programs that utilise multiple programming languages

  • Allow each language to excel at what it does best

  • Write user facing scripts in Python

  • Write the high-performance modules in a language that is more computationally efficient

  • Create Foreign Function Interfaces between them…

9
New cards

Foreign Function Interfaces

  • A function that executes code written in a different programming language

  • Provides finer grained integration of library code into applications than process-based approaches

  • C is the most common language to interface with, as it complements Python well

  • Typically implemented through dynamically linking C libraries into Python interpreter (Java and C# can do this too)

<ul><li><p>A function that executes code written in a different programming language</p></li><li><p>Provides finer grained integration of library code into applications than process-based approaches</p></li><li><p>C is the most common language to interface with, as it complements Python well</p></li><li><p>Typically implemented through dynamically linking C libraries into Python interpreter (Java and C# can do this too)</p></li></ul><p></p>
10
New cards

CPython Extensions

  • CPython extension modules are just shared object C libraries (.so files, or DLLs)

  • Placed in a well know search path, defined by sys.path…

  • The Python VM exposes a set of C library functions and types (classes/structs) that allow:

    • Python modules and functions to be created

    • Marshalling of types between C/Python

    • Initialization callbacks

<ul><li><p><span>CPython extension modules are just shared object C libraries (.so files, or DLLs)</span></p></li><li><p><span>Placed in a well know search path, defined by sys.path…</span></p></li><li><p><span>The Python VM exposes a set of C library functions and types (classes/structs) that allow:</span></p><ul><li><p><span>Python modules and functions to be created</span></p></li><li><p><span>Marshalling of types between C/Python</span></p></li><li><p><span>Initialization callbacks</span></p></li></ul></li></ul><p></p>
11
New cards

Example of CPython Extension

  • C code that makes up the functionality of the module

  • Wrappers methods for each underlying C function. Note standardized naming convention and parameter list

  • Initialization callback function, that registers a table of exposed functions to the Python VM

  • Explicit functions to manage type conversion between C and Python

<ul><li><p><span>C code that makes up the functionality of the module</span></p></li><li><p><span>Wrappers methods for each underlying C function. Note standardized naming convention and parameter list</span></p></li><li><p><span>Initialization callback function, that registers a table of exposed functions to the Python VM</span></p></li><li><p><span>Explicit functions to manage type conversion between C and Python</span></p></li></ul><p></p>
12
New cards

Using CPython Extensions

  • Compile as a dynamic library

  • Include Python VM library

  • Place resultant library in the Python search path

  • Use like any other Python module

13
New cards

Limitations of CPython

  • Powerful, but quite Complex to write

  • Developer needs to handle memory management (ref counts!)

  • Exception handling requirements need to be obeyed

  • Only works with CPython…

14
New cards

3 Higher Level Approaches to CPython

  • Using the ctypes library

  • The C Foreign Function Interface or CFFI

  • The Cython language