Is it possible to reinterpret and reshape without allocating?

AwesomeQuest
Thu, 12 Jun 2025 17:21:03 +0000
Performance

So I have data I’m getting over HTTP,

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:

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?

12 posts - 4 participants