-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Simplify ArrayIterator to a cached length and a position counter #21598
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these tests preserve accidentally existing behavior (found by @johanrd ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -225,42 +225,29 @@ class IteratorWrapper implements OpaqueIterator { | |
| } | ||
|
|
||
| class ArrayIterator implements OpaqueIterator { | ||
| private current: { kind: 'empty' } | { kind: 'first'; value: unknown } | { kind: 'progress' }; | ||
| private pos = 0; | ||
| private pos = -1; | ||
| private empty: boolean; | ||
|
|
||
| constructor( | ||
| private iterator: unknown[] | readonly unknown[], | ||
| private keyFor: KeyFor | ||
| ) { | ||
| if (iterator.length === 0) { | ||
| this.current = { kind: 'empty' }; | ||
| } else { | ||
| this.current = { kind: 'first', value: iterator[this.pos] }; | ||
| } | ||
| // Only this read runs in the tracking frame of the iterator reference. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is kinda "womp" |
||
| this.empty = iterator.length === 0; | ||
| } | ||
|
|
||
| isEmpty(): boolean { | ||
| return this.current.kind === 'empty'; | ||
| return this.empty; | ||
| } | ||
|
|
||
| next(): Nullable<IterationItem<unknown, number>> { | ||
| let value: unknown; | ||
|
|
||
| let current = this.current; | ||
| if (current.kind === 'first') { | ||
| this.current = { kind: 'progress' }; | ||
| value = current.value; | ||
| } else if (this.pos >= this.iterator.length - 1) { | ||
| return null; | ||
| } else { | ||
| value = this.iterator[++this.pos]; | ||
| } | ||
| let memo = ++this.pos; | ||
|
|
||
| let { keyFor } = this; | ||
| // The length is live because code in the block can change a plain array. | ||
| if (memo >= this.iterator.length) return null; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this implicit behavior we had here (and continue to have here): entangling with length, if it's tracked, could be why some of our iteration/updates is slow. I need to think about how we can have a better list update/create/destroy algo in general, but that'll be a much bigger PR |
||
|
|
||
| let key = keyFor(value, this.pos); | ||
| let memo = this.pos; | ||
| let value = this.iterator[memo]; | ||
|
|
||
| return { key, value, memo }; | ||
| return { key: this.keyFor(value, memo), value, memo }; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated cleanup for easier debugging with breakpoints