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 Enumerable(T)#to_a(& : T -> U) forall U #12653

Merged

Conversation

caspiano
Copy link
Contributor

Analogous to Enumerable(T)#(& : T -> Tuple(K, V)) forall K, V except producing an Array from the block's result.

Resolves #12643

Analogous to `Enumerable(T)#(& : T -> Tuple(K, V)) forall K, V` except
producing an Array from the block's result.

Resolves crystal-lang#12643
src/enumerable.cr Outdated Show resolved Hide resolved
@HertzDevil
Copy link
Contributor

HertzDevil commented Oct 24, 2022

Indexable could override this to pre-allocate the returned array with capacity same as size. (This does not improve performance in all cases but is usually a right thing to do, like array literals.)

Also it is probably worth documenting that Array#to_a doesn't create a copy but Array#to_a(&) does.

@asterite
Copy link
Member

Indexable could override this to pre-allocate the returned array with capacity same as size.

Strange, that's what Array already does. I'm surprised when Indexable was introduced this optimization wasn't also done.

This does not improve performance in all cases

Why not?

@HertzDevil
Copy link
Contributor

If you immediately insert an element to the returned array then a reallocation is guaranteed to happen and that is less performant than the default reallocation strategy on average. This is why we didn't pursue #10075

src/enumerable.cr Outdated Show resolved Hide resolved
src/hash.cr Outdated Show resolved Hide resolved
@straight-shoota straight-shoota self-assigned this Dec 12, 2023
@straight-shoota straight-shoota added this to the 1.11.0 milestone Dec 22, 2023
@straight-shoota straight-shoota merged commit e00a0a4 into crystal-lang:master Dec 24, 2023
56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Enumerable(T)#to_a(& : T -> U) forall U
4 participants