Skip to main content

bytecode optimisation using staticness assertions.

Project description

promise: bytecode optimisation using staticness assertions.

This is a module for applying some simple optimisations to function bytecode.
By promising that a function doesn't do certain things at run-time, it's
possible to apply optimisations that are not legal in the general case.

As a simple example, it's possible to promise that a function doesn't modify
(or care if anyone else modifies) any builtin functions by decorating it thus:

@promise.constant(__builtins__)
def function():
...

Such a promise will allow the builtins to be stored as direct object references
in the function bytecode, avoiding name lookups during function execution.

As another example, it's possible to promise that a function is pure; i.e. that
it's a simple algorithm for mapping input values to an output value:

@promise.pure()
def calculate(a,b):
return 2*a*a + 3*b + 7

If a pure function is then used by another function as a constant, it can be
directly inlined into the bytecode to avoid the overhead of a function call:

@promise.constant(("calculate",))
def aggregate(pairs):
# calculate() is a pure constant, so it will be inlined here.
return sum(calculate(a,b) for (a,b) in pairs)

The currently available promises are:

* invariant(names): promise that variables having the given names will
not change value during execution of the function.

* constant(names): promise that variables having the given names will
always refer to the same object, across all calls
to the function.

* pure(): promise that the function is a transparent mapping from inputs
to outputs; this opens up the possibility of inling it directly
into other functions.

* sensible(): promise that the function is "sensibly behaved". All
builtins and module-level functions are considered
constant; all other module-level names are considered
invariant.

Promise is built on Noam Raphael's fantastic "byteplay" module. It used be
be bundled as part of promise because it needed some patches to work with
newer versions of Python; now it's just bundled for your convenience.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

promise-0.2.2.tar.gz (19.8 kB view details)

Uploaded Source

File details

Details for the file promise-0.2.2.tar.gz.

File metadata

  • Download URL: promise-0.2.2.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for promise-0.2.2.tar.gz
Algorithm Hash digest
SHA256 1fb52a23bee47644819c4a11b0b7169474625c44629f9b76a04cf59e118f4f6c
MD5 31855fcb6771fb97afc999733b1c28b6
BLAKE2b-256 13eeeb8413c7e46f2dc110454e366b3ccf39e6db765749c8daf01a625c33323f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page