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

RFC: Promote result array in map #6548

Closed
wants to merge 5 commits into from
Closed

RFC: Promote result array in map #6548

wants to merge 5 commits into from

Conversation

MikeInnes
Copy link
Member

I've added promotion to map – if the next f(x) doesn't fit into the result array, the array type is promoted. This will fix things like map(join, ["z", "я"]). Equally, functions that can return Ints and Floats will promote to Float rather than trying to cast everything to Int (and producing InexactErrors).

Performance in cases that already work should be largely unaffected; in almost all other cases, the promotion will only happen once. #210 still needs fixing for ideal performance, but at least this way map is functional in both senses.

(The worst case performance hit – for a small function over an Int array – is about 2x.)

In order to make this work I had to extend copy! to support undefined values, as well as making it more flexible so that it gets called instead of the generic copy!(dest::AbstractArray, src).

By the way, why are there map definitions for 1, 2, and multiple args? Wouldn't the N arg version suffice?

@MikeInnes MikeInnes changed the title WIP: Promote result array in map RFC: Promote result array in map Apr 16, 2014
@MikeInnes MikeInnes changed the title RFC: Promote result array in map Promote result array in map Apr 16, 2014
@porterjamesj
Copy link
Contributor

+1 to this if it doesn't impact perf, the current behavior is really confusing.

@ihnorton ihnorton added decision and removed decision labels May 5, 2014
@MikeInnes MikeInnes changed the title Promote result array in map RFC: Promote result array in map May 19, 2014
@JeffBezanson
Copy link
Member

This kind of thing is clearly an improvement. However, this approach might copy the array more than once, which is not necessary at all. We can accumulate into an Any array, all the while reducing promote_type over the result types, then finally copy if the type is smaller than Any.

Although map_to! is currently only used by map, in theory it should just do what it's told and put the output in the provided array. But for this change we could just remove it entirely.

@JeffBezanson
Copy link
Member

I have a new implementation on the way.

@MikeInnes
Copy link
Member Author

Fair, I was thinking that making no copies most of the time might be worth it, but that's probably not the right approach.

Any chance you could have a look at #6547 while you're at it? Either way it's great to see this being fixed, so thanks!

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.

4 participants