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

Add type identifiers to loop variables and define functions with cpdef #750

Closed
Joao-Dionisio opened this issue Nov 14, 2023 · 1 comment
Closed

Comments

@Joao-Dionisio
Copy link
Collaborator

In many cases Cython cannot infer the type of the looping variable (for example, when the looping variable is used as an index). In these cases, Cython will run the loop as a Python loop. Adding type identifiers allows Cython to run the loop as a C loop, which is much faster. With methods that are called often (for example, getVars inside a pricer), this could make a noticeable difference. We just need to be careful about potential integer overflows.

In getVars we have the following code:

for i in range(_nvars):
            ptr = <size_t>(_vars[i])

            # check whether the corresponding variable exists already
            if ptr in self._modelvars:
                vars.append(self._modelvars[ptr])
            else:
                # create a new variable
                var = Variable.create(_vars[i])
                assert var.ptr() == ptr
                self._modelvars[ptr] = var
                vars.append(var)

Simply adding cdef int i at the beginning of the code would achieve the desired result.

Probably less importantly, PySCIPOpt's code defines functions using the def command, meaning that it's creating a Python function. Calling these functions incurs some overhead. Using cpdef allows the functions to be used as Python functions from the outside but with the speed of C functions. We need to properly type the arguments of the function, but we should already be doing that (see PR #730). (A caveat of cpdef is that the return type and the argument types must be valid for both Python and C)

The impact shouldn't be very big, since once we're inside the functions, both are the same, but there is no reason not to do it really, and it might have some marginal impact when the user has a loop that calls a function often.

@Joao-Dionisio
Copy link
Collaborator Author

cpdef probably won't have much of an impact, and typing loop variables falls under Issue #730, so I'm closing this as a duplicate.

@Joao-Dionisio Joao-Dionisio closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant