Package: nibbler
nibbleris a runtime bytecode optimizer.It explores the concept of using existing Python syntax features such astype annotationsanddecoratorstospeed upcode execution by running additional bytecode optimization passes that make use of runtime context provided through these means.Optimization passesinlineInlines parameter-less calls to functions that are decorated with@nibbler.inline.constantize_globalsCopies 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_conditionalsStrips out conditionals that test constants which the peephole optimizer doesn't pick up on.global_to_fastTransforms global variable loads to local variable loads if a local variable with the same name exists (mostly a cleanup pass forinline)peepholeInvokes thePython peephole optimizerwith additional context.UsagefromtypingimportIterablefromnibblerimportConstant,NibblerDEBUG:Constant[bool]=Falsenibbler=Nibbler(globals())@nibbler.inlinedefsquare(number:int,base:int)->int:result=number**basereturnresult@nibbler.nibbledefsequential_square(numbers:Iterable[int])->int:product=0base=2fornumberinnumbers:square()ifDEBUG:print(result)product+=resultprint(f"Result:{product}")returnproductsequential_square(range(4))↓Result: 14Examining the function bytecode reveals which optimizationsnibblerhas 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_VALUEThesquarefunction was inlined (inline) (18-24)Conditional (if DEBUG) was stripped out, becauseDEBUGwas declared a constant (precompute_conditionals) (26)Theprintfunction was promoted to a function-level constant (constantize_globals) (38)Installationpip3installnibblerFAQIs this production ready?Hell no.Why is it callednibbler?¯\_(ツ)_/¯Prior ArtfatoptimizerPEP 510PEP 511falcon