From a04c50064696402c5ce4119706e09d90d3e1986b Mon Sep 17 00:00:00 2001 From: Jutho Haegeman Date: Tue, 15 Jun 2021 01:18:49 +0200 Subject: [PATCH] try using atomic{int} for growinglist length --- src/WignerSymbols.jl | 13 ++++--------- src/growinglist.jl | 10 +++++----- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/WignerSymbols.jl b/src/WignerSymbols.jl index 1c7db02..f7d820a 100644 --- a/src/WignerSymbols.jl +++ b/src/WignerSymbols.jl @@ -366,17 +366,12 @@ function compute6jseries(β₁, β₂, β₃, α₁, α₂, α₃, α₄) end function _precompile_() - @assert precompile(prime, (Int,)) - @assert precompile(primefactor, (Int,)) - @assert precompile(primefactorial, (Int,)) @assert precompile(wigner3j, (Type{Float64}, Int, Int, Int, Int, Int, Int)) @assert precompile(wigner6j, (Type{Float64}, Int, Int, Int, Int, Int, Int)) - @assert precompile(wigner3j, (Type{BigFloat}, HalfInt, HalfInt, HalfInt, HalfInt, HalfInt, HalfInt)) - @assert precompile(wigner6j, (Type{BigFloat}, HalfInt, HalfInt, HalfInt, HalfInt, HalfInt, HalfInt)) - @assert precompile(getindex, (GrowingList{Int}, Int)) - @assert precompile(getindex, (GrowingList{BigInt}, Int)) - @assert precompile(get!, (GrowingList{Int}, Int, Int)) - @assert precompile(get!, (GrowingList{BigInt}, Int, BigInt)) + @assert precompile(wigner3j, (Type{BigFloat}, Rational{Int}, Rational{Int}, Rational{Int}, Rational{Int}, Rational{Int}, Rational{Int})) + @assert precompile(wigner6j, (Type{BigFloat}, Rational{Int}, Rational{Int}, Rational{Int}, Rational{Int}, Rational{Int}, Rational{Int})) + @assert precompile(wigner3j, (Type{RRBig}, HalfInt, HalfInt, HalfInt, HalfInt, HalfInt, HalfInt)) + @assert precompile(wigner6j, (Type{RRBig}, HalfInt, HalfInt, HalfInt, HalfInt, HalfInt, HalfInt)) end _precompile_() diff --git a/src/growinglist.jl b/src/growinglist.jl index 13914c1..046746d 100644 --- a/src/growinglist.jl +++ b/src/growinglist.jl @@ -1,4 +1,4 @@ -using Base.Threads: Atomic, SpinLock +using Base.Threads: Atomic, SpinLock, atomic_add! # ListSegment represents a segment from a GrowingList; it has a list `data` to hold the elements, filled up to `currentlength`, and possibly a reference to the next segment, if it is not the final segment. mutable struct ListSegment{T} @@ -66,7 +66,7 @@ The list is grown by adding new segments using a linked list data structure. Thi """ mutable struct GrowingList{T} <: AbstractVector{T} first::ListSegment{T} - totallength::Int + totallength::Atomic{Int} growthfactor::Float64 lock::SpinLock function GrowingList{T}(iter; @@ -88,7 +88,7 @@ mutable struct GrowingList{T} <: AbstractVector{T} _unsafe_getindex(first, i, val, ceil(Int, (i-1)*growthfactor)) next = iterate(iter, state) end - return new{T}(first, i, growthfactor, SpinLock()) + return new{T}(first, Atomic{Int}(i), growthfactor, SpinLock()) end end GrowingList(v::Vector{T}; sizehint = max(16, length(v)), growthfactor = 2.) where {T} = @@ -100,9 +100,9 @@ GrowingList{T}(; sizehint = 16, growthfactor = 2.) where {T} = GrowingList(; sizehint = 16, growthfactor = 2.) = GrowingList{Any}((); sizehint = sizehint, growthfactor = growthfactor) -Base.length(l::GrowingList) = l.totallength +Base.length(l::GrowingList) = l.totallength[] function _raise_length!(l::GrowingList) - l.totallength += 1 + atomic_add!(l.totallength, 1) end Base.size(l::GrowingList) = (length(l),)