Stories
Slash Boxes
Comments

SoylentNews is people

posted by hubie on Wednesday May 03, @11:52AM   Printer-friendly

Improvements to future versions of Python are set to speed it up, slim it down, and pave the way toward even better things:

Because Python is a dynamic language, making it faster has been a challenge. But over the last couple of years, developers in the core Python team have focused on various ways to do it.

At PyCon 2023, held in Salt Lake City, Utah, several talks highlighted Python's future as a faster and more efficient language. Python 3.12 will showcase many of those improvements. Some are new in that latest version, others are already in Python but have been further refined.

Mark Shannon, a longtime core Python contributor now at Microsoft, summarized many of the initiatives to speed up and streamline Python. Most of the work he described in his presentation centered on reducing Python's memory use, making the interpreter faster, and optimizing the compiler to yield more efficient code.

Other projects, still under wraps but already showing promise, offer ways to expand Python's concurrency model. This will allow Python to better use multiple cores with fewer of the tradeoffs imposed by threads, async, or multiprocessing.

[...] One long-dreamed way to solve this problem is to remove Python's GIL, or Global Interpreter Lock. The GIL synchronizes operations between threads to ensure objects are accessed by only one thread at a time. In theory, removing the GIL would allow true multithreading. In practice—and it's been tried many times—it slows down non-threaded use cases, so it's not a net win.

Core python developer Eric Snow, in his talk, unveiled a possible future solution for all this: subinterpreters, and a per-interpreter GIL. In short: the GIL wouldn't be removed, just sidestepped.

Subinterpreters is a mechanism where the Python runtime can have multiple interpreters running together inside a single process, as opposed to each interpreter being isolated in its own process (the current multiprocessing mechanism). Each subinterpreter gets its own GIL, but all subinterpreters can share state more readily.

[...] With Python 3.12, Snow and his cohort cleaned up Python's internals enough to make subinterpreters useful, and they are adding a minimal module to the Python standard library called interpreters. This gives programmers a rudimentary way to launch subinterpreters and execute code on them.

[...] Another major set of performance improvements Shannon mentioned, Python's new adaptive specializing interpreter, was discussed in detail in a separate session by core Python developer Brandt Bucher.

Python 3.11 introduced new bytecodes to the interpreter, called adaptive instructions. These instructions can be replaced automatically at runtime with versions specialized for a given Python type, a process called quickening. This saves the interpreter the step of having to look up what types the objects are, speeding up the whole process enormously. For instance, if a given addition operation regularly takes in two integers, that instruction can be replaced with one that assumes the operands are both integers.

[...] Python objects have historically used a lot of memory. A Python 3 object header, even without the data for the object, occupied 208 bytes.

Over the last several versions of Python, though, various efforts took place to streamline the way Python objects were designed, finding ways to share memory or represent things more compactly. Shannon outlined how as of Python 3.12, the object header's now a mere 96 bytes—slightly less than half of what it was before.

These changes don't just allow more Python objects to be kept in memory, they also improve cache locality for Python objects. While that by itself may not speed things up as significantly as other efforts, it's still a boon.

[...] The default Python implementation, CPython, has three decades of development behind it. That also means three decades of cruft, legacy APIs, and design decisions that can be hard to transcend—all of which make it hard to improve Python in key ways.

[...] One key issue is the proliferation of C APIs found in CPython, the reference runtime for the language. As of Python 3.8, there are a few different sets of APIs, each with different maintenance requirements. Over the last five years, Stinner worked to make many public APIs private, so programmers don't need to deal as directly with sensitive CPython internals. The long-term goal is to make components that use the C APIs, like Python extension modules, less dependent on things that might change with each version.


Original Submission

 
This discussion was created by hubie (1068) for logged-in users only. Log in and try again!
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 3, Insightful) by Snotnose on Thursday May 04, @05:04AM (1 child)

    by Snotnose (1623) on Thursday May 04, @05:04AM (#1304661)

    The easiest problems to parallelize are called "embarrassingly parallel" problems.

    Such as something that you need to do to each and every pixel -- independently from adjacent pixels. So, NOT Conway's Game of Life which requires information about adjacent pixels.

    Say what now? Life is the perfect thing to parallelize. You ping pong between 2 buffers, A and B. While A is displayed you traverse it to build B. Then display B and build the next generation in A. Lather, rinse, repeat.

    If you try using only 1 buffer not only will it look like crap onscreen, it will be wrong. Say 2,2 is dead, but it's live for the next generation. Now when you process 2,3 the cell in 2,2, that should be dead, is now live.

    --
    I just passed a drug test. My dealer has some explaining to do.
    Starting Score:    1  point
    Moderation   +1  
       Insightful=1, Total=1
    Extra 'Insightful' Modifier   0  
    Karma-Bonus Modifier   +1  

    Total Score:   3  
  • (Score: 2) by DannyB on Thursday May 04, @02:12PM

    by DannyB (5839) Subscriber Badge on Thursday May 04, @02:12PM (#1304725) Journal

    If I were to attempt to parallelize Life, I would first implement the Hashlife [wikipedia.org] algorithm.

    This already makes Life much faster.

    Then I would parallelize that.

    --
    While Republicans can get over Trump's sexual assaults, affairs, and vulgarity; they cannot get over Obama being black.