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

Optimized for loop & overflow #2130

Closed
jvasileff opened this issue Apr 15, 2015 · 10 comments
Closed

Optimized for loop & overflow #2130

jvasileff opened this issue Apr 15, 2015 · 10 comments
Assignees
Labels
Milestone

Comments

@jvasileff
Copy link
Member

Neither of these produce output:

    void spanTest(Integer min) {
        variable value x=0;
        for (i in min..runtime.maxIntegerValue) {
            print(i);
            if (x++ > 2) {
                break;
            }
        }
    }
    spanTest(-2);
    void measureTest(Integer min) {
        variable value x=0;
        for (i in min:runtime.maxIntegerValue) {
            print(i);
            if (x++ > 2) {
                break;
            }
        }
    }
    measureTest(1);

This relates to ceylon/ceylon.language/issues/656.

@tombentley
Copy link
Member

Fixed for iteration over measures. Spans next.

@tombentley
Copy link
Member

Optimizing span iteration turns out to be much harder. Specifically getting a C-style for loop to behave the same as the iterator would boils down to replicating the same logic as Span.iterator() and Span.By for increment and loop exit conditions. An additional problem is by() special cases step==1 and the overflow behaviour of Span.iterator() and Span.By.iterator() are different. This means that to generate correct code for for (i in (start..end).by(step)) the loop condition and increment depend on the step. I'm effectively teaching the compiler how to inline all the code used for the iterator.

@gavinking
Copy link
Member

@tombentley Well, yes, but the difference is you don't need to box the longs.

tombentley added a commit that referenced this issue Apr 22, 2015
…e consistent with the unoptimized version even in edge cases like overflow.
tombentley added a commit that referenced this issue Apr 22, 2015
@tombentley
Copy link
Member

Still a couple of edge cases to fix here, but I need to work on something else today.

@gavinking
Copy link
Member

@tombentley good ;)

tombentley added a commit that referenced this issue Apr 24, 2015
@jvasileff
Copy link
Member Author

I'm guessing related to this work, I'm getting:

    print(3 in 0:3); //true
    print((0:3).contains(3)); //false

tombentley added a commit that referenced this issue Apr 29, 2015
…use length > 0 && start < x && x < start.neighbour(length) because that's not what Measure actually does so it doesn't behave correctly wrt overflow.
tombentley added a commit to ceylon/ceylon.language that referenced this issue Jun 16, 2015
…eed it for optimized measure loops on the JVM
tombentley added a commit to ceylon/ceylon.language that referenced this issue Jun 16, 2015
…r because in the case where ++ throws it complicates optimization to a C-style for loop.
tombentley added a commit that referenced this issue Jun 16, 2015
tombentley added a commit that referenced this issue Jun 16, 2015
… cases by == 1 to avoid incorrect results in situations of overflow.
tombentley added a commit that referenced this issue Jun 16, 2015
tombentley added a commit that referenced this issue Jun 16, 2015
@tombentley
Copy link
Member

I think this is fixed with the exception of those Span affected by ceylon/ceylon.language#717, where the unoptimized loop never executes because the Span constructor throws, but the optimized loop is executing at least once. I suppose I can add a check for those cases, but I'll wait to see if people care about fixing ceylon/ceylon.language#717 first.

@tombentley
Copy link
Member

Add immediately after I made that comment I find something else which is broken.

@tombentley
Copy link
Member

I have at this point concluded we're flogging a dead horse in trying to squeeze the span iteration optimization into a for loop, and that a do/while loop is the much more natural construct, though it will complicate the transformation of continue directives with the iterated block.

tombentley added a commit to ceylon/ceylon.language that referenced this issue Jun 18, 2015
…next in the same invocation as we return it. Previously the current invocation computed the next invocations result. That produced incorrect results when that computation overflowed (e.g. iterating up to the last Character).
tombentley added a commit that referenced this issue Jun 18, 2015
tombentley added a commit that referenced this issue Jun 18, 2015
tombentley added a commit that referenced this issue Jun 18, 2015
tombentley added a commit that referenced this issue Jun 18, 2015
tombentley added a commit that referenced this issue Jun 18, 2015
tombentley added a commit that referenced this issue Jun 18, 2015
…definitely entered, so we don't need the flow appeaser. But we do need to omit the post-body statements if we know the body returns.
tombentley added a commit that referenced this issue Jun 18, 2015
@tombentley
Copy link
Member

Fixed, finally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants