# Reinterpret to existing vector

**URL:** https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706
**Category:** Performance
**Tags:** question, performance
**Created:** [January 28, 2023, 7:09pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706 "2023-01-28T19:09:22Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![CodeGodz](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@CodeGodz](https://discourse.julialang.org/u/CodeGodz)
#### Post date: [January 28, 2023, 7:09pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/1 "2023-01-28T19:09:22Z")

</div>

Lets say I have a byte (`UInt8`) array that looks like this:

```julia
source = reinterpret(UInt8, Int64[10,11])
 0x0a
 0x00
 0x00
 0x00
....

```

And an existing allocated array of two integers  
`target = zeros(Int64, 2)`

I basically want to say `reinterpret!(target, source)` where the `UInt8` array is reinterpreted as an `Int64` to `target` (So I don’t allocate new vectors). I thought of using `unsafe_store` or `unsafe_copyto!` but didn’t figure it out yet.

What would be the way to go? 😃

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [January 28, 2023, 7:20pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/2 "2023-01-28T19:20:13Z")

</div>

I am no expert on this but if you reinterpret your target you should not allocate anything.  
So this little snippet should work.

```julia
a = reinterpret(UInt8, Int16[10,23])
target = zeros(Int16, 2)
b = reinterpret(UInt8, target)
unsafe_copyto!(pointer(b), pointer(a), min(length(b), length(a)))
println(target)

```

---

<div class="post-metadata">

### Author: ![CodeGodz](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@CodeGodz](https://discourse.julialang.org/u/CodeGodz)
#### Post date: [January 28, 2023, 7:22pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/3 "2023-01-28T19:22:27Z")

</div>

The line  
`b = reinterpret(UInt8, target)`  
Will allocate

```julia
@btime reinterpret(UInt8, target)
92.453 ns (1 allocation: 32 bytes)

```

---

<div class="post-metadata">

### Author: ![DOT](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DOT](https://discourse.julialang.org/u/DOT)
#### Post date: [January 28, 2023, 7:22pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/4 "2023-01-28T19:22:36Z")

</div>

This, for example, doesn’t allocate a new array:

```julia
x = Int64[1 2 3 4 ; 5 6 7 8]
s = reinterpret(UInt8, @view x[2:2,3:3] )
s[1] = 0x0a
x

```

The assignment in the 3rd line modifies the original array.

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [January 28, 2023, 7:25pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/5 "2023-01-28T19:25:10Z")

</div>

Interesting, maybe it allocates the structure `Base.ReinterpretArray`. The original array, however, is changed by assigning to the reinterpreted array so there should be no reallocation of it.

```julia
julia> @time a = zeros(UInt8,20);
  0.000001 seconds (1 allocation: 80 bytes)

julia> @time b = reinterpret(UInt16,a);
  0.000003 seconds (1 allocation: 32 bytes)

julia> @time a = zeros(UInt8,100);
  0.000001 seconds (1 allocation: 160 bytes)

julia> @time b = reinterpret(UInt16,a);
  0.000003 seconds (1 allocation: 32 bytes)

```

---

<div class="post-metadata">

### Author: ![CodeGodz](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@CodeGodz](https://discourse.julialang.org/u/CodeGodz)
#### Post date: [January 28, 2023, 7:28pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/6 "2023-01-28T19:28:20Z")

</div>

This does allocate,

```julia
@btime reinterpret(UInt8, @view x[2:2,3:3] )
115.531 ns (2 allocations: 144 bytes)

```

* * *

Wasn’t really properly doing my benchmark here 😅 (see below)

---

<div class="post-metadata">

### Author: ![DOT](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DOT](https://discourse.julialang.org/u/DOT)
#### Post date: [January 28, 2023, 7:31pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/7 "2023-01-28T19:31:00Z")

</div>

It doesn’t allocate the array. Check this:

```julia
x = collect( 1:2^30 )
@time s = reinterpret(UInt8, @view x[17:2^29] )
length(s)

```

After running this, `s` is a gigantic array – but only a handfull allocations have been performed.

---

<div class="post-metadata">

### Author: ![CodeGodz](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@CodeGodz](https://discourse.julialang.org/u/CodeGodz)
#### Post date: [January 28, 2023, 7:32pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/8 "2023-01-28T19:32:22Z")

</div>

I know it doesn’t allocate the array itself, but it does allocate. Since the memory for the output array (`target`) is already allocated it should be possible to move the bytes there without any allocations. Similar to just copying values from one array to another (which doesn’t allocate at all)

(Also this doesn’t answer the question to interpret a byte array to int, not the other way around)

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [January 28, 2023, 7:44pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/9 "2023-01-28T19:44:26Z")

</div>

If you put it in a function I think it does not allocate anymore as it gets optimized.

```julia
julia> function test!(a,b)
       a_uint8 = reinterpret(UInt8, a)
       b_uint8 = reinterpret(UInt8, b)
       unsafe_copyto!(pointer(b_uint8), pointer(a_uint8), min(length(b_uint8), length(a_uint8)))
       nothing
end

julia> a = rand(UInt8,4)
4-element Vector{UInt8}:
 0x6f
 0x9b
 0x5b
 0x64

julia> b = zeros(UInt16,2)
2-element Vector{UInt16}:
 0x0000
 0x0000

julia> @timev test!(a,b)
  0.000001 seconds
elapsed time (ns): 1380
gc time (ns): 0
bytes allocated: 0
pool allocs: 0
non-pool GC allocs: 0
minor collections: 0
full collections: 0

julia> b
2-element Vector{UInt16}:
 0x9b6f
 0x645b

```

---

<div class="post-metadata">

### Author: ![CodeGodz](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@CodeGodz](https://discourse.julialang.org/u/CodeGodz)
#### Post date: [January 28, 2023, 7:58pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/10 "2023-01-28T19:58:37Z")

</div>

You are right, as `memcpy` also doesn’t allocate that way

```julia
using BenchmarkTools
function test()
    source = reinterpret(UInt8, Int64[10,11])
    target = zeros(Int64, 2)
    @btime ccall(:memcpy, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, Csize_t), $target, $source, length($source))
end
test()

```

---

<div class="post-metadata">

### Author: ![CodeGodz](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@CodeGodz](https://discourse.julialang.org/u/CodeGodz)
#### Post date: [January 28, 2023, 8:01pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/11 "2023-01-28T20:01:35Z")

</div>

Or

```julia
@ccall memcpy(pointer(target)::Ptr{Cvoid}, pointer(source)::Ptr{Cvoid}, length(source)::Cssize_t)::Cvoid

```

A bit neater

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [January 28, 2023, 8:27pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/12 "2023-01-28T20:27:06Z")

</div>

> [@CodeGodz](#):
>
> The line  
> `b = reinterpret(UInt8, target)`  
> Will allocate

This is just an artefact of your benchmarking code. It does not allocate. Benchmark like this

```julia
@btime b = reinterpret(UInt8, $target)

```

---

<div class="post-metadata">

### Author: ![CodeGodz](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@CodeGodz](https://discourse.julialang.org/u/CodeGodz)
#### Post date: [January 28, 2023, 9:17pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/13 "2023-01-28T21:17:33Z")

</div>

Yeah you are right, realized that too 😅, the answer then is actually simply:  
`b = reinterpret(Int64, source)` where I don’t have to fill `target` at all

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [January 28, 2023, 10:18pm UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/14 "2023-01-28T22:18:36Z")

</div>

Would such a thing be acceptable?

```julia
target .= source.parent

```

or

```julia
target = source.parent

```

---

<div class="post-metadata">

### Author: ![CodeGodz](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@CodeGodz](https://discourse.julialang.org/u/CodeGodz)
#### Post date: [January 29, 2023, 12:06am UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/15 "2023-01-29T00:06:16Z")

</div>

Looking at this again, this only works after the reinterpret?

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [January 29, 2023, 6:41am UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/16 "2023-01-29T06:41:33Z")

</div>

Yes. Is not that your case?

---

<div class="post-metadata">

### Author: ![CodeGodz](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@CodeGodz](https://discourse.julialang.org/u/CodeGodz)
#### Post date: [January 29, 2023, 9:55am UTC](https://discourse.julialang.org/t/reinterpret-to-existing-vector/93706/17 "2023-01-29T09:55:50Z")

</div>

I just used the reinterpret to create the example vector, the actual `source` array are just bytes from a file
