Commit 5754300
authored
Fix excessive monomorphization of
## Objective
Reduce code bloat caused by excessive monomorphization.
## Background
`bevy_asset::Dir::insert` has the following signature:
```rust
fn insert_asset(&self, path: &Path, value: impl Into<Value>)
```
The `value` parameter is usually a `Vec<u8>` or `&'static [u8; N]`,
which is conveniently handled by `Into<Value>`.
`Dir::insert_asset` is called by the `embedded_asset!` macro via
`EmbeddedAssetRegistry::insert_asset`:
```rust
embedded.insert_asset(watched_path, &path, include_bytes!($path));
```
`include_bytes!` returns a `&'static [u8; N]`, which goes to the `value`
parameter of `insert_asset`. This means that `insert_asset` gets
monomorphized many times, since N is part of the generic type via `impl
From<&'static [u8; N]> for Value`.
```
> cargo bloat --example 3d_scene --profile release --filter "insert_asset" -n 10
File .text Size Crate Name
0.0% 0.0% 1.1KiB bevy_anti_alias bevy_asset::io::embedded::EmbeddedAssetRegistry::insert_asset
0.0% 0.0% 1.1KiB bevy_anti_alias bevy_asset::io::embedded::EmbeddedAssetRegistry::insert_asset
0.0% 0.0% 1.1KiB bevy_anti_alias bevy_asset::io::embedded::EmbeddedAssetRegistry::insert_asset
0.0% 0.0% 1.1KiB bevy_anti_alias bevy_asset::io::embedded::EmbeddedAssetRegistry::insert_asset
0.0% 0.0% 1.1KiB bevy_anti_alias bevy_asset::io::embedded::EmbeddedAssetRegistry::insert_asset
0.0% 0.0% 1.1KiB bevy_anti_alias bevy_asset::io::embedded::EmbeddedAssetRegistry::insert_asset
0.0% 0.0% 983B bevy_core_pipeline bevy_asset::io::memory::Dir::insert_asset
0.0% 0.0% 983B bevy_core_pipeline bevy_asset::io::memory::Dir::insert_asset
0.0% 0.0% 983B bevy_core_pipeline bevy_asset::io::memory::Dir::insert_asset
0.0% 0.0% 983B bevy_core_pipeline bevy_asset::io::memory::Dir::insert_asset
0.1% 0.2% 106.3KiB And 111 smaller methods. Use -n N to show more.
0.1% 0.2% 116.5KiB filtered data size, the file size is 79.3MiB
```
## Solution
The fix is to split `insert_asset` into a generic front-end and a
non-generic back-end:
```rust
pub fn insert_asset(&self, path: &Path, value: impl Into<Value>) {
self.insert_asset_internal(path, value.into());
}
fn insert_asset_internal(&self, path: &Path, value: Value) {
...
}
```
This drops a release build of `3d_scene` from 81,190KB to 81,029KB
(-161KB, -0.2%).
## Testing
```sh
cargo test -p bevy_asset
cargo run --example 3d_scene
cargo run --example 3d_scene --features "embedded_watcher"
```Dir::insert_asset (#24015)1 parent edf38ba commit 5754300
2 files changed
Lines changed: 20 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
43 | 49 | | |
44 | 50 | | |
45 | 51 | | |
46 | 52 | | |
47 | 53 | | |
48 | 54 | | |
49 | 55 | | |
50 | | - | |
| 56 | + | |
51 | 57 | | |
52 | 58 | | |
53 | 59 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
51 | 57 | | |
52 | 58 | | |
53 | 59 | | |
| |||
59 | 65 | | |
60 | 66 | | |
61 | 67 | | |
62 | | - | |
| 68 | + | |
63 | 69 | | |
64 | 70 | | |
65 | 71 | | |
| |||
82 | 88 | | |
83 | 89 | | |
84 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
85 | 96 | | |
86 | 97 | | |
87 | 98 | | |
| |||
93 | 104 | | |
94 | 105 | | |
95 | 106 | | |
96 | | - | |
| 107 | + | |
97 | 108 | | |
98 | 109 | | |
99 | 110 | | |
| |||
0 commit comments