Skip to content

Commit d87debb

Browse files
NHDalyomus
authored andcommitted
Add "copy-constructor" to prevent (incorrect) convert call when type is the same (#36)
Before this change: ``` julia> x = f = FixedDecimal{Int128,2}(3.25) >> FixedDecimal{Int128,2}(3.25) julia> @Btime x = FixedDecimal{Int128, 2}($f) 7.272 μs (159 allocations: 2.73 KiB) >> FixedDecimal{Int128,2}(3.25) ``` After this change: ``` julia> x = f = FixedDecimal{Int128,2}(3.25) >> FixedDecimal{Int128,2}(3.25) julia> @Btime x = FixedDecimal{Int128, 2}($f) 1.508 ns (0 allocations: 0 bytes) >> FixedDecimal{Int128,2}(3.25) ```
1 parent cc6aed3 commit d87debb

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

src/FixedPointDecimals.jl

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ struct FixedDecimal{T <: Integer, f} <: Real
9999
_throw_storage_error(f, T, n)
100100
end
101101
end
102+
103+
# Copy constructor -- prevents unneeded work to convert between the same types
104+
FixedDecimal{T,f}(x::FixedDecimal{T,f}) where {T<:Integer,f} = new{T,f}(x.i)
102105
end
103106

104107
@noinline function _throw_storage_error(f, T, n)

0 commit comments

Comments
 (0)