From 1ad557e1178a0ddb9f9e700ad810c8fa728a48d4 Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Fri, 2 Nov 2018 11:49:01 -0400 Subject: [PATCH] Add "copy-constructor" to prevent (incorrect) convert call when type is the same MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) ``` --- src/FixedPointDecimals.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/FixedPointDecimals.jl b/src/FixedPointDecimals.jl index dd744ee..79014c3 100644 --- a/src/FixedPointDecimals.jl +++ b/src/FixedPointDecimals.jl @@ -99,6 +99,9 @@ struct FixedDecimal{T <: Integer, f} <: Real _throw_storage_error(f, T, n) end end + + # Copy constructor -- prevents unneeded work to convert between the same types + FixedDecimal{T,f}(x::FixedDecimal{T,f}) where {T<:Integer,f} = new{T,f}(x.i) end @noinline function _throw_storage_error(f, T, n)