Avoid allocations for isort module names (#11251)

## Summary

Random refactor I noticed when investigating the F401 changes. We don't
need to allocate in most cases here.
This commit is contained in:
Charlie Marsh 2024-05-02 12:17:56 -07:00 committed by GitHub
parent 3a7c01b365
commit 9a1f6f6762
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 50 additions and 35 deletions

View file

@ -715,17 +715,21 @@ where
/// assert_eq!(format_import_from(1, None), ".".to_string());
/// assert_eq!(format_import_from(1, Some("foo")), ".foo".to_string());
/// ```
pub fn format_import_from(level: u32, module: Option<&str>) -> String {
let mut module_name = String::with_capacity(16);
if level > 0 {
for _ in 0..level {
module_name.push('.');
pub fn format_import_from(level: u32, module: Option<&str>) -> Cow<str> {
match (level, module) {
(0, Some(module)) => Cow::Borrowed(module),
(level, module) => {
let mut module_name =
String::with_capacity((level as usize) + module.map_or(0, str::len));
for _ in 0..level {
module_name.push('.');
}
if let Some(module) = module {
module_name.push_str(module);
}
Cow::Owned(module_name)
}
}
if let Some(module) = module {
module_name.push_str(module);
}
module_name
}
/// Format the member reference name for a relative import.
@ -740,9 +744,8 @@ pub fn format_import_from(level: u32, module: Option<&str>) -> String {
/// assert_eq!(format_import_from_member(1, Some("foo"), "bar"), ".foo.bar".to_string());
/// ```
pub fn format_import_from_member(level: u32, module: Option<&str>, member: &str) -> String {
let mut qualified_name = String::with_capacity(
(level as usize) + module.as_ref().map_or(0, |module| module.len()) + 1 + member.len(),
);
let mut qualified_name =
String::with_capacity((level as usize) + module.map_or(0, str::len) + 1 + member.len());
if level > 0 {
for _ in 0..level {
qualified_name.push('.');