Skip to content

Commit

Permalink
Add "copy-constructor" to prevent (incorrect) convert call when type …
Browse files Browse the repository at this point in the history
…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)
```
  • Loading branch information
NHDaly authored and omus committed Nov 5, 2018
1 parent cc6aed3 commit d87debb
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/FixedPointDecimals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d87debb

Please sign in to comment.