Skip to content

Feature request: Decorator to compile python function (and all of its dependencies) to C++ and swap the original function to the new C++ function #2

@ItsCubeTime

Description

@ItsCubeTime

Eg:

import pycer
def someFunctionCalledFromTheCppFunction(var:int, var2:int):
    return var+var2

def somePurePythonFunction(var:int, var2:int):
    return var*var2

@pycer.compileInline # Im not quite that familiar with how C++ functions in Python STD gets exposed to Python, but I would 
# imagine compileInline to take the source code of the function fed into it (in this case 
# someFunctionThatGetsCompiledAndCachedAtExecutionTime) and all of its external references (in this case 
# someFunctionCalledFromTheCppFunction), concatonate it into a single long string, translate the new string to C++ and then
# transformation the C++ code to turn it into a Python exposed C++ module (perhaps using
# https://pybind11.readthedocs.io/en/latest/basics.html#creating-bindings-for-a-simple-function ), compile it using a C++ 
# compiler of choice, like cl.exe or clang.exe and place the resulting binary dll/"module" file right next to the py file 
# (might need to rename the binary module to avoid naming conflicts) and finally, "compileInline" would then import and 
# return the newly created function). 

# Ideally it would also use some kind of alteration aware caching (maybe by storing the python source code somewhere and 
# during successive executions of compileInline check if the source code is the same as it was during the last call before 
# compiling)
def someFunctionThatGetsCompiledAndCachedAtExecutionTime(var:int, var2:int):
    return someFunctionCalledFromTheCppFunction(var, var2)

theAnswerToTheUniverse:int=someFunctionThatGetsCompiledAndCachedAtExecutionTime(30,2)+somePurePythonFunction(5, 2)
print(f"theAnswerToTheUniverse: {theAnswerToTheUniverse}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions