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

Automatic expand simple for-loop from a literal container #2777

Open
KmolYuan opened this Issue Jan 2, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@KmolYuan
Copy link

KmolYuan commented Jan 2, 2019

I often use a simple for-loop to operate variables in Python.

for v in [a, b, c]:
    func1(v)
    func2(v)
    func3(v)
    ...

In Cython, it will be more readable when doing None checking or condition checking:

for e in [e1, e2, e3]:
    if e is None or e.type_case != MY_TYPE:
        raise TypeError(...)

The "simple for-loop" needs to converted into expanded expression in C/C++ implementation without to create an actual container object (list, tuple, set) to iterate it.

# Expanded as:
if e1 is None or e1.type_case != MY_TYPE:
    raise TypeError(...)
if e2 is None or e2.type_case != MY_TYPE:
    raise TypeError(...)
if e3 is None or e3.type_case != MY_TYPE:
    raise TypeError(...)

The cases maybe like:

# "a, b, c" must be the same type or object
# If "v" is not typed, try to convert them into object.
# If you use "v" in the external scope, optimization will be disabled.
# If there has "break" or "continue" keywords, optimization will be disabled.
cdef type_of_abc v
for v in [a, b, c]:
    ...
for v in (a, b, c):
    ...
for v in {a, b, c}:
    ...
@KmolYuan

This comment has been minimized.

Copy link

KmolYuan commented Jan 2, 2019

The case with enumerate or other counter variable:

cdef int i
cdef int counter = 5
cdef type_of_abc v
for i, v in enumerate([a, b, c]):
    func(i, v)
    counter += 2

# Expanded as:
cdef int i
cdef int counter = 5
i = 0
func(i, a)
counter += 2
i += 1
func(i, b)
counter += 2
i += 1
func(i, c)
counter += 2
@scoder

This comment has been minimized.

Copy link
Contributor

scoder commented Jan 2, 2019

Related to, if not a duplicate of, #2117. The potential gain is likely small, but it would avoid a tuple creation for non-constant values.

I don't really see an advantage in unrolling the loop in Cython. Once the C compiler can see that this is a loop over a C array, it can take that decision much better than Cython. The only exception is the case where a/b/c have very different types, especially native C types, so that Cython could specialise the code for each of them. However, given that the code uses a loop, this seems not so likely (to me).

@scoder scoder added the unclear label Jan 11, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment