Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ItsCubeTime opened this issue Nov 9, 2024 · 0 comments

Comments

@ItsCubeTime
Copy link

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}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant