1- use bevy:: ecs:: {
2- component:: { ComponentDescriptor , StorageType } ,
3- entity:: Entity ,
4- world:: World ,
5- } ;
1+ use bevy:: ecs:: { component:: Component , entity:: Entity , world:: World } ;
62use criterion:: { black_box, criterion_group, criterion_main, Criterion } ;
73
84criterion_group ! (
@@ -15,16 +11,18 @@ criterion_group!(
1511) ;
1612criterion_main ! ( benches) ;
1713
18- struct A ( f32 ) ;
14+ #[ derive( Component , Default ) ]
15+ #[ component( storage = "Table" ) ]
16+ struct Table ( f32 ) ;
17+ #[ derive( Component , Default ) ]
18+ #[ component( storage = "SparseSet" ) ]
19+ struct Sparse ( f32 ) ;
1920
2021const RANGE : std:: ops:: Range < u32 > = 5 ..6 ;
2122
22- fn setup ( entity_count : u32 , storage : StorageType ) -> World {
23+ fn setup < T : Component + Default > ( entity_count : u32 ) -> World {
2324 let mut world = World :: default ( ) ;
24- world
25- . register_component ( ComponentDescriptor :: new :: < A > ( storage) )
26- . unwrap ( ) ;
27- world. spawn_batch ( ( 0 ..entity_count) . map ( |_| ( A ( 0.0 ) , ) ) ) ;
25+ world. spawn_batch ( ( 0 ..entity_count) . map ( |_| ( T :: default ( ) , ) ) ) ;
2826 world
2927}
3028
@@ -35,7 +33,7 @@ fn world_entity(criterion: &mut Criterion) {
3533
3634 for entity_count in RANGE . map ( |i| i * 10_000 ) {
3735 group. bench_function ( format ! ( "{}_entities" , entity_count) , |bencher| {
38- let world = setup ( entity_count , StorageType :: Table ) ;
36+ let world = setup :: < Table > ( entity_count ) ;
3937
4038 bencher. iter ( || {
4139 for i in 0 ..entity_count {
@@ -55,21 +53,26 @@ fn world_get(criterion: &mut Criterion) {
5553 group. measurement_time ( std:: time:: Duration :: from_secs ( 4 ) ) ;
5654
5755 for entity_count in RANGE . map ( |i| i * 10_000 ) {
58- for storage in [ StorageType :: Table , StorageType :: SparseSet ] {
59- group. bench_function (
60- format ! ( "{}_entities_{:?}" , entity_count, storage) ,
61- |bencher| {
62- let world = setup ( entity_count, storage) ;
63-
64- bencher. iter ( || {
65- for i in 0 ..entity_count {
66- let entity = Entity :: new ( i) ;
67- assert ! ( world. get:: <A >( entity) . is_some( ) ) ;
68- }
69- } ) ;
70- } ,
71- ) ;
72- }
56+ group. bench_function ( format ! ( "{}_entities_table" , entity_count) , |bencher| {
57+ let world = setup :: < Table > ( entity_count) ;
58+
59+ bencher. iter ( || {
60+ for i in 0 ..entity_count {
61+ let entity = Entity :: new ( i) ;
62+ assert ! ( world. get:: <Table >( entity) . is_some( ) ) ;
63+ }
64+ } ) ;
65+ } ) ;
66+ group. bench_function ( format ! ( "{}_entities_sparse" , entity_count) , |bencher| {
67+ let world = setup :: < Sparse > ( entity_count) ;
68+
69+ bencher. iter ( || {
70+ for i in 0 ..entity_count {
71+ let entity = Entity :: new ( i) ;
72+ assert ! ( world. get:: <Sparse >( entity) . is_some( ) ) ;
73+ }
74+ } ) ;
75+ } ) ;
7376 }
7477
7578 group. finish ( ) ;
@@ -81,22 +84,28 @@ fn world_query_get(criterion: &mut Criterion) {
8184 group. measurement_time ( std:: time:: Duration :: from_secs ( 4 ) ) ;
8285
8386 for entity_count in RANGE . map ( |i| i * 10_000 ) {
84- for storage in [ StorageType :: Table , StorageType :: SparseSet ] {
85- group. bench_function (
86- format ! ( "{}_entities_{:?}" , entity_count, storage) ,
87- |bencher| {
88- let mut world = setup ( entity_count, storage) ;
89- let mut query = world. query :: < & A > ( ) ;
90-
91- bencher. iter ( || {
92- for i in 0 ..entity_count {
93- let entity = Entity :: new ( i) ;
94- assert ! ( query. get( & world, entity) . is_ok( ) ) ;
95- }
96- } ) ;
97- } ,
98- ) ;
99- }
87+ group. bench_function ( format ! ( "{}_entities_table" , entity_count) , |bencher| {
88+ let mut world = setup :: < Table > ( entity_count) ;
89+ let mut query = world. query :: < & Table > ( ) ;
90+
91+ bencher. iter ( || {
92+ for i in 0 ..entity_count {
93+ let entity = Entity :: new ( i) ;
94+ assert ! ( query. get( & world, entity) . is_ok( ) ) ;
95+ }
96+ } ) ;
97+ } ) ;
98+ group. bench_function ( format ! ( "{}_entities_sparse" , entity_count) , |bencher| {
99+ let mut world = setup :: < Sparse > ( entity_count) ;
100+ let mut query = world. query :: < & Sparse > ( ) ;
101+
102+ bencher. iter ( || {
103+ for i in 0 ..entity_count {
104+ let entity = Entity :: new ( i) ;
105+ assert ! ( query. get( & world, entity) . is_ok( ) ) ;
106+ }
107+ } ) ;
108+ } ) ;
100109 }
101110
102111 group. finish ( ) ;
@@ -108,24 +117,32 @@ fn world_query_iter(criterion: &mut Criterion) {
108117 group. measurement_time ( std:: time:: Duration :: from_secs ( 4 ) ) ;
109118
110119 for entity_count in RANGE . map ( |i| i * 10_000 ) {
111- for storage in [ StorageType :: Table , StorageType :: SparseSet ] {
112- group. bench_function (
113- format ! ( "{}_entities_{:?}" , entity_count, storage) ,
114- |bencher| {
115- let mut world = setup ( entity_count, storage) ;
116- let mut query = world. query :: < & A > ( ) ;
117-
118- bencher. iter ( || {
119- let mut count = 0 ;
120- for comp in query. iter ( & world) {
121- black_box ( comp) ;
122- count += 1 ;
123- }
124- assert_eq ! ( black_box( count) , entity_count) ;
125- } ) ;
126- } ,
127- ) ;
128- }
120+ group. bench_function ( format ! ( "{}_entities_table" , entity_count) , |bencher| {
121+ let mut world = setup :: < Table > ( entity_count) ;
122+ let mut query = world. query :: < & Table > ( ) ;
123+
124+ bencher. iter ( || {
125+ let mut count = 0 ;
126+ for comp in query. iter ( & world) {
127+ black_box ( comp) ;
128+ count += 1 ;
129+ }
130+ assert_eq ! ( black_box( count) , entity_count) ;
131+ } ) ;
132+ } ) ;
133+ group. bench_function ( format ! ( "{}_entities_sparse" , entity_count) , |bencher| {
134+ let mut world = setup :: < Sparse > ( entity_count) ;
135+ let mut query = world. query :: < & Sparse > ( ) ;
136+
137+ bencher. iter ( || {
138+ let mut count = 0 ;
139+ for comp in query. iter ( & world) {
140+ black_box ( comp) ;
141+ count += 1 ;
142+ }
143+ assert_eq ! ( black_box( count) , entity_count) ;
144+ } ) ;
145+ } ) ;
129146 }
130147
131148 group. finish ( ) ;
@@ -137,24 +154,32 @@ fn world_query_for_each(criterion: &mut Criterion) {
137154 group. measurement_time ( std:: time:: Duration :: from_secs ( 4 ) ) ;
138155
139156 for entity_count in RANGE . map ( |i| i * 10_000 ) {
140- for storage in [ StorageType :: Table , StorageType :: SparseSet ] {
141- group. bench_function (
142- format ! ( "{}_entities_{:?}" , entity_count, storage) ,
143- |bencher| {
144- let mut world = setup ( entity_count, storage) ;
145- let mut query = world. query :: < & A > ( ) ;
146-
147- bencher. iter ( || {
148- let mut count = 0 ;
149- query. for_each ( & world, |comp| {
150- black_box ( comp) ;
151- count += 1 ;
152- } ) ;
153- assert_eq ! ( black_box( count) , entity_count) ;
154- } ) ;
155- } ,
156- ) ;
157- }
157+ group. bench_function ( format ! ( "{}_entities_table" , entity_count) , |bencher| {
158+ let mut world = setup :: < Table > ( entity_count) ;
159+ let mut query = world. query :: < & Table > ( ) ;
160+
161+ bencher. iter ( || {
162+ let mut count = 0 ;
163+ query. for_each ( & world, |comp| {
164+ black_box ( comp) ;
165+ count += 1 ;
166+ } ) ;
167+ assert_eq ! ( black_box( count) , entity_count) ;
168+ } ) ;
169+ } ) ;
170+ group. bench_function ( format ! ( "{}_entities_sparse" , entity_count) , |bencher| {
171+ let mut world = setup :: < Sparse > ( entity_count) ;
172+ let mut query = world. query :: < & Sparse > ( ) ;
173+
174+ bencher. iter ( || {
175+ let mut count = 0 ;
176+ query. for_each ( & world, |comp| {
177+ black_box ( comp) ;
178+ count += 1 ;
179+ } ) ;
180+ assert_eq ! ( black_box( count) , entity_count) ;
181+ } ) ;
182+ } ) ;
158183 }
159184
160185 group. finish ( ) ;
0 commit comments