Skip to content

Commit d3dfe87

Browse files
killerswanbrson
authored andcommitted
Making vec::reserve reserve precisely the size given (untested)
1 parent 9f0239c commit d3dfe87

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/rt/rust_builtin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ extern "C" CDECL void
102102
vec_reserve_shared(type_desc* ty, rust_vec** vp,
103103
size_t n_elts) {
104104
rust_task *task = rust_task_thread::get_task();
105-
reserve_vec(task, vp, n_elts * ty->size);
105+
reserve_vec_exact(task, vp, n_elts * ty->size);
106106
}
107107

108108
/**

src/rt/rust_util.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,17 @@ inline size_t vec_size(size_t elems) {
178178
return sizeof(rust_vec) + sizeof(T) * elems;
179179
}
180180

181-
inline void reserve_vec(rust_task* task, rust_vec** vpp, size_t size) {
181+
inline void reserve_vec_exact(rust_task* task, rust_vec** vpp, size_t size) {
182182
if (size > (*vpp)->alloc) {
183-
size_t new_alloc = next_power_of_two(size);
184-
*vpp = (rust_vec*)task->kernel->realloc(*vpp, new_alloc +
185-
sizeof(rust_vec));
186-
(*vpp)->alloc = new_alloc;
183+
*vpp = (rust_vec*)task->kernel->realloc(*vpp, size + sizeof(rust_vec));
184+
(*vpp)->alloc = size;
187185
}
188186
}
189187

188+
inline void reserve_vec(rust_task* task, rust_vec** vpp, size_t size) {
189+
reserve_vec_exact(task, vpp, next_power_of_two(size));
190+
}
191+
190192
typedef rust_vec rust_str;
191193

192194
inline rust_str *

0 commit comments

Comments
 (0)