# Is it possible to reinterpret and reshape without allocating?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842>\
**Category:** Performance\
**Tags:** performance, memory-allocation\
**Created:** [June 12, 2025, 5:21pm UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842 "2025-06-12T17:21:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [June 12, 2025, 5:21pm UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/1 "2025-06-12T17:21:03Z")

</div>

So I have data I’m getting over HTTP,

```julia
r = rand(UInt8, 847296)

```

It is arraigned in 3 8 byte chunks, where the first chunk is unwanted metadata, so every 3n -2 chunks is unwanted.  
I have written a function that does this:

```julia
function parseRawData(data)
    data = @view reinterpret(Float64, data)[(3:length(data)÷8+2) .% 3 .!= 0]
    return reshape(data, 2, length(data)÷2)
end

julia> @time parseRawData(r)
  0.000224 seconds (8 allocations: 564.811 KiB)

```

But this allocates 0.55MB. Is it possible to do this while reusing all the memory of the original vector with minimal allocations?

I’ve heard that it’s bad to try to access discontinuous memory like this so is that why it can’t be done much better?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 12, 2025, 5:24pm UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/2 "2025-06-12T17:24:56Z")

</div>

> [@AwesomeQuest](#):
>
> `(3:length(data)÷8+2) .% 3 .!= 0`

This allocates an array.

---

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [June 12, 2025, 5:27pm UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/3 "2025-06-12T17:27:16Z")

</div>

Yes, but only a small one compared to the 0.5MB one

```julia
julia> @time (3:length(r)÷8+2) .% 3 .!= 0
  0.000204 seconds (10 allocations: 13.194 KiB)

```

Is it possible to do this without allocating?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 12, 2025, 5:30pm UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/4 "2025-06-12T17:30:08Z")

</div>

This code returns the same result but does no allocations:

```julia
function parseRawData2(data)
    d = reshape(reinterpret(Float64, data), 3, :)
    return @view d[2:3,:]
end

```

Views involving logical indexing (arrays of booleans) are much less efficient to work with than range slices like `2:3`. (I also think `parseRawData2` is clearer.)

---

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [June 12, 2025, 5:48pm UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/5 "2025-06-12T17:48:19Z")

</div>

Wow! That’s surprising. Very cool.  
So I should always try to avoid logical indexing in favor of finding a pure `@view` slice that does the same thing?  
What about stacking views if your criteria get more complex?

Is there a point where this stops being worth it? (other than the code becoming unreadable)

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [June 12, 2025, 6:12pm UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/6 "2025-06-12T18:12:06Z")

</div>

Basically, logical indexing on the view is better than a view that’s created with logical indexing. The hard thing is that a view has to behave as if it was continuous, so if it’s constructed over disjoint indexes it has to do some work

---

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [June 12, 2025, 8:13pm UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/7 "2025-06-12T20:13:39Z")

</div>

Can you recommend something to read on the subject? Seems really interesting.

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [June 12, 2025, 9:36pm UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/8 "2025-06-12T21:36:41Z")

</div>

You can look at the subarray.jl file which is the source for how views are implemented.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [June 12, 2025, 10:27pm UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/9 "2025-06-12T22:27:12Z")

</div>

> [@AwesomeQuest](#):
>
> So I have data I’m getting over HTTP,

If you are not working with live data then i think you may get better speed performance if you download data and then analyze it offline.

---

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [June 12, 2025, 10:37pm UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/10 "2025-06-12T22:37:44Z")

</div>

What do you mean download? Isn’t that what I’m doing when I do a `HTTP.get(url)`?

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [June 13, 2025, 9:04am UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/11 "2025-06-13T09:04:08Z")

</div>

Yes, You download using http but you connect and disconnect with url many times so this may increase time. Simply try my suggestion. It may increase speed. Earlier I was also working on online data and in my case I saw too much speed 🚄 performance.

---

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [June 13, 2025, 9:18am UTC](https://discourse.julialang.org/t/is-it-possible-to-reinterpret-and-reshape-without-allocating/129842/12 "2025-06-13T09:18:28Z")

</div>

So you mean like opening a web-socket?
