{"id":19353410,"url":"https://github.com/brandtbucher/hax","last_synced_at":"2025-08-22T07:22:32.925Z","repository":{"id":62568953,"uuid":"215491683","full_name":"brandtbucher/hax","owner":"brandtbucher","description":"Write compiled bytecode inline with pure Python. 🤖","archived":false,"fork":false,"pushed_at":"2024-09-03T21:35:12.000Z","size":85,"stargazers_count":75,"open_issues_count":8,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-23T23:12:08.696Z","etag":null,"topics":["bytecode","cpython"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brandtbucher.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-16T08:03:15.000Z","updated_at":"2024-11-29T16:12:09.000Z","dependencies_parsed_at":"2024-12-29T08:10:44.464Z","dependency_job_id":"53e12cfa-82a1-42b9-a92f-743e27ae4264","html_url":"https://github.com/brandtbucher/hax","commit_stats":{"total_commits":107,"total_committers":3,"mean_commits":"35.666666666666664","dds":0.04672897196261683,"last_synced_commit":"88d5815083ca8faab271ec18be163ca596ec56b0"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandtbucher%2Fhax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandtbucher%2Fhax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandtbucher%2Fhax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandtbucher%2Fhax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brandtbucher","download_url":"https://codeload.github.com/brandtbucher/hax/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241587929,"owners_count":19986628,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bytecode","cpython"],"created_at":"2024-11-10T04:42:50.346Z","updated_at":"2025-03-03T00:10:59.964Z","avatar_url":"https://github.com/brandtbucher.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=center\u003e\n\nHAX\n===\n\n[![latest version](https://img.shields.io/github/release-pre/brandtbucher/hax.svg?style=for-the-badge\u0026label=latest)![latest release date](https://img.shields.io/github/release-date-pre/brandtbucher/hax.svg?style=for-the-badge\u0026label=released)](https://github.com/brandtbucher/hax/releases)[![build status](https://img.shields.io/github/actions/workflow/status/brandtbucher/hax/ci.yml.svg?style=for-the-badge\u0026branch=master)](https://github.com/brandtbucher/hax/actions)[![issues](https://img.shields.io/github/issues-raw/brandtbucher/hax.svg?label=issues\u0026style=for-the-badge)](https://github.com/brandtbucher/hax/issues)\n\n\u003cbr\u003e\n\n\u003c/div\u003e\n\n\u003cdiv align=justify\u003e\n\nHAX lets you write compiled bytecode inline with pure Python. It was originally\nbuilt for exploring new improvements to CPython's compiler and peephole\noptimizer.\n\nInstallation\n------------\n\nHAX supports CPython 3.7-3.10 on all platforms.\n\nTo install, just run:\n\n```sh\n$ pip install hax\n```\n\nExample\n-------\n\nConsider the following function; it accepts a sequence of items, and returns a\nlist with each item repeated twice:\n\n```py\ndef doubled(items):\n    out = []\n    for item in items:\n        out += item, item\n    return out\n```\n\nFor example, `doubled((0, 1, 2))` returns `[0, 0, 1, 1, 2, 2]`.\n\nWe can make this function faster by keeping `out` on the stack (instead of in a\nlocal variable) and using the `LIST_APPEND` op to build it. HAX makes it\nsimple to inline these instructions:\n\n```py\nfrom hax import *\n\n@hax\ndef doubled(items):\n\n    BUILD_LIST(0)\n\n    for item in items:\n\n        LOAD_FAST(\"item\")\n        DUP_TOP()\n        LIST_APPEND(3)\n        LIST_APPEND(2)\n\n    RETURN_VALUE()\n```\n\nWith the help of labeled jump targets (`HAX_LABEL`), the function can be further\nsped up by rewriting the for-loop in bytecode, removing _all_ temporary\nvariables, and operating **entirely on the stack**:\n\n```py\nfrom hax import *\n\n@hax\ndef doubled(items):\n\n    BUILD_LIST(0)\n\n    LOAD_FAST(\"items\")\n    GET_ITER()\n    HAX_LABEL(\"loop\")\n    FOR_ITER(\"return\")\n\n    DUP_TOP()\n    LIST_APPEND(3)\n    LIST_APPEND(2)\n    JUMP_ABSOLUTE(\"loop\")\n\n    HAX_LABEL(\"return\")\n    RETURN_VALUE()\n```\n\nIt's important to realize that the functions HAX provides (`BUILD_LIST`,\n`LOAD_FAST`, ...) aren't just \"emulating\" their respective bytecode\ninstructions; the `@hax` decorator detects them, and completely recompiles\n`doubled`'s code to use the _actual_ ops that we've specified here!\n\nThese performance improvements are impossible to get from CPython's compiler and\noptimizer alone.\n\n\u003c/div\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrandtbucher%2Fhax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrandtbucher%2Fhax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrandtbucher%2Fhax/lists"}