Skip to content

Commit 0c78100

Browse files
refactor: promote types in remake_buffer instead of Union
1 parent 1339885 commit 0c78100

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/remake.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ are symbolic variables whose index in the buffer is determined using `sys`. The
66
values in `vals` may not match the types of values stored at the corresponding indexes in
77
the buffer, in which case the type of the buffer should be promoted accordingly. In
88
general, this method should attempt to preserve the types of values stored in `vals` as
9-
much as possible. The returned buffer should be of the same type (ignoring type-parameters)
10-
as `oldbuffer`.
9+
much as possible. Types can be promoted for type-stability, to maintain performance. The
10+
returned buffer should be of the same type (ignoring type-parameters) as `oldbuffer`.
1111
1212
This method is already implemented for
1313
`remake_buffer(sys, oldbuffer::AbstractArray, vals::Dict)` and supports static arrays
@@ -19,11 +19,11 @@ function remake_buffer(sys, oldbuffer::AbstractArray, vals::Dict)
1919
if ArrayInterface.ismutable(oldbuffer) && !isa(oldbuffer, MArray)
2020
elT = Union{}
2121
for val in values(vals)
22-
elT = Union{elT, typeof(val)}
22+
elT = promote_type(elT, typeof(val))
2323
end
2424

2525
newbuffer = similar(oldbuffer, elT)
26-
setu(sys, collect(keys(vals)))(newbuffer, values(vals))
26+
setu(sys, collect(keys(vals)))(newbuffer, elT.(values(vals)))
2727
else
2828
mutbuffer = remake_buffer(sys, collect(oldbuffer), vals)
2929
newbuffer = similar_type(oldbuffer, eltype(mutbuffer))(mutbuffer)

test/remake_test.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ for (buf, newbuf, newvals) in [
77
# standard operation
88
([1.0, 2.0, 3.0], [2.0, 3.0, 4.0],
99
Dict(:x => 2.0, :y => 3.0, :z => 4.0))
10-
# type "demotion"
10+
# buffer type "demotion"
1111
([1.0, 2.0, 3.0], [2, 3, 4],
1212
Dict(:x => 2, :y => 3, :z => 4))
13-
# type promotion
13+
# buffer type promotion
1414
([1, 2, 3], [2.0, 3.0, 4.0],
1515
Dict(:x => 2.0, :y => 3.0, :z => 4.0))
16-
# union
17-
([1, 2, 3], Union{Int, Float64}[2, 3.0, 4.0],
16+
# value type promotion
17+
([1, 2, 3], [2.0, 3.0, 4.0],
1818
Dict(:x => 2, :y => 3.0, :z => 4.0))
1919
# standard operation
2020
([1.0, 2.0, 3.0], [2.0, 3.0, 4.0],
2121
Dict(:a => 2.0, :b => 3.0, :c => 4.0))
22-
# type "demotion"
22+
# buffer type "demotion"
2323
([1.0, 2.0, 3.0], [2, 3, 4],
2424
Dict(:a => 2, :b => 3, :c => 4))
25-
# type promotion
25+
# buffer type promotion
2626
([1, 2, 3], [2.0, 3.0, 4.0],
2727
Dict(:a => 2.0, :b => 3.0, :c => 4.0))
28-
# union
29-
([1, 2, 3], Union{Int, Float64}[2, 3.0, 4.0],
28+
# value type promotion
29+
([1, 2, 3], [2, 3.0, 4.0],
3030
Dict(:a => 2, :b => 3.0, :c => 4.0))]
3131
for arrType in [Vector, SVector{3}, MVector{3}, SizedVector{3}]
3232
buf = arrType(buf)

0 commit comments

Comments
 (0)