I am parsing some data from file where one signal is composed or couple UInt8
glued together. I want to separate them into individual signals, so I did the following:
mutable struct Status
one::Vector{<:Integer}
two::Vector{<:Integer}
three::Vector{<:Integer}
end
This is embedded into a bigger struct, everything is mutable. Then I have a code that initializes this struct and fills it up in a loop value by value.
function Status(length)
one = Vector{UInt8}(undef, length)
two = Vector{UInt8}(undef, length)
three = Vector{UInt8}(undef, length)
return Status(one, two, three)
end
function parse_status!(input, status, idx)
status.one[idx] = input[3*idx+1]
status.two[idx] = input[3*idx+2]
status.three[idx] = input[3*idx+3]
end
sts = Status(20)
for i in 1:20
parse_status!(input, sts, i)
end
This leads to allocations, curiously of type Int64
although all data is UInt8
.
When I try this code outside of the whole context, it seems to not allocate (the initialzed struct is being passed into a chain of functions and at one point filled with the parse_status
function).
I thought that maybe it has something to do with Julia not figuring out the types of containers, so I tried different ways to make it more explicit and making the Status
struct parametric removed the allocations.
mutable struct Status{T<:Integer}
one::Vector{T}
two::Vector{T}
three::Vector{T}
end
while I don’t mind parametrizing it, I don’t really need it for anything else.
So I am wondering - have I correctly identified the original problem? And is this solution reasonable (are there any downsides to parametrizing structs)?