Skip to content

Commit

Permalink
updated benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
louisponet committed May 10, 2023
1 parent 2dfb239 commit 29ca45c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
3 changes: 1 addition & 2 deletions benchmark/bench_component.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ module BenchComponent
using BenchmarkTools
import Random
using Overseer
using Parameters

@component @with_kw struct Spatial
@component Base.@kwdef struct Spatial
position::NTuple{3, Float64} = (1.0,1.0,1.0)
velocity::NTuple{3, Float64} = (1.0,1.0,1.0)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
module BenchSharedComponent
module BenchPooledComponent

using BenchmarkTools
import Random
using Overseer
using Parameters

@shared_component @with_kw struct Spatial
@pooled_component Base.@kwdef struct Spatial
position::NTuple{3, Float64} = (1.0,1.0,1.0)
velocity::NTuple{3, Float64} = (1.0,1.0,1.0)
end

@shared_component struct Spring
@pooled_component struct Spring
center::NTuple{3, Float64}
spring_constant::Float64
end

@shared_component mutable struct Rotation
@pooled_component mutable struct Rotation
omega::Float64
center::NTuple{3, Float64}
axis::NTuple{3, Float64}
Expand Down Expand Up @@ -107,11 +106,11 @@ suite["basic"] = BenchmarkGroup()

const ids = Entity.(unique(rand(1:1000, 1000)))

suite["basic"]["insertion"] = @benchmarkable bench_insertion(c, $ids, $(Spatial((123.0, 1.2, 1.0), (0.4, 12.0, 234.9)))) setup=(c=SharedComponent{Spatial}()) evals=1
suite["basic"]["insertion"] = @benchmarkable bench_insertion(c, $ids, $(Spatial((123.0, 1.2, 1.0), (0.4, 12.0, 234.9)))) setup=(c=PooledComponent{Spatial}()) evals=1

suite["basic"]["access"] = @benchmarkable bench_access(c, $ids, 0.0) setup=(c = SharedComponent{Spatial}(); bench_insertion(c, $ids, Spatial((123.0, 1.2, 1.0), (0.4, 12.0, 234.9))))
suite["basic"]["access"] = @benchmarkable bench_access(c, $ids, 0.0) setup=(c = PooledComponent{Spatial}(); bench_insertion(c, $ids, Spatial((123.0, 1.2, 1.0), (0.4, 12.0, 234.9))))

const c_full = SharedComponent{Spatial}()
const c_full = PooledComponent{Spatial}()
bench_insertion(c_full, ids, Spatial((123.0, 1.2, 1.0), (0.4, 12.0, 234.9)))

