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.
Installation
HAX supports CPython 3.7+ on all platforms.
To install, just run:
$ pip install hax
Example
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
For example, 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 (BUILD_LIST,
LOAD_FAST, ...) aren't just "emulating" their respective bytecode
instructions; the @hax decorator detects them, and completely recompiles
doubled'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.
Metadata
Release files for hax 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| hax-0.3.0.tar.gz | 12.4 kB | Without provenance | Details for hax-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hax-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for hax-0.3.0-py3-none-any.whl |
Total release size: 24.9 kB
Release files / hax-0.3.0.tar.gz
| Download URL | hax-0.3.0.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b78835659e700075ac88492753d3e04d5fb5c1dfd6cf3a86bcb3c3bb3b0f96ac
|
|
BLAKE2b-256 checksum How to use checksums |
f78a6cdaaab8c26df6b3c90895e819843a93673997e142823396056c9cb2e71b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.0
|
Release files / hax-0.3.0-py3-none-any.whl
| Download URL | hax-0.3.0-py3-none-any.whl |
|---|---|
| Size | 12.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
615fe8f8edb1277a38a15dd5215d2eb6ac82f8decb405b48b7262f4db9f26332
|
|
BLAKE2b-256 checksum How to use checksums |
b69ddb969bf2d0eee2b771415f49595815e5f2350e451947e28abc13c885f856
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.10.0
|
Security for hax 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.