nibbler is a runtime bytecode optimizer.
It explores the concept of using existing Python syntax features such as type annotations and decorators to speed up code execution by running additional bytecode optimization passes that make use of runtime context provided through these means.
Optimization passes
inline
Inlines parameter-less calls to functions that are decorated with@nibbler.inline.constantize_globals
Copies the value of globals that were marked constant (with aConstanttype annotation or with the@nibbler.constantdecorator) into theco_conststuple of functions that would normally have to access the global namespace, which speeds up variable access. This also applies to builtins (any,all,print, ...).precompute_conditionals
Strips out conditionals that test constants which the peephole optimizer doesn't pick up on.global_to_fast
Transforms global variable loads to local variable loads if a local variable with the same name exists (mostly a cleanup pass forinline)peephole
Invokes the Python peephole optimizer with additional context.
Usage
from typing import Iterable
from nibbler import Constant, Nibbler
DEBUG: Constant[bool] = False
nibbler = Nibbler(globals())
@nibbler.inline
def square(number: int, base: int) -> int:
result = number ** base
return result
@nibbler.nibble
def sequential_square(numbers: Iterable[int]) -> int:
product = 0
base = 2
for number in numbers:
square()
if DEBUG:
print(result)
product += result
print(f"Result: {product}")
return product
sequential_square(range(4))
↓
Result: 14
Examining the function bytecode reveals which optimizations nibbler has performed:
2 0 LOAD_CONST 1 (0)
2 STORE_FAST 1 (product)
3 4 LOAD_CONST 2 (2)
6 STORE_FAST 2 (base)
4 8 SETUP_LOOP 28 (to 38)
10 LOAD_FAST 0 (numbers)
12 GET_ITER
>> 14 FOR_ITER 20 (to 36)
16 STORE_FAST 3 (number)
5 18 LOAD_FAST 3 (number)
20 LOAD_FAST 2 (base)
22 BINARY_POWER
24 STORE_FAST 4 (result)
6 26 LOAD_FAST 1 (product)
28 LOAD_FAST 4 (result)
30 INPLACE_ADD
32 STORE_FAST 1 (product)
34 JUMP_ABSOLUTE 14
>> 36 POP_BLOCK
8 >> 38 LOAD_CONST 5 (<built-in function print>)
40 LOAD_CONST 3 ('Result: ')
42 LOAD_FAST 1 (product)
44 FORMAT_VALUE 0
46 BUILD_STRING 2
48 CALL_FUNCTION 1
50 POP_TOP
9 52 LOAD_FAST 1 (product)
54 RETURN_VALUE
- The
squarefunction was inlined (inline) (18-24) - Conditional (
if DEBUG) was stripped out, becauseDEBUGwas declared a constant (precompute_conditionals) (26) - The
printfunction was promoted to a function-level constant (constantize_globals) (38)
Installation
pip3 install nibbler
FAQ
- Is this production ready?
Hell no. - Why is it called nibbler?
¯\_(ツ)_/¯
Prior Art
Metadata
Release files for nibbler 0.1.3
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 |
|---|---|---|---|---|
| nibbler-0.1.3.tar.gz | 12.4 kB | Without provenance | Details for nibbler-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nibbler-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for nibbler-0.1.3-py3-none-any.whl |
Total release size: 50.7 kB
Release files / nibbler-0.1.3.tar.gz
| Download URL | nibbler-0.1.3.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
945fd900b47311898c245f79ef7bfd6f20538ed1cb8c4793cebf471c273aa852
|
|
BLAKE2b-256 checksum How to use checksums |
8594e5f5894e7f8fa8cd0a328d213e25c2528147ade859cd6a3b1429256e8704
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.0a2 CPython/2.7.16 Darwin/18.2.0
|
Release files / nibbler-0.1.3-py3-none-any.whl
| Download URL | nibbler-0.1.3-py3-none-any.whl |
|---|---|
| Size | 38.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5e30c8fc4bc70eaebdf127919493c4f8362e763c00c03213ce8d60785b824ff8
|
|
BLAKE2b-256 checksum How to use checksums |
6bf8fe6821ccfae42dc6bb7f61cef8ee90deb4cefb00c074e5ad1381195a3d6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.0a2 CPython/2.7.16 Darwin/18.2.0
|
Security for nibbler 0.1.3
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.