Early exit for empty index in vec_assign()#1590
Conversation
Yes please, it never hurts to add one more test :-) |
DavisVaughan
left a comment
There was a problem hiding this comment.
I'm actually tempted to put this optimization deep within the assignment macros, like ASSIGN_INDEX(), ASSIGN_COMPACT(), the corresponding BARRIER macros, and the corresponding "shaped" ones for arrays. Otherwise I think it is very hard to get the placement of the optimization right, because we aren't always exactly sure what index is (is it compact, or not?) and we have all kinds of other checks on x/proxy/value that we want to ensure are run before we decide to early exit.
Basically I think the early exit should be done as close to the generation of the copy as possible (i.e. the vec_clone_referenced() call)
|
|
||
| // Cast and recycle `value` | ||
| value = KEEP(vec_cast(value, x, opts.value_arg, opts.x_arg, opts.call)); |
There was a problem hiding this comment.
I think the cast and recycle checks need to run before we can think about early exiting
|
Closing for the reasons explained in #1636 (comment) |
|
Reopening, because I'd like to reconsider in light of tidyverse/tidyr#1392 — the performance gains seem meaningful, and it seems much more reasonable to handle this in vctrs rather than having to wrap every use of |
Closes #1408.
For example this is relevant for
coalesce()like uses, e.g.vec_assign(x, is.na(x), 1L).I only added this optimisation for
vec_assign_opts()and not for other functions inslice-assign.he.g.vec_proxy_assign_opts(). I only sawvec_proxy_assign_opts()being called inunchop()andvec_c()but it don't think one would encounter an empty index there.Do you want an explicit test that
vec_assign(x, integer(), -1L)works? It is currently tested "indirectly" viavec_slice(x, FALSE) <- 2L.Created on 2022-07-15 by the reprex package (v2.0.1)