# Extract an \`AbstractVector{T}\` from an \`AbstractVector{T, Missing}\`

**URL:** https://discourse.julialang.org/t/extract-an-abstractvector-t-from-an-abstractvector-t-missing/79698
**Category:** Performance
**Created:** [April 19, 2022, 3:43pm UTC](https://discourse.julialang.org/t/extract-an-abstractvector-t-from-an-abstractvector-t-missing/79698 "2022-04-19T15:43:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)
#### Post date: [April 19, 2022, 3:43pm UTC](https://discourse.julialang.org/t/extract-an-abstractvector-t-from-an-abstractvector-t-missing/79698/1 "2022-04-19T15:43:58Z")

</div>

I know that a certain subset of the indices of an `AbstractVector{T, Missing}` are not missing. I want to efficiently manipulate that subset.

How can I extract `x::AbstractVector{T}` from `y::AbstractVector{T, Missing}` such that `x[i] === y[i]` whenever `y[i] !== missing` with minimal runtime overhead at creation and at use?

I am okay with undefined behavior if I attempt to access a missing element.

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 19, 2022, 4:09pm UTC](https://discourse.julialang.org/t/extract-an-abstractvector-t-from-an-abstractvector-t-missing/79698/2 "2022-04-19T16:09:44Z")

</div>

You’ll probably have something like

```julia
function extract!(avec::AbstractVector{T}, avecm::AbstractVector{Union{T, Missing}}, is) where {T}
    for i in is
        avec[i] = avecm[i]
    end
    avec
end

function extract(avecm::AbstractVector{Union{T, Missing}}, is)::AbstractVector{T} where {T}
    avec = similar(avecm, T, length(avecm));
    extract!(avec, avecm, is)
end

@btime extract!(vec, vecm, is) setup=(
    vecm = Vector{Union{Int, Missing}}(undef, 10);
    is = [1, 3, 5, 7, 9];
    vecm[is] .= is;
    vec = similar(vecm, Int, length(vecm));
)

@btime extract(vecm, is) setup=(
    vecm = Vector{Union{Int, Missing}}(undef, 10);
    is = [1, 3, 5, 7, 9];
    vecm[is] .= is;
)

```

as a baseline already, yielding

```julia
  8.000 ns (0 allocations: 0 bytes)
  34.340 ns (1 allocation: 144 bytes)

```

---

<div class="post-metadata">

### Author: ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)
#### Post date: [April 19, 2022, 5:44pm UTC](https://discourse.julialang.org/t/extract-an-abstractvector-t-from-an-abstractvector-t-missing/79698/3 "2022-04-19T17:44:53Z")

</div>

This is nice, but I’m looking for something substantially faster. I would like to avoid allocating and copying data at all and have a constant runtime with respect to input length. I think this should be possible, at least in the case of `Vector{Union{T, Missing}}`, because `Vector{Union{T, Missing}}` is stored internally as a vector of data and a vector of bits representing whether each element is missing. I just want to get a handle on the internal Vector of data.

I’m looking for something like `x = reinterpret(T, y)`.

---

<div class="post-metadata">

### Author: ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)
#### Post date: [April 19, 2022, 6:00pm UTC](https://discourse.julialang.org/t/extract-an-abstractvector-t-from-an-abstractvector-t-missing/79698/4 "2022-04-19T18:00:46Z")

</div>

For `Array`s, I think this might be safe:

```julia
without_missing(avecm::Array{Union{T, Missing}}) where T = 
    unsafe_wrap(Array, reinterpret(Ptr{Int}, pointer(avecm)), size(avecm))

@btime without_missing(v) setup=(
    v = Vector{Union{Int, Missing}}(undef, 100000);
    is = 1:90000;
    v[is] .= is;
);
# 35.259 ns (2 allocations: 64 bytes)

```

But for `AbstractArrays`, I still don’t know.
