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

Cheat to speedup using nqp::iterator+nqp::shift on a list #1809

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MasterDuke17
Copy link
Contributor

We can assume the backing array is in a good state so reach in directly during iteration.

time MVM_SPESH_BLOCKING=1 nqp -e 'my $l := nqp::list(); my int $p := 0; while $p++ < 10_000 { nqp::push($l, $p) }; my int $j := 10000; while $j-- { my $iter := nqp::iterator($l); nqp::while($iter, my $a := nqp::shift($iter)) }' drops from ~1.46s to ~1.25s and time MVM_SPESH_BLOCKING=1 nqp -e 'my $l := nqp::list_s(); my int $p := 0; while $p++ < 10_000 { nqp::push_s($l, $p) }; my int $j := 10000; while $j-- { my $iter := nqp::iterator($l); nqp::while($iter, my $a := nqp::shift_s($iter)) }' drops from ~2.01s to ~1.83s.

@MasterDuke17
Copy link
Contributor Author

I noticed this when looking at Raku/nqp#685.

@niner
Copy link
Contributor

niner commented May 19, 2024

Why can we assume that the array is in a good state?

@MasterDuke17
Copy link
Contributor Author

Why can we assume that the array is in a good state?

Well, I believe we’re allowed to act as if it’s in a good state. Modifying an array during iteration is not allowed AIUI. Also, the iterator keeps track of the index compared to the size of the array.

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

Successfully merging this pull request may close these issues.

None yet

2 participants