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 Iterators and use them for for #3814

Merged
merged 3 commits into from
Feb 28, 2016

Conversation

est31
Copy link
Contributor

@est31 est31 commented Feb 24, 2016

Fixes #3765 . I believe this is a great addition and speedup for GDscript.

The PR does three things, in three commits:

  1. Integrate commit by @sheepandshepherd to add a RangeIterator class. Many thanks to @sheepandshepherd for writing the class!
  2. Add a built in xrange function similar to the one in Python 2, and document the function
  3. Replace range with xrange for for loops. This is guaranteed to be 100% backwards compatible, unless you relied on for i in range(...) to allocate memory xD... I'm not really proud of how I hacked the parser to do it but this is most conservative regarding backwards compatibility. Note that the GDParser::_reduce_expression method does stuff like this as well, so I guess its fine. It catches 99% of all uses. Or do you know somebody who assigns range to a variable and then loops over the variable?

@est31 est31 force-pushed the iterators_for_for branch from 2b8db6b to a4cf060 Compare February 24, 2016 02:02
@est31 est31 force-pushed the iterators_for_for branch 2 times, most recently from 80a6e9d to 011af15 Compare February 24, 2016 02:18
@est31 est31 changed the title Iterators for for Add Iterators and use them for for Feb 24, 2016

int count=*p_args[0];

RangeIterator *itr = memnew(RangeIterator);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure -- is this the right way to allocate? It works, that's great, but does it leak memory?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It leaks, yes. I made it a Reference because I couldn't find any convenient location to call memdelete. To fix it, replace the allocations with Refs:

Ref<RangeIterator> itr = Ref<RangeIterator>( memnew(RangeIterator) );

@est31 est31 force-pushed the iterators_for_for branch 2 times, most recently from 01596c9 to 7a08792 Compare February 24, 2016 02:53
@sheepandshepherd
Copy link
Contributor

I like the range optimization, actually. Replacing GEN_RANGE with GEN_XRANGE in for loops is a clean way of using the iterator and staying backwards-compatible.

One more thing -- the first RangeIterator had some naming convention mistakes and other issues that I meant to fix in a second commit. You might want to add it, but it does rename _set_range to set_range in C++ (the one with the underscore was supposed to be the GDScript version with Variants).

@est31 est31 force-pushed the iterators_for_for branch from 7a08792 to e1b7beb Compare February 24, 2016 04:32
@est31
Copy link
Contributor Author

est31 commented Feb 24, 2016

Thanks, the new commit is now added.

I've squashed two commits, to keep the history clean.

@bojidar-bg
Copy link
Contributor

👍

@alketii
Copy link

alketii commented Feb 28, 2016

Wow, this is awesome, please merge.

@est31 est31 force-pushed the iterators_for_for branch from e1b7beb to d2e1854 Compare February 28, 2016 21:44
sheepandshepherd and others added 3 commits February 28, 2016 22:47
Also update classes.xml in order to document xrange
Make the parser eliminate a wasteful allocation and initialisation
of a possibly large array.
@est31 est31 force-pushed the iterators_for_for branch from d2e1854 to 5f66692 Compare February 28, 2016 21:47
akien-mga added a commit that referenced this pull request Feb 28, 2016
Add Iterators and use them for for
@akien-mga akien-mga merged commit adf5056 into godotengine:master Feb 28, 2016
@reduz
Copy link
Member

reduz commented Mar 1, 2016

Please revert this, let me review it properly first

@reduz
Copy link
Member

reduz commented Mar 1, 2016

Also please don't merge anything related to GDScript without me reviewing it first

akien-mga added a commit to akien-mga/godot that referenced this pull request Mar 1, 2016
…for"

This reverts commit adf5056, reversing
changes made to ee2bc87.
akien-mga added a commit that referenced this pull request Mar 1, 2016
Revert "Merge pull request #3814 from est31/iterators_for_for"
@akien-mga
Copy link
Member

Reverted by #3894.

@vnen
Copy link
Member

vnen commented Jul 12, 2016

@est31 could this code be sent as a new PR? I think it's a nice addition and the code does not seem bad. It might be forgotten here as a merged PR.

This would also be a start for list comprehension asked in #4716.

@est31
Copy link
Contributor Author

est31 commented Jul 12, 2016

@vnen the code does not look bad, I agree, but it turns out to be slower, by a large factor. That's why it was reverted.

@sheepandshepherd
Copy link
Contributor

The overhead of calling an Object's _iter_next method for each iteration is one reason this way is much slower (here, in variant_op.cpp), compared to the really simple code for Array a few lines below that.

The only way this iterator could be as fast as an Array is if we modify the for loop to call its _iter_next function from C++ instead of through GDScript. I haven't looked at how the GDScript for loop works though, so I don't know if this is possible.

@bojidar-bg
Copy link
Contributor

@sheepandshepherd Calling the function directly would essentially break custom iterators built in GDScript.

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

Successfully merging this pull request may close these issues.

7 participants