JuliaMath / JuliaMath/FixedPointDecimals.jl

FD{BigInt} operations currently allocate more than necessary due to promotion.

Open
#87 1 comment 0 reactions 0 assignees View on GitHub
performance
Dominant language
Julia
Stars
37
Forks
23
PR merge metrics
No merged PRs in 30d

Description

Consider:
```julia
julia> @which FD{BigInt,2}(2) + 2
+(x::Number, y::Number)
@ Base promotion.jl:410

julia> @code_typed FD{BigInt,2}(2) + 2
CodeInfo(
1 ─ %1 = invoke Base.GMP.MPZ.set_si(10::Int64)::BigInt
│ %2 = invoke Base.GMP.bigint_pow(%1::BigInt, 2::Int64)::BigInt
│ %3 = invoke Base.GMP.MPZ.mul_si(%2::BigInt, y::Int64)::BigInt
│ %4 = Base.getfield(x, :i)::BigInt
│ %5 = invoke Base.GMP.MPZ.add(%4::BigInt, %3::BigInt)::BigInt
│ %6 = %new(FixedDecimal{BigInt, 2}, %5)::FixedDecimal{BigInt, 2}
└── return %6
) => FixedDecimal{BigInt, 2}

julia> @code_typed optimize=false FD{BigInt,2}(2) + 2
CodeInfo(
1 ─ %1 = Base.:+::Core.Const(+)
│ %2 = Base.promote(x, y)::Tuple{FixedDecimal{BigInt, 2}, FixedDecimal{BigInt, 2}}
│ %3 = Core._apply_iterate(Base.iterate, %1, %2)::FixedDecimal{BigInt, 2}
└── return %3
) => FixedDecimal{BigInt, 2}
```

If we instead had special-cased operators for `::FD{BigInt}, ::Integer` we could avoid the promotion and save an allocation.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.