I have a quite trivial question. I have read large arrays of bytes from different source into a single array (sometimes 2e9 bytes and more) and now I need to make it pretty for the user and thought utilising reinterpret
using the corresponding struct
, which is basically a nop.
My problem is: packed data. The structures I deal with are completely mixed and their on-disk representation is packed. I looked into some older packages (StrPack.jl
, StructIO.jl
, etc.) which I also use for other projects but could not find a way to reinterpret the data without any copy or new allocation.
A dummy example:
data = [0x01, 0x00, 0x10, 0x02, 0x00, 0x20]
struct Foo
x::UInt8
y::UInt16
end
reinterpret(Foo, data)
gives of course
ArgumentError: cannot reinterpret an `UInt8` array to `Foo` whose first dimension has size `6`.
The resulting array would have non-integral first dimension.
Since the memory representation of this structure is unpacked and includes padding.
It would work if I had the padding bytes (I marked them with 0xFF
) which I obviously don’t have
data = [0x01, 0xFF, 0x00, 0x10, 0x02, 0xFF, 0x00, 0x20]
reinterpret(Foo, data)
2-element reinterpret(Foo, ::Array{UInt8,1}):
Foo(0x01, 0x1000)
Foo(0x02, 0x2000)
Am I hitting the wall here or is there some (dirty) way to create an actual structure in Julia which I can map over my in-memory data?
Btw. I also tried to unpack the data already while reading it from disk but 1. it’s a bit tricky since data can be split and 2. I hit huge drops in performance when doing so (100x and more). Let alone it’s quite ugly…