From 26195b5429b47092b6c4db1f3a1b5bad1f484191 Mon Sep 17 00:00:00 2001 From: louisponet Date: Wed, 10 May 2023 21:48:41 +0200 Subject: [PATCH] some cleanup --- src/component.jl | 43 +++++++++++++++++++++---------------------- src/indices.jl | 6 +++--- src/iteration.jl | 4 ++-- src/ledger.jl | 9 ++++++++- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/component.jl b/src/component.jl index a14ae17..368b8a9 100644 --- a/src/component.jl +++ b/src/component.jl @@ -36,7 +36,7 @@ Base.empty!(c::AbstractComponent) = empty!(component(c)) Base.@propagate_inbounds @inline Base.getindex(c::AbstractComponent, i::Integer) = data(c)[data_index(c, i)] -@inline function Base.getindex(c::AbstractComponent, e::AbstractEntity) +Base.@propagate_inbounds @inline function Base.getindex(c::AbstractComponent, e::AbstractEntity) @boundscheck if !in(e, c) throw(BoundsError(c, Entity(e))) end @@ -139,7 +139,7 @@ Base.@propagate_inbounds @inline data_index(c::Component, e::AbstractEntity) = c ##### BASE Extensions #### -@inline function Base.setindex!(c::Component{T}, v::T, e::AbstractEntity) where {T} +Base.@propagate_inbounds @inline function Base.setindex!(c::Component{T}, v::T, e::AbstractEntity) where {T} eid = Entity(e).id @boundscheck if !in(e, c) push!(c.indices, eid) @@ -161,7 +161,7 @@ end pops the data for `entity` out of `component`. """ -function Base.pop!(c::Component, e::AbstractEntity) +Base.@propagate_inbounds @inline function Base.pop!(c::Component, e::AbstractEntity) @boundscheck if !in(e, c) throw(BoundsError(c, Entity(e))) end @@ -175,7 +175,7 @@ function Base.pop!(c::Component, e::AbstractEntity) end end -function Base.pop!(c::Component) +Base.@propagate_inbounds @inline function Base.pop!(c::Component) @boundscheck if isempty(c) throw(BoundsError(c)) end @@ -305,29 +305,28 @@ Base.@propagate_inbounds @inline Base.parent(c::PooledComponent, e::Entity) = pa # c[entity] = value # set value of this entity -@inline function Base.setindex!(c::PooledComponent{T}, v::T, e::AbstractEntity) where {T} +Base.@propagate_inbounds @inline function Base.setindex!(c::PooledComponent{T}, v::T, e::AbstractEntity) where {T} eid = e.id - if in(e, c) - @inbounds begin - pid = c.indices[eid] - g = c.pool[pid] - if c.pool_size[g] == 1 - # the entity already has its own (otherwise empty) pool - adjust value - c.data[g] = v - else - # the entity is part of a larger pool - create a new one - c.pool_size[g] -= 1 - push!(c.data, v) - push!(c.pool_size, 1) - c.pool[pid] = length(c.data) - end - end - else - # the entity is not in the component - add it + @boundscheck if !in(e, c) push!(c.indices, eid) push!(c.pool, length(c.data) + 1) push!(c.pool_size, 1) push!(c.data, v) + return v + end + @inbounds begin + pid = c.indices[eid] + g = c.pool[pid] + if c.pool_size[g] == 1 + # the entity already has its own (otherwise empty) pool - adjust value + c.data[g] = v + else + # the entity is part of a larger pool - create a new one + c.pool_size[g] -= 1 + push!(c.data, v) + push!(c.pool_size, 1) + c.pool[pid] = length(c.data) + end end return v end diff --git a/src/indices.jl b/src/indices.jl index 2b10d61..e949b29 100644 --- a/src/indices.jl +++ b/src/indices.jl @@ -79,7 +79,7 @@ end return false else page = @inbounds rev[pageid] - return page !== NULL_INT_PAGE && !isempty(page) && @inbounds page[offset] != 0 + return page !== NULL_INT_PAGE && @inbounds page[offset] != 0 end end @@ -146,7 +146,7 @@ end return s end -@inline Base.@propagate_inbounds function Base.pop!(s::Indices) +Base.@propagate_inbounds @inline function Base.pop!(s::Indices) if isempty(s) throw(ArgumentError("Cannot pop an empty set.")) end @@ -158,7 +158,7 @@ end return id end -@inline Base.@propagate_inbounds function Base.pop!(s::Indices, id::Integer) +Base.@propagate_inbounds @inline function Base.pop!(s::Indices, id::Integer) id < 0 && throw(ArgumentError("Int to pop needs to be positive.")) @boundscheck if !in(id, s) diff --git a/src/iteration.jl b/src/iteration.jl index 3741e5d..1f0b54b 100644 --- a/src/iteration.jl +++ b/src/iteration.jl @@ -203,8 +203,8 @@ Base.in(e::AbstractEntity, i::EntityIterator) = in(e.id, i.it) @inline function Base.iterate(i::EntityIterator, state = i.it isa ReverseIndicesIterator ? length(i.it.shortest) : 1) n = iterate(i.it, state) n === nothing && return n - e = Entity(n[1]) - return EntityState(e, i.components), n[2] + @inbounds e = Entity(n[1]) + @inbounds return EntityState(e, i.components), n[2] end Base.last(i::EntityIterator) = EntityState(Entity(last(i.it)), i.components) diff --git a/src/ledger.jl b/src/ledger.jl index 4784ad3..1688710 100644 --- a/src/ledger.jl +++ b/src/ledger.jl @@ -125,7 +125,14 @@ function Base.empty!(m::AbstractLedger) end function Base.getindex(m::AbstractLedger, ::Type{T})::component_type(T) where {T} - return get!(components(m), T, component_type(T)()) + comps = components(m) + if haskey(comps, T) + return comps[T]::component_type(T) + else + c = component_type(T)() + comps[T] = c + return c + end end Base.copy(m::AbstractLedger) = Ledger(copy(entities(m)),