Skip to content

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

Merged
hecrj merged 1 commit intomasterfrom
avoid-fill-text-allocation
Apr 1, 2024
Merged

Use generic Content in Text to avoid reallocation in fill_text#2360
hecrj merged 1 commit intomasterfrom
avoid-fill-text-allocation

Conversation

@hecrj
Copy link
Member

@hecrj hecrj commented Apr 1, 2024

Renderer::fill_text asks for a &str just to turn it into a String... Amazing.

Let's get rid of that, shall we?

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! 😄

@hecrj hecrj enabled auto-merge April 1, 2024 09:40
@hecrj hecrj merged commit 14ed71e into master Apr 1, 2024
@hecrj hecrj deleted the avoid-fill-text-allocation branch April 1, 2024 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments