Skip to content

Native lowering

This page covers when the compiler lowers a function to native code and what a function may contain to lower.

Eligibility and profitability

The compiler asks two different questions:

  • can_lower_native decides whether correct native code exists.
  • should_lower_native decides whether crossing the Python/native boundary pays for it.

A function with a loop, a buffer parameter, enough straight-line work, or an explicit @ppy.native/@ppy.jit/@ppy.specialize/@ppy.parallel gets the boundary. Native callers call its native symbol directly, boundary or not.

The generated wrapper's call costs about what a Python call does: 35 ns for def add(x: int, y: int) -> int: return x + y called from Python, against 33 ns for CPython's own call of it. So straight-line work pays from two operations (0.5 * base * height), and a one-operation helper stays on the Python side (remarked as R3004). The rest costs more:

what crosses straight-line work it takes
numbers, tuples of numbers 2 operations
a str parameter or result 4 more each: a native string is made for it
a value class 1 more per field
output held while the call runs (a print) a loop: one line written through Python costs more than CPython's print, many cost much less
a module global the function reads 6 more: a Python frame reads it for the wrapper
a call that draws from random 4: the wrapper saves its state (2.5 KB) and puts it back where the call falls back; 16 where the function also reads module globals, since the Python-level binding saves the state then

A straight-line function that calls itself, as power(b, e - 1) does, stays off the boundary: how deep it goes is the argument's to decide, and CPython raises RecursionError where native code has no limit to stop at.

A function that returns nothing gets the boundary where it loops over what it is given, or reads or fills a container the caller passed, and the work pays for it as for any other function. A function that only checks its arguments and raises stays a native caller's, and so does a main() that takes nothing: it runs once, and what it calls goes native on its own terms.

A container or an object crosses whole on each call, read in place where the call writes through none of its parameters and copied otherwise, so the body has to do work in proportion to it (Lists, dicts, and sets). An object of a plain class that crosses again and again is the exception: it stays resident in native memory between calls (Resident objects), and costs a flat price per call: about seven operations, one more per object, eight more for each object the call writes, and a hundred for each object the body makes. A function that makes objects stays in Python when its heaviest loop, over 16 passes, does less work than those hundreds, and always when it makes one inside a loop: an object native code makes is made in Python too when the call answers, about a microsecond each. ppy explain gives that reason as "the objects it makes cost more to hand to Python than its loops save".

ppy explain module.name (or FILE.ppy:LINE) reports the decision and, when the answer is no, the first blocking construct.

What a call costs

examples/bench_boundary.py calls each shape below many times from a Python loop and prints the time per call. The ppy run column is the program as ppy run runs it; the CPython column is the same program under python (python examples/bench_boundary.py --python prints both). The median of three runs, in nanoseconds:

call ppy run CPython
x + y of two ints, kept in Python by the cost model 32 33
x + y of two ints, @ppy.native 35 33
the same, y passed by keyword 39 38
the same, y left to its default 33 34
a loop of 100 additions 70 993
sum of a borrowed buffer of 100 ints 75 285
a guard that fails, so the Python body runs 99 43
a list[int] of 100 written in place, @ppy.native 1,208 2,738
a dict[int, int] of 100 walked, @ppy.native 1,380 1,900
a chain of 10 objects walked, @ppy.native 110 160
a function returning None that fills a list of 100 350 620
a list[int] of 100 summed 274 948
a list[list[int]] of 10 by 10 summed, @ppy.native 542 1,063
the lengths of a list[str] of 100 summed, @ppy.native 2,028 1,313
100 lookups in a set[int], @ppy.native 2,758 1,149
100 lookups of list[str] keys in a dict[str, int], @ppy.native 10,562 1,739
the trues of a list[bool] of 100 counted 207 714
one element of a list[int] of 100 written, @ppy.native 278 36
a function returning None that reads 100 and writes one 437 1,967
a random.randint and an addition 61 135
straight-line work on three objects, @ppy.native 100 78
find in a 10,000-node linked list, @ppy.native 33,883 62,507
len of a 10,000-node linked list, @ppy.native 79,835 128,830
contains in a 10,000-node search tree, @ppy.native 410 467
push onto a 10,000-node linked list, @ppy.native 1,848 163
insert into a 10,000-node search tree, @ppy.native 1,738 448

