Write compiled bytecode inline with standard Python syntax.
HAX lets you write compiled bytecode inline with pure Python. It was originally built for exploring new improvements to CPython's compiler and peephole optimizer.
HAX supports CPython 3.6+ on all platforms.
To install, just run:
$ pip install hax
Consider the following function; it accepts a sequence of items, and returns a list with each item repeated twice:
def doubled(items): out =  for item in items: out += item, item return out
doubled((0, 1, 2)) returns
[0, 0, 1, 1, 2, 2].
We can make this function faster by keeping
out on the stack (instead of in a
local variable) and using the
LIST_APPEND op to build it. HAX makes it
simple to inline these instructions:
from hax import * @hax def doubled(items): BUILD_LIST(0) for item in items: LOAD_FAST("item") DUP_TOP() LIST_APPEND(3) LIST_APPEND(2) RETURN_VALUE()
With the help of labeled jump targets (
HAX_LABEL), the function can be further
sped up by rewriting the for-loop in bytecode, removing all temporary
variables, and operating entirely on the stack:
from hax import * @hax def doubled(items): BUILD_LIST(0) LOAD_FAST("items") GET_ITER() HAX_LABEL("loop") FOR_ITER("return") DUP_TOP() LIST_APPEND(3) LIST_APPEND(2) JUMP_ABSOLUTE("loop") HAX_LABEL("return") RETURN_VALUE()
It's important to realize that the functions HAX provides (
LOAD_FAST, ...) aren't just "emulating" their respective bytecode
@hax decorator detects them, and completely recompiles
double's code to use the actual ops that we've specified here!
These performance improvements are impossible to get from CPython's compiler and optimizer alone.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size hax-0.2.4-py3-none-any.whl (12.1 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size hax-0.2.4.tar.gz (11.5 kB)||File type Source||Python version None||Upload date||Hashes View|