Skip to content

Commit 1172074

Browse files
committed
Make performance of String::insert_str more precise
1 parent 87b4541 commit 1172074

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
@@ -1489,10 +1489,11 @@ impl String {
14891489
Some(ch)
14901490
}
14911491

1492-
/// Removes a [`char`] from this `String` at a byte position and returns it.
1492+
/// Removes a [`char`] from this `String` at byte position `idx` and returns it.
14931493
///
1494-
/// This is an *O*(*n*) operation, as it requires copying every element in the
1495-
/// buffer.
1494+
/// Copies all bytes after the removed char to new positions.
1495+
///
1496+
/// NB Calling this in a loop can result in quadratic behavior.
14961497
///
14971498
/// # Panics
14981499
///
@@ -1678,10 +1679,14 @@ impl String {
16781679
drop(guard);
16791680
}
16801681

1681-
/// Inserts a character into this `String` at a byte position.
1682+
/// Inserts a character into this `String` at byte position `idx`.
1683+
///
1684+
/// Reallocates if `self.capacity()` is insufficient,
1685+
/// which may involve copying all `self.capacity()` bytes.
1686+
/// Makes space for the insertion,
1687+
/// by copying all bytes of `&self[idx..]` to new positions.
16821688
///
1683-
/// This is an *O*(*n*) operation as it requires copying every element in the
1684-
/// buffer.
1689+
/// NB Calling this in a loop can result in quadratic behavior.
16851690
///
16861691
/// # Panics
16871692
///
@@ -1733,10 +1738,14 @@ impl String {
17331738
}
17341739
}
17351740

1736-
/// Inserts a string slice into this `String` at a byte position.
1741+
/// Inserts a string slice into this `String` at byte position `idx`.
1742+
///
1743+
/// Reallocates if `self.capacity()` is insufficient,
1744+
/// which may involve copying all `self.capacity()` bytes.
1745+
/// Makes space for the insertion,
1746+
/// by copying all bytes of `&self[idx..]` to new positions.
17371747
///
1738-
/// This is an *O*(*n*) operation as it requires copying every element in the
1739-
/// buffer.
1748+
/// NB Calling this in a loop can result in quadratic behavior.
17401749
///
17411750
/// # Panics
17421751
///

0 commit comments

Comments
 (0)