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,17 +11,18 @@ criterion_group!(
1511) ;
1612criterion_main ! ( benches) ;
1713
18- #[ derive( Component ) ]
19- 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 ) ;
2020
2121const RANGE : std:: ops:: Range < u32 > = 5 ..6 ;
2222
23- fn setup ( entity_count : u32 , storage : StorageType ) -> World {
23+ fn setup < T : Component + Default > ( entity_count : u32 ) -> World {
2424 let mut world = World :: default ( ) ;
25- world
26- . register_component ( ComponentDescriptor :: new :: < A > ( storage) )
27- . unwrap ( ) ;
28- world. spawn_batch ( ( 0 ..entity_count) . map ( |_| ( A ( 0.0 ) , ) ) ) ;
25+ world. spawn_batch ( ( 0 ..entity_count) . map ( |_| ( T :: default ( ) , ) ) ) ;
2926 world
3027}
3128
@@ -36,7 +33,7 @@ fn world_entity(criterion: &mut Criterion) {
3633
3734 for entity_count in RANGE . map ( |i| i * 10_000 ) {
3835 group. bench_function ( format ! ( "{}_entities" , entity_count) , |bencher| {
39- let world = setup ( entity_count , StorageType :: Table ) ;
36+ let world = setup :: < Table > ( entity_count ) ;
4037
4138 bencher. iter ( || {
4239 for i in 0 ..entity_count {
@@ -56,21 +53,26 @@ fn world_get(criterion: &mut Criterion) {
5653 group. measurement_time ( std:: time:: Duration :: from_secs ( 4 ) ) ;
5754
5855 for entity_count in RANGE . map ( |i| i * 10_000 ) {
59- for storage in [ StorageType :: Table , StorageType :: SparseSet ] {
60- group. bench_function (
61- format ! ( "{}_entities_{:?}" , entity_count, storage) ,
62- |bencher| {
63- let world = setup ( entity_count, storage) ;
64-
65- bencher. iter ( || {
66- for i in 0 ..entity_count {
67- let entity = Entity :: new ( i) ;
68- assert ! ( world. get:: <A >( entity) . is_some( ) ) ;
69- }
70- } ) ;
71- } ,
72- ) ;
73- }
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+ } ) ;
7476 }
7577
7678 group. finish ( ) ;
@@ -82,22 +84,28 @@ fn world_query_get(criterion: &mut Criterion) {
8284 group. measurement_time ( std:: time:: Duration :: from_secs ( 4 ) ) ;
8385
8486 for entity_count in RANGE . map ( |i| i * 10_000 ) {
85- for storage in [ StorageType :: Table , StorageType :: SparseSet ] {
86- group. bench_function (
87- format ! ( "{}_entities_{:?}" , entity_count, storage) ,
88- |bencher| {
89- let mut world = setup ( entity_count, storage) ;
90- let mut query = world. query :: < & A > ( ) ;
91-
92- bencher. iter ( || {
93- for i in 0 ..entity_count {
94- let entity = Entity :: new ( i) ;
95- assert ! ( query. get( & world, entity) . is_ok( ) ) ;
96- }
97- } ) ;
98- } ,
99- ) ;
100- }
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+ } ) ;
101109 }
102110
103111 group. finish ( ) ;
@@ -109,24 +117,32 @@ fn world_query_iter(criterion: &mut Criterion) {
109117 group. measurement_time ( std:: time:: Duration :: from_secs ( 4 ) ) ;
110118
111119 for entity_count in RANGE . map ( |i| i * 10_000 ) {
112- for storage in [ StorageType :: Table , StorageType :: SparseSet ] {
113- group. bench_function (
114- format ! ( "{}_entities_{:?}" , entity_count, storage) ,
115- |bencher| {
116- let mut world = setup ( entity_count, storage) ;
117- let mut query = world. query :: < & A > ( ) ;
118-
119- bencher. iter ( || {
120- let mut count = 0 ;
121- for comp in query. iter ( & world) {
122- black_box ( comp) ;
123- count += 1 ;
124- }
125- assert_eq ! ( black_box( count) , entity_count) ;
126- } ) ;
127- } ,
128- ) ;
129- }
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+ } ) ;
130146 }
131147
132148 group. finish ( ) ;
@@ -138,24 +154,32 @@ fn world_query_for_each(criterion: &mut Criterion) {
138154 group. measurement_time ( std:: time:: Duration :: from_secs ( 4 ) ) ;
139155
140156 for entity_count in RANGE . map ( |i| i * 10_000 ) {
141- for storage in [ StorageType :: Table , StorageType :: SparseSet ] {
142- group. bench_function (
143- format ! ( "{}_entities_{:?}" , entity_count, storage) ,
144- |bencher| {
145- let mut world = setup ( entity_count, storage) ;
146- let mut query = world. query :: < & A > ( ) ;
147-
148- bencher. iter ( || {
149- let mut count = 0 ;
150- query. for_each ( & world, |comp| {
151- black_box ( comp) ;
152- count += 1 ;
153- } ) ;
154- assert_eq ! ( black_box( count) , entity_count) ;
155- } ) ;
156- } ,
157- ) ;
158- }
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+ } ) ;
159183 }
160184
161185 group. finish ( ) ;
0 commit comments