From b3e41e4e9c29d91899112329b597f38806c18b85 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 3 Sep 2021 22:26:24 +0200 Subject: [PATCH] Use new type Estring to avoid cloning &'static str `ui.label("static string")` is a very common use case, and currently egui clones the string in these cases. This PR introduces a new type: ``` rust pub enum Estring { Static(&'static str), Owned(Arc), } ``` which is used everywhere text is needed, with `impl Into` in the API for e.g. `ui.label`. This reduces the number of copies drastically and speeds up the benchmark demo_with_tessellate__realistic by 17%. This hurts the ergonomics of egui a bit, and this is a breaking change. For instance, this used to work: ``` rust fn my_label(ui: &mut egui::Ui, text: &str) { ui.label(text); } ``` This must now either be changed to ``` rust fn my_label(ui: &mut egui::Ui, text: &str) { ui.label(text.to_string()); } ``` (or the argument must be changed to either `text: &'static str` or `text: String`) --- egui/src/containers/collapsing_header.rs | 2 +- egui/src/containers/combo_box.rs | 16 +- egui/src/containers/popup.rs | 2 +- egui/src/containers/window.rs | 5 +- egui/src/layout.rs | 4 +- egui/src/lib.rs | 6 +- egui/src/menu.rs | 7 +- egui/src/painter.rs | 27 +-- egui/src/placer.rs | 2 +- egui/src/response.rs | 7 +- egui/src/style.rs | 2 +- egui/src/ui.rs | 22 +-- egui/src/widgets/button.rs | 21 +-- egui/src/widgets/drag_value.rs | 14 +- egui/src/widgets/hyperlink.rs | 21 +-- egui/src/widgets/label.rs | 27 ++- egui/src/widgets/mod.rs | 4 +- egui/src/widgets/plot/items.rs | 71 ++++--- egui/src/widgets/progress_bar.rs | 11 +- egui/src/widgets/selected_label.rs | 7 +- egui/src/widgets/slider.rs | 20 +- egui/src/widgets/text_edit.rs | 7 +- egui_demo_lib/benches/benchmark.rs | 14 +- egui_demo_lib/src/apps/color_test.rs | 10 +- egui_demo_lib/src/apps/demo/code_editor.rs | 10 +- egui_demo_lib/src/apps/demo/font_book.rs | 6 +- .../src/apps/demo/misc_demo_window.rs | 17 +- egui_demo_lib/src/apps/demo/widget_gallery.rs | 2 +- egui_demo_lib/src/apps/http_app.rs | 12 +- .../src/easy_mark/easy_mark_viewer.rs | 6 +- egui_demo_lib/src/wrap_app.rs | 2 +- epaint/src/shape.rs | 7 +- epaint/src/text/estring.rs | 173 ++++++++++++++++++ epaint/src/text/fonts.rs | 8 +- epaint/src/text/mod.rs | 2 + epaint/src/text/text_layout_types.rs | 64 +++++-- 36 files changed, 413 insertions(+), 225 deletions(-) create mode 100644 epaint/src/text/estring.rs diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index 8681007f..85e747cb 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -152,7 +152,7 @@ impl CollapsingHeader { /// If the label is unique and static this is fine, /// but if it changes or there are several `CollapsingHeader` with the same title /// you need to provide a unique id source with [`Self::id_source`]. - pub fn new(label: impl ToString) -> Self { + pub fn new(label: impl Into) -> Self { let label = Label::new(label).wrap(false); let id_source = Id::new(label.text()); Self { diff --git a/egui/src/containers/combo_box.rs b/egui/src/containers/combo_box.rs index 40480f5c..5a63b298 100644 --- a/egui/src/containers/combo_box.rs +++ b/egui/src/containers/combo_box.rs @@ -23,7 +23,7 @@ use epaint::Shape; pub struct ComboBox { id_source: Id, label: Option