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

Champ (Map/Set) Iterators should expose their knownSize #11606

Open
joshlemer opened this issue Jul 2, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@joshlemer
Copy link
Member

commented Jul 2, 2019

All iterators over the CHAMP Maps/Sets currently expose a knownSize of -1. Since Maps/Sets cache their size, it should at the very least be possible to track how many calls to next() have occurred, and decrement a counter. Or if that is too inconvenient (since there are many overrides of next() for each variant of the Iterator), then perhaps atleast return rootNode.size in the case where the Iterator is still in starting position.

scala> import collection.immutable._
import collection.immutable._

scala> HashMap(1 -> 1, 2 -> 2).iterator.knownSize
res1: Int = -1

scala> HashSet(1,2).iterator.knownSize
res2: Int = -1
@dwijnand

This comment has been minimized.

Copy link
Member

commented Jul 2, 2019

(-.-)

@NthPortal

This comment has been minimized.

Copy link

commented Jul 5, 2019

it's not clear to me how much effort should be put into making iterators know their size, especially if only before they're iterated over

@joshlemer

This comment has been minimized.

Copy link
Member Author

commented Jul 5, 2019

@NthPortal I'm just thinking that in the vast majority of cases, knownSize is called on a fresh iterator in order to preallocate or short circuit, and then never called again, so if it's too costly to maintain a count throughout the iteration, we'll still get almost all the benefits by knowing the size at the start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.