Blog post #3

bksahu
Published: 07/07/2019

Hello everyone! Welcome to my 3rd blog post.

So, I am very happy to inform that I have passed my first evaluations with flying colors and now working on phase 2.

I learned a new way of optimization for Nuitka that doesn't need any CPython code replication. The idea is very simple i.e to build on top of existing nodes on Nuitka. Consider the following example, below is the optimization for min() built-in

makeStatementConditional(
    condition=ExpressionComparisonGt(
        left=ExpressionTempVariableRef(
            variable=min_result_variable, source_ref=source_ref
        ),
        right=ExpressionTempVariableRef(
            variable=min_arg_variable, source_ref=source_ref
        ),
        source_ref=source_ref,
    ),
    yes_branch=StatementAssignmentVariable(
        variable=min_result_variable,
        source=ExpressionTempVariableRef(
            variable=min_arg_variable, source_ref=source_ref
        ),
        source_ref=source_ref,
    ),
    no_branch=None,
    source_ref=source_ref,
)

The above is equivalent to (in Python):

min_result_variable if min_result_variable > min_arg_variable else None

In this way we can optimize reusing existing nodes. Now the only difficulty is in the case when there is more than one argument. Such type of problems are handled in Nuitka using something called reformulation. So, by using reformulation our problem will decomposed in to a simpler problem. For example:

min(1, 2, 3) ---(reformulation)---> min(min(1, 2), 3)

Hence, in this way we can optimize in an efficient way.

Thank you for stopping by! 
Batakrishna

1000 characters left