Skip to content

Commit

Permalink
Create HAX.
Browse files Browse the repository at this point in the history
  • Loading branch information
brandtbucher committed Oct 16, 2019
0 parents commit 7a74dc4
Show file tree
Hide file tree
Showing 10 changed files with 1,079 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.egg-info/
.hypothesis/
.mypy_cache/
.pytest_cache/
.vscode/
__pycache__/
build/
dist/
70 changes: 70 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# https://github.com/brandtbucher/travis-python-matrix

# CPython 3.7.4 / CPython 3.6.8
# Ubuntu 16.04 (Xenial Xerus) / Windows 10 (64-bit) / macOS 10.14 (Mojave)

matrix:

include:

- name: CPython 3.7.4 on Ubuntu 16.04 (Xenial Xerus)
language: python
os: linux
dist: xenial
python: 3.7.4

- name: CPython 3.6.8 on Ubuntu 16.04 (Xenial Xerus)
language: python
os: linux
dist: xenial
python: 3.6.8

- name: CPython 3.7.4 on Windows 10 (64-bit)
language: shell
os: windows
before_install:
- export PATH=/c/Python37:/c/Python37/Scripts:$PATH
- choco install python --version 3.7.4

- name: CPython 3.6.8 on Windows 10 (64-bit)
language: shell
os: windows
before_install:
- export PATH=/c/Python36:/c/Python36/Scripts:$PATH
- choco install python --version 3.6.8

- name: CPython 3.7.4 on macOS 10.14 (Mojave)
language: shell
os: osx
osx_image: xcode10.2
before_install:
- export PATH=/Users/travis/.pyenv/shims:$PATH PYENV_VERSION=3.7.4
- travis_wait brew upgrade pyenv && pyenv install $PYENV_VERSION

- name: CPython 3.6.8 on macOS 10.14 (Mojave)
language: shell
os: osx
osx_image: xcode10.2
before_install:
- export PATH=/Users/travis/.pyenv/shims:$PATH PYENV_VERSION=3.6.8
- CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install $PYENV_VERSION

- name: mypy
language: python
os: linux
dist: xenial
python: 3.8
script: mypy

- name: Black
language: python
os: linux
dist: xenial
python: 3.8
script: black --check .

# Python and pip exectuables are just named "python" and "pip" on all platforms!

install: pip install -r requirements.txt

script: pytest
30 changes: 30 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div align=center>

The MIT License
===============

### Copyright © 2019 Gary Brandt Bucher, II

</div>

<div align=justify>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

</div>
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<div align=center>

HAX
===

[![latest version](https://img.shields.io/github/release-pre/brandtbucher/hax.svg?style=for-the-badge&label=latest)![latest release date](https://img.shields.io/github/release-date-pre/brandtbucher/hax.svg?style=for-the-badge&label=released)](https://github.com/brandtbucher/hax/releases)[![build status](https://img.shields.io/travis/com/brandtbucher/hax/master.svg?style=for-the-badge)](https://travis-ci.com/brandtbucher/hax/branches)[![issues](https://img.shields.io/github/issues-raw/brandtbucher/hax.svg?label=issues&style=for-the-badge)](https://github.com/brandtbucher/hax/issues)

<br>

</div>

<div align=justify>

HAX lets you write compiled bytecode inline with standard Python syntax.

Installation
------------

HAX supports CPython 3.6–3.9.

To install, just run:

```sh
$ pip install hax
```

Example
-------

Consider the following function; it accepts a sequence of items, and returns a
list with each item repeated twice:

```py
from typing import List, Sequence, TypeVar

T = TypeVar("T")

def double(items: Sequence[T]) -> List[T]:
out = []
for item in items:
out += item, item
return out
```

For example, `(0, 1, 2)` becomes `[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:

```py
from hax import *

@hax
def double(items: Sequence[T]) -> List[T]:

BUILD_LIST(0)

for item in items:

LOAD_FAST("item")
DUP_TOP()
LIST_APPEND(3)
LIST_APPEND(2)

RETURN_VALUE()
```

If you're up to the challenge of computing jump targets, the function can be
further sped up by rewriting the for-loop in bytecode, removing _all_ temporary
variables, and operating **entirely on the stack**:

```py
@hax
def double(items: Sequence[T]) -> List[T]:

BUILD_LIST(0)

LOAD_FAST("items")
GET_ITER()
FOR_ITER(34) # When done, jump forward to RETURN_VALUE().

DUP_TOP()
LIST_APPEND(3)
LIST_APPEND(2)
JUMP_ABSOLUTE(28) # Jump back to FOR_ITER(34).

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
`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.

</div>
Loading

0 comments on commit 7a74dc4

Please sign in to comment.