The object rows call functions and methods on objects that stay resident between calls (Resident objects). Copied at every call, as they were in 0.6, the 10,000-node ones took 3 to 5 ms a call and the chain of 10 about 1,060 ns. A resident object costs about 80 ns a call; native code then walks the nodes faster than CPython (find and len 1.6 to 1.8 times as fast), but an object it makes is made in Python too, with a __dict__ and a weak reference, about a microsecond, so push and insert, which make a node each, stay slower and the cost model keeps them in Python. Straight-line work on three objects costs 100 ns against CPython's 78, so without @ppy.native such a function runs its Python body.

The @ppy.native x + y row is the wrapper alone: parsing the arguments, the exact type checks, and boxing the result. A failed guard costs the wrapper plus a Python call. The rows that read a container and write none read it in place: the list of lists is laid out in the call's arena, the strings are borrowed, the dict is walked without being hashed. The written list copies 100 ints in and sets back only those that changed. What is left in the slower rows is native code's own: holding each string, hashing each key it looks up. Those are the shapes the cost model keeps off the boundary, as the CPython column says it should: one operation per string, per lookup, or per object does not pay, and without @ppy.native Python runs their Python bodies. Measured on Python 3.14 on an Intel Core Ultra 9 386H under WSL2; the CPython column is the same program in the same session.

Byte-wide buffers

A buffer's element is a machine word unless it says otherwise. Buffer[ppy.i8] and Buffer[ppy.u8] are one byte each, which is what text and packed data want: four million characters cost four megabytes rather than thirty-two.

The width is storage. Reading one hands out an int, and arithmetic on it is integer arithmetic. Writing a value that does not fit in a byte falls back to CPython rather than wrapping. An array.array("b", ...) is what such a buffer is made of.

Types that lower

A function lowers when each of its parameters, locals, and its result has a type native code holds, and its body stays inside the modeled subset. The types are:

A parameter has to be annotated, or under --no-strict inferred from the calls the project makes (Types from call sites). list[Any], a bare list, and NumPy arrays have no native form, and a function that takes one runs as Python. Under --no-strict, a parameter declared as a bare list takes the element type its calls agree on, and a local written out: list = [] (or dict, set with an empty display) is typed as out = [] would be, by what the function puts in it.

A *args: int or *args: float, with no keyword-only parameters or **kwargs after it, is a list of numbers to the native entry. When Python calls the function, the boundary packs the positions after the named parameters into that list. A native caller packs them the same way, or passes a list it was given whole as f(k, *xs). A position that is not of the declared type runs the call as Python.

A list parameter the function only reads is lent as a buffer, a copy of its numbers. One it returns, slices (xs[1:]), or adds to another list is held by handle instead, since a buffer is not a list it could hand back.

Numbers and strings that may be None

int | None, float | None, and bool | None are a number and a flag that says whether there is one. A local keeps them in two slots, a parameter and a result cross the native ABI as the number's atom and a byte, and a field or an element of a list or dict takes two words: the number, then the flag. None is the flag clear and the number 0. str | None is a string's handle, null for None. They can be parameters, results, fields, list and dict elements, and locals:

class Node:
    def __init__(self, key: int, label: int | None = None) -> None:
        self.key = key
        self.label = label
        self.left: Node | None = None
        self.right: Node | None = None


def floor_label(root: Node, key: int) -> int | None:
    best: int | None = None
    node: Node | None = root
    while node is not None:
        if node.key <= key:
            best = node.label
            node = node.right
        else:
            node = node.left
    return best

Natively, x is None and x == None read the flag; if x: is the flag and a nonzero number (a non-empty string); x == 3 is false for None; x or d gives d for None and for zero; x in (True, None), isinstance(x, int), print(x), str(x), f"{x}", and the repr of a list or dict holding them ([1, None]) write None where it is one. d.get(k) with no default gives None for a missing key, None in xs and xs.count(None) find it, and any/all count it as false. Of a dict[str, int | None], d.get(k, 5) and d.pop(k, 5) give the stored None where the key holds one and the default only where the key is missing, and the checker types them int | None accordingly. A display of Nones returned in place (return [[None] * w for _ in range(h)] from a -> list[list[str | None]]) is made as the declared type, as it is when assigned to an annotated local. After a test that narrows x, its number is read directly.

A field the checker narrowed (if node.label is not None:) may still be None when a call between the test and the read set it so, which CPython then meets: the read checks the flag. Arithmetic, an order comparison, or a negation that finds None raises CPython's TypeError, with its text (unsupported operand type(s) for +: 'NoneType' and 'int', '<' not supported between instances of 'int' and 'NoneType'); a read where None cannot be the answer (passing it on as an int) falls back under ppy run and stops a standalone binary.

At the Python boundary None crosses as a clear flag or a null handle, in arguments, results, fields of objects, and elements of lists and dicts, both ways. A value of another type keeps the call in Python: a bool for an int | None, and an int for a float | None whose int-ness the body would show. CPython keeps an int an int in a float | None, so a native caller that passes one is not compiled either. An object whose fields may be None stays resident between native calls like any other: a field Python sets to None (or back) between the calls is read again, and one native code sets is set on the Python object when the call answers.

What stays in Python: sorting, min, and max over elements that may be None (CPython raises for them, in an order that depends on the comparisons), keys that may be None, a format spec over a value that may be None (f"{x:>4}"), x and y used as a value of two such operands, repr(s) of a str | None, unions of numbers with anything but None, tuples holding a value that may be None (tuple[int | None, int]), and a value class that may be None (d.get(k) of a dict[str, Item] where Item holds only numbers: the class is its fields' words, with no flag beside them).

What the body may contain

A function declared to return a value may end in an if/elif/else whose every side returns or raises: no path reaches the end, so the end needs no value. Where a path does reach the end, CPython returns None, which an int or a float result cannot hold, so the native call falls back there and Python runs it and returns that None. A standalone build has no Python to fall back to and refuses such a function.

The subset includes what a loop is normally made of:

  • break and continue (a continue in a for still advances the counter)
  • statement-level calls whose result is discarded
  • buffers handed on to another native function
  • the bitwise operators, including ~
  • raise, try with its handlers, else, and finally, and assert
  • a generator consumed where it is made: by for, next, sum, min, max, a comprehension, or a collection built from it; and one that is returned, passed on, stepped in a loop, or made by __iter__, which gets a frame of its own

Exceptions and generators has the details.

Expressions

  • isinstance(x, T) and isinstance(x, (A, B)) with builtin classes (int, float, bool, str, list, dict, set, tuple, type(None)) fold to a constant where the checker's type of x decides the answer. An int may be a bool and a float may be an int, so isinstance(n, bool) of an n: int local stays in Python. A parameter of a module-level function that is only called by name and never rebound in its body is the exception: Python's calls reach it only with a real int (the boundary refuses a bool), and a native call that passes a bool to an int parameter whose body shows the difference (prints it, formats it, asks its class, returns it) stays in Python, so isinstance(n, bool) of that parameter is False natively. A float parameter that shows whether it is an int is likewise always a float. A local that every assignment gives a real int (an int literal, len(...), int(...), or arithmetic other than &, |, ^ on ints and bools) or a real float (a float literal, float(...), or arithmetic with a real float on one side), and that no loop, with, handler, or nested scope rebinds, is decided the same way. Object classes are tested by the class tag the instance carries.
  • A bool stays a bool in Python wherever it is stored: after x: int = flag, x prints True. Native code holds an int as a 64-bit word, which keeps only the 1. So a function that stores a bool where an int is declared stays in Python, and a standalone build reports it. That covers a local (also one rebound from an int), a return from an -> int function, an element of a list[int], dict[..., int], or tuple[int, ...], an int field, and an argument through a Callable[[int], ...] or to a class. A call of a module function by name stays in Python only where the callee prints, returns, stores, or tests that parameter. Arithmetic is not a store: flag + n, -flag, and True + 1 are ints in Python too and stay native. To keep the function native, annotate the slot bool, or store int(flag) where an int is meant.
  • A chained comparison, 0 <= i < n, is its comparisons joined by and, each operand evaluated once and the ones after a false comparison not at all.
  • x in (a, b), x in {a, b}, and x in [a, b] are x == a or x == b. x in range(start, stop, step) is a bounds check and a remainder, with no range made. not in is the negation. A NaN on the left falls back, since CPython matches it by identity.
  • a ** b and pow(a, b) of ints multiply by squaring. A product past 64 bits falls back, and so does a negative exponent, whose result is a float. pow(a, b, m) reduces as it goes, and pow(a, b, 0) raises CPython's ValueError.
  • a = b = value evaluates the value once and binds each name. r, c = (x, y) if flag else (y, x) makes and unpacks only the chosen side.
  • holes, seen = [0] * n, [] binds each list in turn, and a, b = b, a of two lists takes both before it binds either, as Python does.
  • a, b, c = xs of a list checks its length first and raises CPython's ValueError (not enough values to unpack (expected 3, got 2)) when it differs.
  • if not xs: and xs == [] of a list test its length.
  • A module-level string bound once to a literal, such as LETTERS = "ABC...", is read as that literal: LETTERS.find(c), LETTERS[:6], c in LETTERS.

Loops

A for walks:

  • range with any step, including one known only at run time; a zero step raises ValueError
  • reversed(range(...)), from its last value
  • a string's characters, a tuple display (for dx, dy in ((0, 1), (1, 0))), and a tuple local
  • a list parameter lent as a buffer

Each of these also works under enumerate (with start=), zip, and, for ranges, buffers, and tuples, reversed, alone or mixed with the collections. A for over any of them, or over a collection, may have an else, which runs when the loop ends without break. Identity between two values the checker types bool (flag is True) is equality, since there is one True and one False.

A float given an int

CPython lets an int stand where a float is declared and keeps it an int: f(0) of def f(x: float) -> float: return x is 0. Native code converts at the boundary, which is only right where the difference cannot show. The compiler follows each float parameter (and each tuple, list, or value class holding floats) through the body. Where the value, or something computed from it the way an int stays an int, is returned, printed or formatted, stored where the caller sees it, or asked its class, the boundary takes only a float for it and an int keeps the call in Python. A native caller that passes an int to such a parameter stays in Python too, and so does one that builds a value class with an int for a float field. Every other float parameter takes an int up to 2**53, converted exactly.

A module constant written as an expression, such as MOD = 10**9 + 7 or LIMIT = 1 << 20, folds into the code rather than staying a global read. So does a table of numbers bound once at module level, such as RATES = (0.1, 0.3, 0.6): a tuple of up to 16 ints, floats, or bools of one type. Native code builds it once per call and reads RATES[i] with an index known only at run time, after the bounds check CPython makes. Being a constant, it reaches a standalone binary too, where a global read does not.

Calls

A call between native functions may name its arguments and leave some to their defaults. The compiler binds the call the way Python would: each keyword goes to the parameter it names, including a keyword-only one, and each parameter left out takes its default. The call then goes ahead in order. This holds for functions, methods, and __init__.

def scale(x: int, factor: int = 3, *, offset: int = 0) -> int:
    return x * factor + offset


def use(n: int) -> int:
    return scale(n) + scale(x=n, offset=1) + scale(n, factor=4)

Python evaluates arguments in the order they are written. Binding moves a keyword argument to its parameter's place, so it may only move where the order cannot be seen: at most one of the keyword arguments that change places runs any code, and the rest are names, constants, or attributes. Python evaluates a default once, when the def runs, so the compiler puts a default into the call only where it is a constant: a number, a string, None, or a tuple of those. A call that leaves out a parameter whose default is anything else (xs: list[int] = []), a call that spreads *args (other than a list passed whole to a *args parameter, as in Types that lower) or **kwargs, and a method call bound by keyword where a subclass overrides the method stay in Python.

When Python calls a native function with keywords or with defaults left out, the boundary binds the call by the Python function's own signature, with its own default values, and calls the native entry in order. A call that does not bind goes to the Python function, which raises CPython's TypeError. The generated C wrapper does this binding itself: it matches the call's keyword names against the parameter names and takes left-out values from the function's __defaults__ and __kwdefaults__, so such a call costs about what a positional one does (the table above). When a guard refuses the bound arguments, Python gets the call as it was written.

A function whose writes all happen inside a callee it handed a buffer to lowers too: the write lands in the caller's memory either way. The reverse also lowers: filling memory you allocated and then passing it on.

A call to a function that did not lower goes through Python under ppy run: the native caller boxes the arguments, calls the Python function with the GIL held, and takes back a result of the type the checker gave the call. Which results it may take, and why such a call is often a barrier, is in Calls into Python. A standalone binary has no Python to call, so there the caller does not lower either, and the build stops with E1803.

int(x) of a float truncates toward zero, as CPython does, when the result is a 64-bit word. The machine's conversion has no answer for the rest (x86 gives -2**63, and C leaves it undefined), so each case is a guard:

x CPython ppy run a standalone binary, emitted C or C++
NaN ValueError: cannot convert float NaN to integer falls back and raises it prints it and exits 1
an infinity OverflowError: cannot convert float infinity to integer falls back and raises it prints it and exits 1
past 2**63 the exact integer falls back and returns it OverflowError: the result does not fit in a 64-bit integer, exit 1

The guards are range checks, so --unsafe keeps them. Nothing saturates to the largest or smallest word: Python's integers have no largest.

Finding what stays in Python

ppy explain --summary answers, for a file, a directory, or a project, how much of the code goes native and what keeps the rest in Python. Here it is over the sorts folder of TheAlgorithms/Python with --no-strict, trimmed to the first two reasons:

175 functions, 1389 statements
  native, called from Python              7 functions (  4%)       88 statements (  6%)
  native, called from native code         7 functions (  4%)       74 statements (  5%)
  Python                                161 functions ( 92%)     1227 statements ( 88%)
  (73 of the Python functions are generic: each native caller compiles its own instance)

what keeps functions in Python, by statements kept out (a function can count under more than one):
       81 statements      3 functions  a parameter of type `(Any) -> Any | NoneType`
      take a type native code holds (numbers, str, tuples, lists, dicts, sets, ppy collections, project classes)
      see https://ppy.franknoh.dev/latest/guide/native-lowering/
      sorts/power_sort.py:34 sorts.power_sort._find_run
      sorts/power_sort.py:137 sorts.power_sort._merge
      sorts/power_sort.py:199 sorts.power_sort.power_sort
       59 statements     12 functions  a parameter or result with no annotation the checker could infer
      annotate it, or run `ppy convert` to write the inferred annotations
      see https://ppy.franknoh.dev/latest/guide/subset/
      sorts/external_sort.py:13 sorts.external_sort.FileSplitter.__init__
      sorts/external_sort.py:26 sorts.external_sort.FileSplitter.split
      sorts/external_sort.py:48 sorts.external_sort.NWayMerge.select
  ... 62 more reasons, 73 functions (--limit to see more, --json for all)

native, but Python calls the Python body (why its boundary is not used):
      5 functions  copying the collections in costs more than the body does with them
      1 functions  copying its strings across costs what one pass over them saves

Read it from the top down:

  • The first block counts every function once. "Called from native code" means the function compiled but Python calls its Python body, because the crossing costs more than the body saves, the objects it makes cost more to hand to Python than its loops save, or it passes objects by handle; the summary lists those reasons last.
  • The reasons are ordered by statements kept out, so the first one is where a change moves the most code. A function with several effects counts under each.
  • A generic function is not a blocker: it has no entry point of its own and is compiled for each native caller that names its types. Most of sorts is generic sorts that nothing calls natively.
  • A nested function that shares no variable with the functions around it, or only reads variables nothing rebinds while it runs, is counted on its own, since it has an entry of its own (Functions as values). One that rebinds a shared variable lowers with the function around it: it counts as native and called from native code when that function is native, and otherwise says that the function around it stays in Python, which is the reason to fix.
  • Each reason says what to do and links the page that explains it. The first places it occurs are listed with their line.

--json gives every function with its tier and reason, for a script or a dashboard. See the command. Without --no-strict the summary analyzes as ppy check does, so an unannotated parameter is an error and its function counts as Python; on code written without annotations, pass --no-strict to see what ppy run --no-strict compiles.

Threads

The generated wrapper releases the GIL around a native call that loops or calls another function, so @ppy.native functions scale across threads. A short straight-line body keeps it: dropping the GIL and taking it back costs about 20 ns, what two operations cost. A function that prints, reads, or calls into Python keeps the GIL too: its wrapper holds its output until the call ends and writes it out then, and the call takes the GIL where it reaches Python (Effects in native code). So does a call whose objects are resident (Resident objects); the same function called with objects that are copied releases it.

Reading input is its own guide: Reading input.

Examples: Algorithms, Buffers and JIT.