\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.6+ 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`double`'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