diff --git a/Cargo.lock b/Cargo.lock index 6b62a62a..047903a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1265,6 +1265,7 @@ dependencies = [ "egui", "image", "log", + "puffin", "resvg", "serde", "tiny-skia", diff --git a/crates/egui_extras/Cargo.toml b/crates/egui_extras/Cargo.toml index fe3bf887..23583a22 100644 --- a/crates/egui_extras/Cargo.toml +++ b/crates/egui_extras/Cargo.toml @@ -29,12 +29,17 @@ default = [] ## Enable [`DatePickerButton`] widget. datepicker = ["chrono"] -## Support loading svg images. -svg = ["resvg", "tiny-skia", "usvg"] - ## Log warnings using [`log`](https://docs.rs/log) crate. log = ["dep:log", "egui/log"] +## Enable profiling with the [`puffin`](https://docs.rs/puffin) crate. +## +## Only enabled on native, because of the low resolution (1ms) of clocks in browsers. +puffin = ["dep:puffin", "egui/puffin"] + +## Support loading svg images. +svg = ["resvg", "tiny-skia", "usvg"] + [dependencies] egui = { version = "0.22.0", path = "../egui", default-features = false } @@ -65,6 +70,8 @@ image = { version = "0.24", optional = true, default-features = false } # feature "log" log = { version = "0.4", optional = true, features = ["std"] } +puffin = { version = "0.16", optional = true } + # svg feature resvg = { version = "0.28", optional = true, default-features = false } tiny-skia = { version = "0.8", optional = true, default-features = false } # must be updated in lock-step with resvg diff --git a/crates/egui_extras/src/image.rs b/crates/egui_extras/src/image.rs index bec11d1a..9304f2b8 100644 --- a/crates/egui_extras/src/image.rs +++ b/crates/egui_extras/src/image.rs @@ -203,6 +203,7 @@ use egui::ColorImage; /// On invalid image or unsupported image format. #[cfg(feature = "image")] pub fn load_image_bytes(image_bytes: &[u8]) -> Result { + crate::profile_function!(); let image = image::load_from_memory(image_bytes).map_err(|err| err.to_string())?; let size = [image.width() as _, image.height() as _]; let image_buffer = image.to_rgba8(); @@ -235,6 +236,7 @@ pub fn load_svg_bytes_with_size( svg_bytes: &[u8], fit_to: FitTo, ) -> Result { + crate::profile_function!(); let opt = usvg::Options::default(); let rtree = usvg::Tree::from_data(svg_bytes, &opt).map_err(|err| err.to_string())?; diff --git a/crates/egui_extras/src/lib.rs b/crates/egui_extras/src/lib.rs index 243127f2..29475c14 100644 --- a/crates/egui_extras/src/lib.rs +++ b/crates/egui_extras/src/lib.rs @@ -28,6 +28,38 @@ pub use crate::sizing::Size; pub use crate::strip::*; pub use crate::table::*; +// --------------------------------------------------------------------------- + +mod profiling_scopes { + #![allow(unused_macros)] + #![allow(unused_imports)] + + /// Profiling macro for feature "puffin" + macro_rules! profile_function { + ($($arg: tt)*) => { + #[cfg(not(target_arch = "wasm32"))] // Disabled on web because of the coarse 1ms clock resolution there. + #[cfg(feature = "puffin")] + puffin::profile_function!($($arg)*); + }; + } + pub(crate) use profile_function; + + /// Profiling macro for feature "puffin" + macro_rules! profile_scope { + ($($arg: tt)*) => { + #[cfg(not(target_arch = "wasm32"))] // Disabled on web because of the coarse 1ms clock resolution there. + #[cfg(feature = "puffin")] + puffin::profile_scope!($($arg)*); + }; + } + pub(crate) use profile_scope; +} + +#[allow(unused_imports)] +pub(crate) use profiling_scopes::*; + +// --------------------------------------------------------------------------- + /// Log an error with either `log` or `eprintln` macro_rules! log_err { ($fmt: literal, $($arg: tt)*) => {{