Skip to content

Commit ddce22b

Browse files
committed
Decouple some dependencies (#3886)
# Objective Reduce from scratch build time. ## Solution Reduce the size of the critical path by removing dependencies between crates where not necessary. For `cargo check --no-default-features` this reduced build time from ~51s to ~45s. For some commits I am not completely sure if the tradeoff between build time reduction and convenience caused by the commit is acceptable. If not, I can drop them.
1 parent f1aae38 commit ddce22b

File tree

16 files changed

+138
-60
lines changed

16 files changed

+138
-60
lines changed

crates/bevy_derive/src/enum_variant_meta.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use bevy_macro_utils::BevyManifest;
21
use proc_macro::{Span, TokenStream};
32
use quote::quote;
43
use syn::{parse_macro_input, Data, DeriveInput};
@@ -14,8 +13,6 @@ pub fn derive_enum_variant_meta(input: TokenStream) -> TokenStream {
1413
}
1514
};
1615

17-
let bevy_util_path = BevyManifest::default().get_path(crate::modules::BEVY_UTILS);
18-
1916
let generics = ast.generics;
2017
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
2118

@@ -25,13 +22,13 @@ pub fn derive_enum_variant_meta(input: TokenStream) -> TokenStream {
2522
let indices = 0..names.len();
2623

2724
TokenStream::from(quote! {
28-
impl #impl_generics #bevy_util_path::EnumVariantMeta for #struct_name #ty_generics #where_clause {
29-
fn enum_variant_index(&self) -> usize {
25+
impl #impl_generics #struct_name #ty_generics #where_clause {
26+
pub fn enum_variant_index(&self) -> usize {
3027
match self {
3128
#(#struct_name::#idents {..} => #indices,)*
3229
}
3330
}
34-
fn enum_variant_name(&self) -> &'static str {
31+
pub fn enum_variant_name(&self) -> &'static str {
3532
static variants: &[&str] = &[
3633
#(#names,)*
3734
];

crates/bevy_derive/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ mod app_plugin;
44
mod bevy_main;
55
mod derefs;
66
mod enum_variant_meta;
7-
mod modules;
87

98
use bevy_macro_utils::{derive_label, BevyManifest};
109
use proc_macro::TokenStream;
1110
use quote::format_ident;
1211

13-
/// Generates a dynamic plugin entry point function for the given `Plugin` type.
12+
/// Generates a dynamic plugin entry point function for the given `Plugin` type.
1413
#[proc_macro_derive(DynamicPlugin)]
1514
pub fn derive_dynamic_plugin(input: TokenStream) -> TokenStream {
1615
app_plugin::derive_dynamic_plugin(input)

crates/bevy_derive/src/modules.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/bevy_ecs/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ bevy_ecs_macros = { path = "macros", version = "0.8.0-dev" }
2222
async-channel = "1.4"
2323
fixedbitset = "0.4"
2424
fxhash = "0.2"
25-
thiserror = "1.0"
2625
downcast-rs = "1.2"
2726
serde = "1"
2827

crates/bevy_ecs/src/entity/map_entities.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
use crate::entity::Entity;
22
use bevy_utils::{Entry, HashMap};
3-
use thiserror::Error;
3+
use std::fmt;
44

5-
#[derive(Error, Debug)]
5+
#[derive(Debug)]
66
pub enum MapEntitiesError {
7-
#[error("the given entity does not exist in the map")]
87
EntityNotFound(Entity),
98
}
109

10+
impl std::error::Error for MapEntitiesError {}
11+
12+
impl fmt::Display for MapEntitiesError {
13+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14+
match self {
15+
MapEntitiesError::EntityNotFound(_) => {
16+
write!(f, "the given entity does not exist in the map")
17+
}
18+
}
19+
}
20+
}
21+
1122
pub trait MapEntities {
1223
fn map_entities(&mut self, entity_map: &EntityMap) -> Result<(), MapEntitiesError>;
1324
}

crates/bevy_ecs/src/query/state.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
};
1313
use bevy_tasks::TaskPool;
1414
use fixedbitset::FixedBitSet;
15-
use thiserror::Error;
15+
use std::fmt;
1616

1717
/// Provides scoped access to a [`World`] state according to a given [`WorldQuery`] and query filter.
1818
pub struct QueryState<Q: WorldQuery, F: WorldQuery = ()>
@@ -936,16 +936,29 @@ where
936936

937937
/// An error that occurs when retrieving a specific [`Entity`]'s query result.
938938
// TODO: return the type_name as part of this error
939-
#[derive(Error, Debug, PartialEq, Clone, Copy)]
939+
#[derive(Debug, PartialEq, Clone, Copy)]
940940
pub enum QueryEntityError {
941-
#[error("The given entity does not have the requested component.")]
942941
QueryDoesNotMatch(Entity),
943-
#[error("The requested entity does not exist.")]
944942
NoSuchEntity(Entity),
945-
#[error("The entity was requested mutably more than once.")]
946943
AliasedMutability(Entity),
947944
}
948945

946+
impl std::error::Error for QueryEntityError {}
947+
948+
impl fmt::Display for QueryEntityError {
949+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
950+
match self {
951+
QueryEntityError::QueryDoesNotMatch(_) => {
952+
write!(f, "The given entity does not have the requested component.")
953+
}
954+
QueryEntityError::NoSuchEntity(_) => write!(f, "The requested entity does not exist."),
955+
QueryEntityError::AliasedMutability(_) => {
956+
write!(f, "The entity was requested mutably more than once.")
957+
}
958+
}
959+
}
960+
}
961+
949962
#[cfg(test)]
950963
mod tests {
951964
use crate::{prelude::*, query::QueryEntityError};

crates/bevy_ecs/src/schedule/state.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ use crate::{
55
},
66
system::{In, IntoChainSystem, Local, Res, ResMut},
77
};
8-
use std::{any::TypeId, fmt::Debug, hash::Hash};
9-
use thiserror::Error;
8+
use std::{
9+
any::TypeId,
10+
fmt::{self, Debug},
11+
hash::Hash,
12+
};
1013

1114
pub trait StateData: Send + Sync + Clone + Eq + Debug + Hash + 'static {}
1215
impl<T> StateData for T where T: Send + Sync + Clone + Eq + Debug + Hash + 'static {}
@@ -399,16 +402,32 @@ where
399402
}
400403
}
401404

402-
#[derive(Debug, Error)]
405+
#[derive(Debug)]
403406
pub enum StateError {
404-
#[error("Attempted to change the state to the current state.")]
405407
AlreadyInState,
406-
#[error("Attempted to queue a state change, but there was already a state queued.")]
407408
StateAlreadyQueued,
408-
#[error("Attempted to queue a pop, but there is nothing to pop.")]
409409
StackEmpty,
410410
}
411411

412+
impl std::error::Error for StateError {}
413+
414+
impl fmt::Display for StateError {
415+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
416+
match self {
417+
StateError::AlreadyInState => {
418+
write!(f, "Attempted to change the state to the current state.")
419+
}
420+
StateError::StateAlreadyQueued => write!(
421+
f,
422+
"Attempted to queue a state change, but there was already a state queued."
423+
),
424+
StateError::StackEmpty => {
425+
write!(f, "Attempted to queue a pop, but there is nothing to pop.")
426+
}
427+
}
428+
}
429+
}
430+
412431
fn should_run_adapter<T: StateData>(In(cmp_result): In<bool>, state: Res<State<T>>) -> ShouldRun {
413432
if state.end_next_loop {
414433
return ShouldRun::No;

crates/bevy_ecs/src/system/query.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::{
99
};
1010
use bevy_tasks::TaskPool;
1111
use std::{any::TypeId, fmt::Debug};
12-
use thiserror::Error;
1312

1413
/// Provides scoped access to components in a [`World`].
1514
///
@@ -670,7 +669,7 @@ where
670669
/// for (targeting_entity, targets, origin) in targeting_query.iter(){
671670
/// // We can use "destructuring" to unpack the results nicely
672671
/// let [target_1, target_2, target_3] = targets_query.many(targets.0);
673-
///
672+
///
674673
/// assert!(target_1.distance(origin) <= 5);
675674
/// assert!(target_2.distance(origin) <= 5);
676675
/// assert!(target_3.distance(origin) <= 5);
@@ -779,10 +778,10 @@ where
779778
/// for spring in spring_query.iter(){
780779
/// // We can use "destructuring" to unpack our query items nicely
781780
/// let [(position_1, mut force_1), (position_2, mut force_2)] = mass_query.many_mut(spring.connected_entities);
782-
///
781+
///
783782
/// force_1.x += spring.strength * (position_1.x - position_2.x);
784783
/// force_1.y += spring.strength * (position_1.y - position_2.y);
785-
///
784+
///
786785
/// // Silence borrow-checker: I have split your mutable borrow!
787786
/// force_2.x += spring.strength * (position_2.x - position_1.x);
788787
/// force_2.y += spring.strength * (position_2.y - position_1.y);
@@ -1157,28 +1156,61 @@ where
11571156
}
11581157

11591158
/// An error that occurs when retrieving a specific [`Entity`]'s component from a [`Query`]
1160-
#[derive(Error, Debug)]
1159+
#[derive(Debug)]
11611160
pub enum QueryComponentError {
1162-
#[error("This query does not have read access to the requested component.")]
11631161
MissingReadAccess,
1164-
#[error("This query does not have write access to the requested component.")]
11651162
MissingWriteAccess,
1166-
#[error("The given entity does not have the requested component.")]
11671163
MissingComponent,
1168-
#[error("The requested entity does not exist.")]
11691164
NoSuchEntity,
11701165
}
11711166

1167+
impl std::error::Error for QueryComponentError {}
1168+
1169+
impl std::fmt::Display for QueryComponentError {
1170+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1171+
match self {
1172+
QueryComponentError::MissingReadAccess => {
1173+
write!(
1174+
f,
1175+
"This query does not have read access to the requested component."
1176+
)
1177+
}
1178+
QueryComponentError::MissingWriteAccess => {
1179+
write!(
1180+
f,
1181+
"This query does not have write access to the requested component."
1182+
)
1183+
}
1184+
QueryComponentError::MissingComponent => {
1185+
write!(f, "The given entity does not have the requested component.")
1186+
}
1187+
QueryComponentError::NoSuchEntity => {
1188+
write!(f, "The requested entity does not exist.")
1189+
}
1190+
}
1191+
}
1192+
}
1193+
11721194
/// An error that occurs when evaluating a [`Query`] as a single expected resulted via
11731195
/// [`Query::single`] or [`Query::single_mut`].
1174-
#[derive(Debug, Error)]
1196+
#[derive(Debug)]
11751197
pub enum QuerySingleError {
1176-
#[error("No entities fit the query {0}")]
11771198
NoEntities(&'static str),
1178-
#[error("Multiple entities fit the query {0}!")]
11791199
MultipleEntities(&'static str),
11801200
}
11811201

1202+
impl std::error::Error for QuerySingleError {}
1203+
1204+
impl std::fmt::Display for QuerySingleError {
1205+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1206+
match self {
1207+
QuerySingleError::NoEntities(query) => write!(f, "No entities fit the query {}", query),
1208+
QuerySingleError::MultipleEntities(query) => {
1209+
write!(f, "Multiple entities fit the query {}!", query)
1210+
}
1211+
}
1212+
}
1213+
}
11821214
impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F>
11831215
where
11841216
F::Fetch: FilterFetch,

crates/bevy_macro_utils/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ license = "MIT OR Apache-2.0"
99
keywords = ["bevy"]
1010

1111
[dependencies]
12-
cargo-manifest = "0.2.6"
12+
toml = "0.5.8"
1313
syn = "1.0"
1414
quote = "1.0"

crates/bevy_macro_utils/src/lib.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ pub use attrs::*;
88
pub use shape::*;
99
pub use symbol::*;
1010

11-
use cargo_manifest::{DepsSet, Manifest};
1211
use proc_macro::TokenStream;
1312
use quote::quote;
1413
use std::{env, path::PathBuf};
14+
use toml::{map::Map, Value};
1515

1616
pub struct BevyManifest {
17-
manifest: Manifest,
17+
manifest: Map<String, Value>,
1818
}
1919

2020
impl Default for BevyManifest {
@@ -24,7 +24,8 @@ impl Default for BevyManifest {
2424
.map(PathBuf::from)
2525
.map(|mut path| {
2626
path.push("Cargo.toml");
27-
Manifest::from_path(path).unwrap()
27+
let manifest = std::fs::read_to_string(path).unwrap();
28+
toml::from_str(&manifest).unwrap()
2829
})
2930
.unwrap(),
3031
}
@@ -36,13 +37,24 @@ impl BevyManifest {
3637
const BEVY: &str = "bevy";
3738
const BEVY_INTERNAL: &str = "bevy_internal";
3839

39-
let find_in_deps = |deps: &DepsSet| -> Option<syn::Path> {
40+
fn dep_package(dep: &Value) -> Option<&str> {
41+
if dep.as_str().is_some() {
42+
None
43+
} else {
44+
dep.as_table()
45+
.unwrap()
46+
.get("package")
47+
.map(|name| name.as_str().unwrap())
48+
}
49+
}
50+
51+
let find_in_deps = |deps: &Map<String, Value>| -> Option<syn::Path> {
4052
let package = if let Some(dep) = deps.get(name) {
41-
return Some(Self::parse_str(dep.package().unwrap_or(name)));
53+
return Some(Self::parse_str(dep_package(dep).unwrap_or(name)));
4254
} else if let Some(dep) = deps.get(BEVY) {
43-
dep.package().unwrap_or(BEVY)
55+
dep_package(dep).unwrap_or(BEVY)
4456
} else if let Some(dep) = deps.get(BEVY_INTERNAL) {
45-
dep.package().unwrap_or(BEVY_INTERNAL)
57+
dep_package(dep).unwrap_or(BEVY_INTERNAL)
4658
} else {
4759
return None;
4860
};
@@ -54,8 +66,14 @@ impl BevyManifest {
5466
Some(path)
5567
};
5668

57-
let deps = self.manifest.dependencies.as_ref();
58-
let deps_dev = self.manifest.dev_dependencies.as_ref();
69+
let deps = self
70+
.manifest
71+
.get("dependencies")
72+
.map(|deps| deps.as_table().unwrap());
73+
let deps_dev = self
74+
.manifest
75+
.get("dev-dependencies")
76+
.map(|deps| deps.as_table().unwrap());
5977

6078
deps.and_then(find_in_deps)
6179
.or_else(|| deps_dev.and_then(find_in_deps))

0 commit comments

Comments
 (0)