Skip to content

Commit d23cd8f

Browse files
committed
rt: Don't hit TLS on upcall_vec_push unless necessary
1 parent a3fdd8c commit d23cd8f

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/rt/rust_upcall.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ upcall_vec_grow(rust_vec** vp, size_t new_sz) {
434434
// Copy elements from one vector to another,
435435
// dealing with reference counts
436436
static inline void
437-
copy_elements(rust_task *task, type_desc *elem_t,
437+
copy_elements(type_desc *elem_t,
438438
void *pdst, void *psrc, size_t n) {
439439
char *dst = (char *)pdst, *src = (char *)psrc;
440440
memmove(dst, src, n);
@@ -454,12 +454,10 @@ extern "C" CDECL void
454454
upcall_vec_push(rust_vec** vp, type_desc* elt_ty, void* elt) {
455455
// NB: This runs entirely on the Rust stack because it invokes take glue
456456

457-
rust_task *task = rust_task_thread::get_task();
458-
459457
size_t new_sz = (*vp)->fill + elt_ty->size;
460-
reserve_vec(task, vp, new_sz);
458+
reserve_vec_fast(vp, new_sz);
461459
rust_vec* v = *vp;
462-
copy_elements(task, elt_ty, &v->data[0] + v->fill,
460+
copy_elements(elt_ty, &v->data[0] + v->fill,
463461
elt, elt_ty->size);
464462
v->fill += elt_ty->size;
465463
}

src/rt/rust_util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ inline void reserve_vec(rust_task* task, rust_vec** vpp, size_t size) {
189189
reserve_vec_exact(task, vpp, next_power_of_two(size));
190190
}
191191

192+
// Call this when you don't already have a task pointer and it will
193+
// avoid hitting the TLS if it doesn't have to
194+
inline void reserve_vec_fast(rust_vec **vpp, size_t size) {
195+
size_t new_size = next_power_of_two(size);
196+
if (new_size > (*vpp)->alloc) {
197+
rust_task *task = rust_task_thread::get_task();
198+
size_t alloc_size = new_size + sizeof(rust_vec);
199+
*vpp = (rust_vec*)task->kernel->realloc(*vpp, alloc_size);
200+
(*vpp)->alloc = new_size;
201+
}
202+
}
203+
192204
typedef rust_vec rust_str;
193205

194206
inline rust_str *

0 commit comments

Comments
 (0)