Skip to content

Commit b7b66b6

Browse files
committed
core: Don't call into the runtime to reserve if we have capacity
1 parent c0a9979 commit b7b66b6

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/libcore/str.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,9 @@ capacity, then no action is taken.
15041504
* n - The number of bytes to reserve space for
15051505
"]
15061506
fn reserve(&s: str, n: uint) {
1507-
rustrt::str_reserve_shared(s, n);
1507+
if capacity(s) < n {
1508+
rustrt::str_reserve_shared(s, n);
1509+
}
15081510
}
15091511

15101512
#[doc = "

src/libcore/vec.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ capacity, then no action is taken.
113113
* n - The number of elements to reserve space for
114114
"]
115115
fn reserve<T>(&v: [const T], n: uint) {
116-
rustrt::vec_reserve_shared(sys::get_type_desc::<T>(), v, n);
116+
// Only make the (slow) call into the runtime if we have to
117+
if capacity(v) < n {
118+
rustrt::vec_reserve_shared(sys::get_type_desc::<T>(), v, n);
119+
}
117120
}
118121

119122
#[doc = "

0 commit comments

Comments
 (0)