suite["basic"]["access inbounds"] = @benchmarkable bench_access_inbounds(c, $ids, 0.0) setup=(c = deepcopy(c_full))
Expand Down Expand Up @@ -178,4 +177,4 @@ suite["real life"]["update"]["new school full"] = @benchmarkable (update_new(Osc
update_new(Mover(), m)) setup=(m = Ledger(st); e_fill(m))

end
BenchSharedComponent.suite
BenchPooledComponent.suite
10 changes: 5 additions & 5 deletions src/component.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ Base.@propagate_inbounds function Base.getindex(c::AbstractComponent,
return map(x -> c[x], I)
end

Base.@propagate_inbounds @inline Base.setindex!(c::AbstractComponent, v, i::Integer) = data(c)[data_index(c, i)] = v
Base.@propagate_inbounds @inline Base.setindex!(c::AbstractComponent, v, e::AbstractEntity) =
Base.@propagate_inbounds @inline Base.setindex!(c::AbstractComponent{T}, v::T, i::Integer) where {T} = data(c)[data_index(c, i)] = v
Base.@propagate_inbounds @inline Base.setindex!(c::AbstractComponent{T}, v::T, e::AbstractEntity) where {T} =
setindex!(component(c), v, e)

function Base.permute!(c::AbstractComponent, permvec::AbstractVector{<:Integer})
Expand Down Expand Up @@ -171,11 +171,11 @@ function Base.pop!(c::Component, e::AbstractEntity)
c.data[id] = c.data[end]
pop!(c.data)
pop!(c.indices, e.id)
return v
return EntityState(Entity(e), v)
end
end

function Base.pop!(c::AbstractComponent)
function Base.pop!(c::Component)
@boundscheck if isempty(c)
throw(BoundsError(c))
end
Expand Down Expand Up @@ -415,7 +415,7 @@ function Base.pop!(c::PooledComponent, e::AbstractEntity)
val = c.data[g]
maybe_cleanup_empty_pool!(c, g)

return val
return EntityState(Entity(e), val)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/entity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ end

Entity(e::Entity) = e

Base.@propagate_inbounds Entity(c::AbstractComponent, i::Int) = Entity(c.indices.packed[i])
Base.@propagate_inbounds Entity(c::AbstractComponent, i::Int) = Entity(indices(c).packed[i])

Base.iterate(e::Entity, state = 1) = state > 1 ? nothing : (e, state + 1)

Expand Down
28 changes: 15 additions & 13 deletions src/interfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Base.@kwdef mutable struct TestCompData
p::Int = 0
end
Base.:(==)(t::TestCompData, t1::TestCompData) = t.p == t1.p
Base.:(<)(en1::TestCompData, en2::TestCompData) = en1.p < en2.p

Base.:(<)(v, en1::TestCompData) = v < en1.p

Base.:(==)(v, en1::TestCompData) = en1.p == v

# AbstractComponent Interface
"""
Expand All @@ -15,24 +20,21 @@ function test_abstractcomponent_interface(::Type{T}) where {T<:AbstractComponent
c = T{TestCompData}()

@test eltype(c) <: TestCompData
if hasfield(T, :indices)
@test c.indices isa Indices
else
@test indices_iterator(c) isa IndicesIterator
@test reverse_indices_iterator(c) isa ReverseIndicesIterator
end
@test indices(c) isa Indices
@test indices_iterator(c) isa Union{Indices, IndicesIterator}
@test reverse_indices_iterator(c) isa ReverseIndicesIterator

@test isempty(c)
@test length(c) == 0
c[Entity(1)] = TestCompData(1)
c[Entity(2)] = TestCompData(1)
@test Entity(2) in c
@test length(c) == 2 == size(c)[1] == length(c.indices) == length(data(c))
@test length(c) == 2 == size(c)[1] == length(indices(c)) == length(data(c))

@test c[Entity(2)] isa TestCompData

@test entity(c, 1) isa EntityState{Tuple{T{TestCompData}}}
@test pop!(c, Entity(2)) == TestCompData(1)
@test pop!(c, Entity(2)) == EntityState(Entity(2), TestCompData(1))
@test pop!(c) == EntityState(Entity(1), TestCompData(1))
@test isempty(c)

Expand Down Expand Up @@ -94,7 +96,7 @@ function test_abstractcomponent_interface(::Type{T}) where {T<:AbstractComponent
end

@testset "Component manipulation" begin
@test pop!(c, Entity(10)) == TestCompData(10)
@test pop!(c, Entity(10)) == EntityState(Entity(10), TestCompData(10))

@test length(c) == length(entities1) - 1
@test c[1] == TestCompData(2)
Expand All @@ -116,15 +118,15 @@ function test_abstractcomponent_interface(::Type{T}) where {T<:AbstractComponent
orig1 = c[e1]
orig2 = c[e2]

orig_id1 = c.indices[e1.id]
orig_id2 = c.indices[e2.id]
orig_id1 = indices(c)[e1.id]
orig_id2 = indices(c)[e2.id]

swap_order!(c, e1, e2)
@test c[e2] == orig2
@test c[e1] == orig1

@test c.indices[e2.id] == orig_id1
@test c.indices[e1.id] == orig_id2
@test indices(c)[e2.id] == orig_id1
@test indices(c)[e1.id] == orig_id2

es = map(x->x.e, @entities_in(c))
cur = c[es]
Expand Down
4 changes: 2 additions & 2 deletions src/ledger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Ledger() = Ledger(Entity[],
function Ledger(comps::Dict{DataType, AbstractComponent})
out = Ledger()
out.components = comps
out.entities = Entity.(union(map(x->x.indices.packed, values(comps))...))
out.entities = Entity.(union(map(x->indices(x).packed, values(comps))...))
return out
end

Expand Down Expand Up @@ -124,7 +124,7 @@ function Base.empty!(m::AbstractLedger)
empty!(groups(m))
end

function Base.getindex(m::AbstractLedger, ::Type{T}) where {T}
function Base.getindex(m::AbstractLedger, ::Type{T})::component_type(T) where {T}
return get!(components(m), T, component_type(T)())
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ end

# Check some basics
@test p1 in c5
@test pop!(c5, p1) == Test5(1)
@test pop!(c5, p1) == EntityState(p1, Test5(1))
@test !(p1 in c5)
@test length(c5.pool) == 9
@test c5[p2] == Test5(2)
Expand Down

0 comments on commit 29ca45c

Please sign in to comment.