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 ListExtensions.firstOrNull, lastOrNull, singleOrNull. #53756

Open
scheglov opened this issue Oct 14, 2023 · 3 comments
Open

Add ListExtensions.firstOrNull, lastOrNull, singleOrNull. #53756

scheglov opened this issue Oct 14, 2023 · 3 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-collection library-core type-enhancement A request for a change that isn't a bug

Comments

@scheglov
Copy link
Contributor

See dart-archive/collection#312

For List we don't have to instantiate Iterator to implement these methods.

@scheglov scheglov added the area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. label Oct 14, 2023
@lrhn
Copy link
Member

lrhn commented Oct 14, 2023

We could also let the existing operation on Iterable check for whether the Iterable has efficient length. If it has, it can do the same length checks as here, also for sets and queues, then use first/last on the Iterable for the value.

The lastOrNull code already does this.

@scheglov
Copy link
Contributor Author

From performance perspective this seems bad. If we statically know that it is a List, why should we lose this information and invoke a more generic version, that then checks for the type to recover the lost information? Or do we know that VM will inline such methods, and rewrite by leaving only the branch for List types, practically removing the type check?

@lrhn
Copy link
Member

lrhn commented Oct 14, 2023

I don't think we know that anything will be inlined.
The extension on List will clearly be better, but will not work for Set or Queue, or on a list which is not statically known to be a list.

So we could do both.

@lrhn lrhn added library-core library-collection type-enhancement A request for a change that isn't a bug labels Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-collection library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants