Skip to content

Optimising

Optimisation

Bases: ABC

Base class for tracing-related optimisations. If more optimisations need to be made, this class should be inherited from and the abstract methods are to be implemented

__init__(fwm)

Construct an Optimisation for a stack frame

Parameters:

Name Type Description Default
fwm FrameWithMetadata

The frame that originally activates the Optimisation

required

status() abstractmethod

Get the optimisation's status. See TriggerStatus for an explanation of how each one influences the impact of the optimization upon a frame in self.apply

Returns:

Type Description
TriggerStatus

The optimisation's status

advance(current_frame, traced) abstractmethod

Modify the optimization's internal state based on given frame. The traced DataFrame should be treated as read-only

Parameters:

Name Type Description Default
current_frame FrameWithMetadata

The current stack frame

required
traced pd.DataFrame

The current trace data from the Tracer

required

__eq__(o) abstractmethod

Overloaded equality in order to check for already active / duplicate optimizations

Returns:

Type Description
bool

True if the other optimisation is of the same type and references the same frame

TriggerStatus

Bases: enum.IntEnum

The different states an Optimisation can find itself in

INACTIVE = 0 class-attribute

Non-optimising, usually for gathering information during execution

ENTRY = 1 class-attribute

Optimising, first time the tracer will be turned off

ONGOING = 2 class-attribute

Optimising, tracer is continually being turned off

EXITED = 3 class-attribute

Non-optimising, the tracer will remove this optimisation at the first opportunity

TypeStableLoop

Bases: Optimisation

Triggers when the types of instances within loop bodies are stable for the given amount of iterations.

__init__(frame, iterations_until_entry=5)

Parameters:

Name Type Description Default
frame FrameWithMetadata

Representation of stack frame that points to head of for loop

required
iterations_until_entry int

The amount of iterations that shall pass until the optimisation starts firing

5

FrameWithMetadata dataclass

A wrapper dataclass that takes the current frame and checks properties of the frame's state

co_filename() property cached

Get the filename of the code referenced by the frame

f_lineno() property cached

Get the line number referenced by the frame

tokens() property cached

Get a representation of the line currently being executed

is_return()

Return True if the frame represents a return statement

is_break()

Return True if the frame represents a break statement

is_for_loop()

Return True if the frame represents a for loop