reduce: unalias arrays for hcat/vcat (map)reduce #58490
Open
+8
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was using
reduce(vcat, list_of_arrays)
and then modifying the resulting array. This worked great, except that in the case whenlength(list_of_arrays) == 1
, the resulting array would alias the input, resulting in the input being modified and causing havoc down the road. Of course the proper fix for this is better built-in support for immutable arrays (and possibly memory ownership) to encode those assumptions explicitly. However, while this hack is unsatisfying, I do think it is somewhat defensible. One argumenthcat
andvcat
perform the copy, as do the various optimized special cases for these functions. Of course, this is fragile and would break as soon as someone puts it in a closure, but maybe it saves at least a little bit of headache.I considered a more aggressive change to make the generic fallback
reduce_first(op, x) = op(x)
, but in discussion with Jeff, this was deemed too breaking, since it would add an assumption thatop
has a one-arg form.