This package allows to combine multiple heterogeneous types in a single one. This helps to write type-stable code by avoiding Union-splitting, which has big performance drawbacks when many types are unionized. A second aim of this library is to provide a syntax as similar as possible to standard Julia structs to help integration within other libraries.
Two macros implement different strategies to create a compact representation of the types: @compact_structs
and
@sum_structs
.
Both work very similarly, but there are some differences:
-
@compact_structs
is faster; -
@sum_structs
is more memory efficient and allows to mix mutable and immutable structs where fields belonging to different structs can also have different types, it uses SumTypes.jl under the hood.
Even if there is only a unique type defined by these macros, you can access a symbol containing the conceptual type
of an instance with the function kindof
.
julia> using MixedStructTypes
julia> abstract type AbstractA{X} end
julia> @sum_structs A{X} <: AbstractA{X} begin
@kwdef mutable struct B{X}
a::X = 1
b::Float64 = 1.0
end
@kwdef mutable struct C
a::Int = 2
c::Bool = true
end
@kwdef mutable struct D
a::Int = 3
const d::Symbol = :s
end
@kwdef struct E{X}
a::X = 4
end
end
julia> b = B(1, 1.5)
B{Int64}(1, 1.5)::A
julia> b.a
1
julia> b.a = 3
3
julia> kindof(b)
:B
julia> abstract type AbstractF{X} end
julia> # as you can see, here, all structs are mutable
# and all shared fields in different structs have
# the same type
@compact_structs F{X} <: AbstractF{X} begin
@kwdef mutable struct G{X}
a::X = 1
b::Float64 = 1.0
end
@kwdef mutable struct H{X}
a::X = 2
c::Bool = true
end
@kwdef mutable struct I{X}
a::X = 3
const d::Symbol = :s
end
@kwdef mutable struct L{X}
a::X = 4
end
end
julia> g = G(1, 1.5)
G{Int64}(1, 1.5)::F
julia> g.a
1
julia> g.a = 3
3
julia> kindof(g)
:G
There are currently two ways to define function on the types created with this package:
- Use manual branching;
- Use the
@dispatch
macro.
For example, let's say we want to create a sum function where different values are added depending on the kind of each element in a vector:
julia> v = A{Int}[rand((B,C,D,E))() for _ in 1:10^6];
julia> function sum1(v) # with manual branching
s = 0
for x in v
if kindof(x) === :B
s += value_B()
elseif kindof(x) === :C
s += value_C()
elseif kindof(x) === :D
s += value_D()
elseif kindof(x) === :E
s += value_E()
else
error()
end
end
return s
end
sum1 (generic function with 1 method)
julia> value_B() = 1;
julia> value_C() = 2;
julia> value_D() = 3;
julia> value_E() = 4;
julia> function sum2(v) # with @dispatch macro
s = 0
for x in v
s += value(x)
end
return s
end
sum2 (generic function with 1 method)
julia> @dispatch value(::B) = 1;
julia> @dispatch value(::C) = 2;
julia> @dispatch value(::D) = 3;
julia> @dispatch value(::E) = 4;
julia> sum1(v)
2499517
julia> sum2(v)
2499517
As you can see the version using the @dispatch
macro is much less verbose and more intuitive. In some more
advanced cases the verbosity of the first approach could be even stronger.
Since the macro essentially reconstruct the branching version described above, to ensure that everything works correctly
when using it, do not define functions operating on the main type of a mixed struct without using the @dispatch
macro.
Consult the API page for more information on the available functionalities.
Let's see briefly how the two macros compare performance-wise in respect to a Union
of types:
julia> @kwdef mutable struct M{X}
a::X = 1
b::Float64 = 1.0
end
julia> @kwdef mutable struct N{X}
a::X = 2
c::Bool = true
end
julia> @kwdef mutable struct O{X}
a::X = 3
const d::Symbol = :s
end
julia> @kwdef mutable struct P{X}
a::X = 4
end
julia> vec_union = Union{M{Int},N{Int},O{Int},P{Int}}[rand((M,N,O,P))() for _ in 1:10^6];
julia> vec_sum = A{Int}[rand((B,C,D,E))() for _ in 1:10^6];
julia> vec_compact = F{Int}[rand((G,H,I,L))() for _ in 1:10^6];
julia> Base.summarysize(vec_union)
21997856
julia> Base.summarysize(vec_sum)
28868832
julia> Base.summarysize(vec_compact)
49924817
julia> using BenchmarkTools
julia> @btime sum(x.a for x in $vec_union);
26.762 ms (999788 allocations: 15.26 MiB)
julia> @btime sum(x.a for x in $vec_sum);
6.595 ms (0 allocations: 0 bytes)
julia> @btime sum(x.a for x in $vec_compact);
1.936 ms (0 allocations: 0 bytes)
In this case, @compact_structs
types are almost 15 times faster than Union
ones, even if they require more than
double the memory. Whereas, as expected, @sum_structs
types are less time efficient than @compact_structs
ones,
but the memory usage increase in respect to Union
types is smaller.
Contributions are welcome! If you encounter any issues, have suggestions for improvements, or would like to add new features, feel free to open an issue or submit a pull request.