1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 23:00:04 -04:00

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<str>),
}
```

which is used everywhere text is needed, with
`impl Into<Estring>` 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`)
This commit is contained in:
Emil Ernerfeldt
2021-09-03 22:26:24 +02:00
parent 3b75a84d3b
commit b3e41e4e9c
36 changed files with 413 additions and 225 deletions

View File

@@ -57,7 +57,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
ui.label("the quick brown fox jumps over the lazy dog");
})
});
c.bench_function("label format!", |b| {
c.bench_function("label String", |b| {
b.iter(|| {
ui.label("the quick brown fox jumps over the lazy dog".to_owned());
})
@@ -77,20 +77,16 @@ pub fn criterion_benchmark(c: &mut Criterion) {
b.iter(|| {
use egui::epaint::text::{layout, LayoutJob};
let job = LayoutJob::simple(
LOREM_IPSUM_LONG.to_owned(),
egui::TextStyle::Body,
color,
wrap_width,
);
let job =
LayoutJob::simple(LOREM_IPSUM_LONG, egui::TextStyle::Body, color, wrap_width);
layout(&fonts, job.into())
})
});
c.bench_function("text_layout_cached", |b| {
b.iter(|| fonts.layout(LOREM_IPSUM_LONG.to_owned(), text_style, color, wrap_width))
b.iter(|| fonts.layout(LOREM_IPSUM_LONG, text_style, color, wrap_width))
});
let galley = fonts.layout(LOREM_IPSUM_LONG.to_owned(), text_style, color, wrap_width);
let galley = fonts.layout(LOREM_IPSUM_LONG, text_style, color, wrap_width);
let mut tessellator = egui::epaint::Tessellator::from_options(Default::default());
let mut mesh = egui::epaint::Mesh::default();
c.bench_function("tessellate_text", |b| {