Skip to content

Commit cea703d

Browse files
committed
Make performance of String::insert_str more precise
1 parent 4d30011 commit cea703d

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

library/alloc/src/string.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,10 +1486,11 @@ impl String {
14861486
Some(ch)
14871487
}
14881488

1489-
/// Removes a [`char`] from this `String` at a byte position and returns it.
1489+
/// Removes a [`char`] from this `String` at byte position `idx` and returns it.
14901490
///
1491-
/// This is an *O*(*n*) operation, as it requires copying every element in the
1492-
/// buffer.
1491+
/// Copies all bytes after the removed char to new positions.
1492+
///
1493+
/// NB Calling this in a loop can result in quadratic behavior.
14931494
///
14941495
/// # Panics
14951496
///
@@ -1675,10 +1676,14 @@ impl String {
16751676
drop(guard);
16761677
}
16771678

1678-
/// Inserts a character into this `String` at a byte position.
1679+
/// Inserts a character into this `String` at byte position `idx`.
1680+
///
1681+
/// Reallocates if `self.capacity()` is insufficient,
1682+
/// which may involve copying all `self.capacity()` bytes.
1683+
/// Makes space for the insertion,
1684+
/// by copying all bytes of `&self[idx..]` to new positions.
16791685
///
1680-
/// This is an *O*(*n*) operation as it requires copying every element in the
1681-
/// buffer.
1686+
/// NB Calling this in a loop can result in quadratic behavior.
16821687
///
16831688
/// # Panics
16841689
///
@@ -1723,10 +1728,14 @@ impl String {
17231728
}
17241729
}
17251730

1726-
/// Inserts a string slice into this `String` at a byte position.
1731+
/// Inserts a string slice into this `String` at byte position `idx`.
1732+
///
1733+
/// Reallocates if `self.capacity()` is insufficient,
1734+
/// which may involve copying all `self.capacity()` bytes.
1735+
/// Makes space for the insertion,
1736+
/// by copying all bytes of `&self[idx..]` to new positions.
17271737
///
1728-
/// This is an *O*(*n*) operation as it requires copying every element in the
1729-
/// buffer.
1738+
/// NB Calling this in a loop can result in quadratic behavior.
17301739
///
17311740
/// # Panics
17321741
///

0 commit comments

Comments
 (0)