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

Improve performance for long strings by preallocating the needed memory #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Improve performance for long strings by preallocating the needed memory #9

wants to merge 1 commit into from

Conversation

iscgar
Copy link

@iscgar iscgar commented Dec 27, 2016

Instead of causing a regrow of the String's underlying buffer by the push loop and then by pushing s, use with_capacity to preallocate the memory.

@iscgar
Copy link
Author

iscgar commented Dec 27, 2016

Oops. When I forked the repository #6 didn't exist, so I created this PR.
Sorry. Choose whichever suits you best (though I should note that my version was consistently a few ns faster in my benchmarks).

@iscgar
Copy link
Author

iscgar commented Dec 27, 2016

This is actually silly, because len() returns the amount of bytes the str occupies, where people expect it to work on Unicode codepoints (.chars().chount() does that, but with UTF-8 it takes O(n) time). Rust is doing the right thing here, because the "length" of a string doesn't really mean anything. In fact, when people talk about string length they actually mean the amount of grapheme clusters, which most string libraries don't provide (the unicode-segmentation crate provides that functionality, but it's even slower).

Just an example of how even a simple thing like leftpad is incorrectly defined 😆

@hfiguiere
Copy link
Owner

leftpad-rs was written as a joke. So hasty that the first implementation didn't even match what I was trying to clone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants