Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use generic Content in Text to avoid reallocation in fill_text #2360

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/src/renderer/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl text::Renderer for () {

fn fill_text(
&mut self,
_paragraph: Text<'_, Self::Font>,
_paragraph: Text,
_position: Point,
_color: Color,
_clip_bounds: Rectangle,
Expand All @@ -78,11 +78,11 @@ impl text::Renderer for () {
impl text::Paragraph for () {
type Font = Font;

fn with_text(_text: Text<'_, Self::Font>) -> Self {}
fn with_text(_text: Text<&str>) -> Self {}

fn resize(&mut self, _new_bounds: Size) {}

fn compare(&self, _text: Text<'_, Self::Font>) -> text::Difference {
fn compare(&self, _text: Text<&str>) -> text::Difference {
text::Difference::None
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use std::hash::{Hash, Hasher};

/// A paragraph.
#[derive(Debug, Clone, Copy)]
pub struct Text<'a, Font> {
pub struct Text<Content = String, Font = crate::Font> {
/// The content of the paragraph.
pub content: &'a str,
pub content: Content,

/// The bounds of the paragraph.
pub bounds: Size,
Expand Down Expand Up @@ -219,7 +219,7 @@ pub trait Renderer: crate::Renderer {
/// [`Color`].
fn fill_text(
&mut self,
text: Text<'_, Self::Font>,
text: Text<String, Self::Font>,
position: Point,
color: Color,
clip_bounds: Rectangle,
Expand Down
6 changes: 3 additions & 3 deletions core/src/text/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ pub trait Paragraph: Sized + Default {
type Font: Copy + PartialEq;

/// Creates a new [`Paragraph`] laid out with the given [`Text`].
fn with_text(text: Text<'_, Self::Font>) -> Self;
fn with_text(text: Text<&str, Self::Font>) -> Self;

/// Lays out the [`Paragraph`] with some new boundaries.
fn resize(&mut self, new_bounds: Size);

/// Compares the [`Paragraph`] with some desired [`Text`] and returns the
/// [`Difference`].
fn compare(&self, text: Text<'_, Self::Font>) -> Difference;
fn compare(&self, text: Text<&str, Self::Font>) -> Difference;

/// Returns the horizontal alignment of the [`Paragraph`].
fn horizontal_alignment(&self) -> alignment::Horizontal;
Expand All @@ -35,7 +35,7 @@ pub trait Paragraph: Sized + Default {
fn grapheme_position(&self, line: usize, index: usize) -> Option<Point>;

/// Updates the [`Paragraph`] to match the given [`Text`], if needed.
fn update(&mut self, text: Text<'_, Self::Font>) {
fn update(&mut self, text: Text<&str, Self::Font>) {
match self.compare(text) {
Difference::None => {}
Difference::Bounds => {
Expand Down
4 changes: 2 additions & 2 deletions graphics/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ where

fn fill_text(
&mut self,
text: Text<'_, Self::Font>,
text: Text,
position: Point,
color: Color,
clip_bounds: Rectangle,
) {
self.primitives.push(Primitive::Text {
content: text.content.to_string(),
content: text.content,
bounds: Rectangle::new(position, text.bounds),
size: text.size,
line_height: text.line_height,
Expand Down
4 changes: 2 additions & 2 deletions graphics/src/text/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Paragraph {
impl core::text::Paragraph for Paragraph {
type Font = Font;

fn with_text(text: Text<'_, Font>) -> Self {
fn with_text(text: Text<&str>) -> Self {
log::trace!("Allocating paragraph: {}", text.content);

let mut font_system =
Expand Down Expand Up @@ -146,7 +146,7 @@ impl core::text::Paragraph for Paragraph {
}
}

fn compare(&self, text: Text<'_, Font>) -> core::text::Difference {
fn compare(&self, text: Text<&str>) -> core::text::Difference {
let font_system = text::font_system().read().expect("Read font system");
let paragraph = self.internal();
let metrics = paragraph.buffer.metrics();
Expand Down
2 changes: 1 addition & 1 deletion renderer/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ where

fn fill_text(
&mut self,
text: core::Text<'_, Self::Font>,
text: core::Text<String, Self::Font>,
position: Point,
color: Color,
clip_bounds: Rectangle,
Expand Down
2 changes: 1 addition & 1 deletion widget/src/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ where
if self.is_checked {
renderer.fill_text(
text::Text {
content: &code_point.to_string(),
content: code_point.to_string(),
font: *font,
size,
line_height: *line_height,
Expand Down
2 changes: 1 addition & 1 deletion widget/src/overlay/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ where

renderer.fill_text(
Text {
content: &option.to_string(),
content: option.to_string(),
Copy link
Member Author

@hecrj hecrj Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No more (&option.to_string()).to_string()! I will sleep tight tonight.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, since you're looking into this .to_string reallocations couldn't the text helper function avoid reallocation as well here:
Text::new(text.to_string())
I always tend to use Text::new(&self.some_str) instead of text(&self.some_str) because of this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not if we want to keep the ToString compatibility (e.g. text(10)). Specialization hasn't landed yet in Rust.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't think of that. I guess I can always make my own helper! 😄

bounds: Size::new(f32::INFINITY, bounds.height),
size: text_size,
line_height: self.text_line_height,
Expand Down
4 changes: 2 additions & 2 deletions widget/src/pick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ where

renderer.fill_text(
Text {
content: &code_point.to_string(),
content: code_point.to_string(),
size,
line_height,
font,
Expand All @@ -502,7 +502,7 @@ where

let label = selected.map(ToString::to_string);

if let Some(label) = label.as_deref().or(self.placeholder.as_deref()) {
if let Some(label) = label.or_else(|| self.placeholder.clone()) {
let text_size =
self.text_size.unwrap_or_else(|| renderer.default_size());

Expand Down
6 changes: 4 additions & 2 deletions widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ where
let placeholder_text = Text {
font,
line_height: self.line_height,
content: &self.placeholder,
content: self.placeholder.as_str(),
bounds: Size::new(f32::INFINITY, text_bounds.height),
size: text_size,
horizontal_alignment: alignment::Horizontal::Left,
Expand All @@ -251,9 +251,11 @@ where
});

if let Some(icon) = &self.icon {
let mut content = [0; 4];

let icon_text = Text {
line_height: self.line_height,
content: &icon.code_point.to_string(),
content: icon.code_point.encode_utf8(&mut content) as &_,
font: icon.font,
size: icon.size.unwrap_or_else(|| renderer.default_size()),
bounds: Size::new(f32::INFINITY, text_bounds.height),
Expand Down
Loading