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

Faster LinkedHashSet head() #2728

Merged

Conversation

j-baker
Copy link
Contributor

@j-baker j-baker commented Nov 29, 2022

While writing up #2727 I noticed that LinkedHashSet.head() is implemented by iterator().head(). This is inefficient because queue.iterator().next() makes a call to .head(), but also to .tail() so as to prepare for the next head() call. Given the structure of the underlying Queue, the head() call is worst case O(1) but the tail call is worst case O(n). The present worst case will be achieved if there have never been overwrites or removals from the set, which is probably a fairly common case.

While writing up vavr-io#2727 I noticed that `LinkedHashSet.head()` is implemented
`iterator().head()`. This is inefficient because `queue.iterator().next()`
makes a call to `.head()`, but also to `.tail()` so as to prepare for the
next `head()` call. Given the structure of the underlying `Queue`, the
`head()` call is worst case `O(1)` but the tail call is worst case
`O(n)`. The present worst case will be achieved if there have never been
overwrites or removals from the set, which is probably a fairly common
case.
Copy link
Member

@danieldietrich danieldietrich left a comment

Choose a reason for hiding this comment

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

Great finding, thank you!
I will prepare a patch release with your latest changes the next days.
Thx

@danieldietrich danieldietrich merged commit f41a6fa into vavr-io:master Jan 3, 2023
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