-
Notifications
You must be signed in to change notification settings - Fork 1.7k
optimize performance of the repeat function (up to 50% faster) #14697
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
Conversation
Hi, @alamb. In addition, thank you very much for your help, but I have tried some suggestions in #14610 to reduce memory copy by using the
for _ in 0..number {
write!(builder, "{}", string)?;
}
builder.append_value("") |
That is an interesting finding -- I see that std::String::repeat uses slice::repeat which seems quite optimized. Thanks for testing it out. If avoiding the allocation doesn't help then that is the end of that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @zjregee
(Some(string), Some(number)) if number >= 0 => { | ||
if number as usize * string.len() > max_str_len { | ||
let item_capcaity = string.len() * number as usize; | ||
if item_capcaity > max_str_len { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo item_capcaity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! It has been updated.
Thanks again @Dandandan and @zjregee |
Which issue does this PR close?
repeat
function #14610.Rationale for this change
What changes are included in this PR?
By calculating the length in advance and allocating memory accordingly, the repeat function achieves improved performance to a certain degree.
The following are the local test results:
Are these changes tested?
Yes.
Are there any user-facing changes?
None.