Skip to content

0.5.0

Choose a tag to compare

@franknoh franknoh released this 30 Sep 09:45
· 104 commits to main since this release
a070e98

Changes

Ordinary Python goes native without a rewrite. Plain lists, dicts, and sets compile, and so do exceptions, generators, closures, lambdas, and functions passed as values. A warm ppy run is within noise of python or faster on every example, and a first run takes about half as long as in 0.4. ppy explain --summary shows what stays in Python and why. A differential fuzzer now checks every path against CPython, and the bugs it and a corpus of public programs found are fixed.

Lists, dicts, and sets

  • Plain list, dict, and set lower to native code under ppy run, in standalone builds, and in emitted C and C++, with no rewrite to ppy.Vec or ppy.HashMap. This covers:
    • displays and comprehensions;
    • their methods, slicing, and negative indices;
    • in, del, sorted, min, max, sum, and set algebra.
  • Results and error messages are CPython's, and except KeyError and the like around them run natively.
  • A function that takes or returns a container can be called from Python natively. Arguments are copied in, and writes come back to the caller's object.
  • On a benchmark of plain-container code (longest increasing subsequence, a dict histogram, a set of tuples, a list sieve), CPython takes 1042 ms, ppy run 587 ms, and a standalone binary 554 ms.

Exceptions and generators

  • raise, try/except/else/finally, and assert compile to native code, and exceptions cross native calls.
  • Generator functions and generator expressions compile when a for, next, sum, min, max, sorted, any, all, or a comprehension consumes them.
  • New example 50_errors_and_generators: python 2.1 s, ppy run 0.43 s, standalone 0.29 s.

ppy run is never slower

  • A warm ppy run starts in about 15 to 20 ms over python, down from about 40 ms.
    • It checks only the program's own sources and runs before the command line is parsed.
    • A program with no native entry point runs from a precompiled plan.
  • The runtime decides to call native code from Python only when the work grows with what crosses the boundary.
  • String work is faster: the strings example runs in about 1 s warm, against 2.4 s for python.
  • scripts/run_overhead.py measures every example, and the performance page shows the result.

Functions as values

  • Nested functions, lambda, and nonlocal compile. They use CPython's late binding, so a closure sees the variable as it is when the closure runs.
  • Callable values work as parameters, results, container elements, and fields.
  • sorted/min/max/sort take any key=.
  • map and filter compile where they are consumed.

More of Python native

  • Exception classes with fields, and raise ... from.
  • Generators stepped with next and for.
  • Sets of ints walked in CPython's order.
  • float and bool dict keys.
  • Dataclass == and repr, str()/repr() of objects.
  • str.format and % formatting.

First runs and coverage

  • A first ppy run takes a median of 0.38 s, down from 0.67 s. The C runtimes are compiled once per machine with optimization, which also makes warm runs of collection- and string-heavy programs faster.
  • ppy explain --summary [PATH ...] reports how many functions and statements go native and ranks what keeps the rest in Python, with a hint and a guide link for each reason.
    • On TheAlgorithms/Python, 14% of functions compile today. The top blockers are calls with unknown effects, I/O, and writes to parameters.
    • Running 400 of its scripts under CPython and ppy run found several wrong answers, now fixed. Two examples: row = [] hoisted out of a loop, and a, b = [0]*n, [0]*n sharing one list.

Differential fuzzing and fixes

  • scripts/fuzz.py generates programs over the native subset and runs each on CPython, the Python backend, ppy run, a standalone binary, and C and C++ under AddressSanitizer. It reports any difference, and tests/test_fuzz.py replays every case it has found.
  • It found, and this release fixes:
    • NaN comparisons and truth.
    • a if c else b evaluating both sides.
    • and/or over numbers returning a bool.
    • A range loop that rebinds its variable looping forever.
    • Several undefined behaviors in emitted C.
    • A stale list length after del, pop, or append.
    • min/max reading memory before writing it.
    • An interned empty literal shadowing "-inf".
    • Alias analysis taking gigabytes on nested loops.
    • A warm ppy run reusing an old build after a same-size edit made within one timestamp tick.

Known limitation: a native function with a float parameter given an int returns a float where CPython keeps the int (f(0) gives 0.0). A fix is planned for 0.6.0.

Install or upgrade with pip install --upgrade ppy-lang==0.5.0.

The full repository gate passed: 2,151 tests, 45/45 examples on all three paths, pylint on every example, and the documentation examples. The emitted C and C++ of the new tests runs clean under AddressSanitizer with leak detection.