Skip to content

Commit

Permalink
perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed Nov 6, 2023
1 parent f114706 commit 43f997c
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Observables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ mutable struct ObserverFunction <: Function
end


const OBSID_COUNTER = Base.Threads.Atomic{UInt64}(UInt64(0))

"""
obs = Observable(val; ignore_equal_values=false)
obs = Observable{T}(val; ignore_equal_values=false)
Expand All @@ -67,13 +69,15 @@ mutable struct Observable{T} <: AbstractObservable{T}
listeners::Vector{Pair{Int, Any}}
inputs::Vector{ObserverFunction} # for map!ed Observables
ignore_equal_values::Bool
id::UInt64
val::T

function Observable{T}(; ignore_equal_values::Bool=false) where {T}
return new{T}(Pair{Int, Any}[], [], ignore_equal_values)
Base.Threads.atomic_add!(OBSID_COUNTER, UInt64(1))
return new{T}(Pair{Int,Any}[], [], ignore_equal_values, OBSID_COUNTER[])
end
function Observable{T}(@nospecialize(val); ignore_equal_values::Bool=false) where {T}
return new{T}(Pair{Int, Any}[], [], ignore_equal_values, val)
Base.Threads.atomic_add!(OBSID_COUNTER, UInt64(1))
return new{T}(Pair{Int,Any}[], [], ignore_equal_values, OBSID_COUNTER[], val)
end
end

Expand All @@ -82,14 +86,14 @@ end
Gets a unique id for an observable.
"""
obsid(observable::Observable) = string(objectid(observable))
obsid(observable::Observable) = string(getfield(observable, :id))
obsid(obs::AbstractObservable) = obsid(observe(obs))

function Base.getproperty(obs::Observable, field::Symbol)
if field === :id
return obsid(obs)
else
getfield(obs, field)
return getfield(obs, field)
end
end

Expand Down Expand Up @@ -462,7 +466,7 @@ All other objects in `args` are passed as-is.
See also: [`on`](@ref).
"""
function onany(f, args...; weak::Bool = false, priority::Int = 0, update::Bool = false)
function onany(f, args...; weak::Bool=false, priority::Int=0, update::Bool = false)
callback = OnAny(f, args)
obsfuncs = ObserverFunction[]
for observable in args
Expand Down

0 comments on commit 43f997c

Please sign in to comment.