Skip to content

Commit

Permalink
simpler and faster deepcopy_internal for BigInt and BigFloat (JuliaLa…
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Jun 8, 2021
1 parent 523dafe commit f262de8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
9 changes: 1 addition & 8 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,7 @@ Base.add_with_overflow(a::BigInt, b::BigInt) = a + b, false
Base.sub_with_overflow(a::BigInt, b::BigInt) = a - b, false
Base.mul_with_overflow(a::BigInt, b::BigInt) = a * b, false

function Base.deepcopy_internal(x::BigInt, stackdict::IdDict)
if haskey(stackdict, x)
return stackdict[x]
end
y = MPZ.set(x)
stackdict[x] = y
return y
end
Base.deepcopy_internal(x::BigInt, stackdict::IdDict) = get!(() -> MPZ.set(x), stackdict, x)

## streamlined hashing for BigInt, by avoiding allocation from shifts ##

Expand Down
16 changes: 8 additions & 8 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1021,14 +1021,14 @@ set_emax!(x) = check_exponent_err(ccall((:mpfr_set_emax, :libmpfr), Cint, (Clong
set_emin!(x) = check_exponent_err(ccall((:mpfr_set_emin, :libmpfr), Cint, (Clong,), x))

function Base.deepcopy_internal(x::BigFloat, stackdict::IdDict)
haskey(stackdict, x) && return stackdict[x]
# d = copy(x._d)
d = x._d
d′ = GC.@preserve d unsafe_string(pointer(d), sizeof(d)) # creates a definitely-new String
y = _BigFloat(x.prec, x.sign, x.exp, d′)
#ccall((:mpfr_custom_move,:libmpfr), Cvoid, (Ref{BigFloat}, Ptr{Limb}), y, d) # unnecessary
stackdict[x] = y
return y
get!(stackdict, x) do
# d = copy(x._d)
d = x._d
d′ = GC.@preserve d unsafe_string(pointer(d), sizeof(d)) # creates a definitely-new String
y = _BigFloat(x.prec, x.sign, x.exp, d′)
#ccall((:mpfr_custom_move,:libmpfr), Cvoid, (Ref{BigFloat}, Ptr{Limb}), y, d) # unnecessary
return y
end
end

function decompose(x::BigFloat)::Tuple{BigInt, Int, Int}
Expand Down

0 comments on commit f262de8

Please sign in to comment.