Skip to content

Commit b099b1e

Browse files
committed
Make std::istr::push_byte efficient
It used to allocate two (!) heap values per pushed byte. It now goes through a runtime function that simply grows the istr and writes the byte.
1 parent 14567c5 commit b099b1e

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

src/lib/istr.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ as_buf, push_byte;
88

99
export from_estr, to_estr, from_estrs, to_estrs;
1010

11+
native "rust" mod rustrt {
12+
fn rust_istr_push(s: &mutable istr, ch: u8);
13+
}
14+
1115
fn from_estr(s: &str) -> istr {
1216
let s2 = ~"";
1317
for u in s {
@@ -361,12 +365,12 @@ fn pop_byte(s: &mutable istr) -> u8 {
361365
}
362366

363367
fn push_byte(s: &mutable istr, b: u8) {
364-
s += unsafe_from_byte(b);
368+
rustrt::rust_istr_push(s, b);
365369
}
366370

367371
fn push_bytes(s: &mutable istr, bytes: &[u8]) {
368372
for byte in bytes {
369-
push_byte(s, byte);
373+
rustrt::rust_istr_push(s, byte);
370374
}
371375
}
372376

src/rt/rust_builtin.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ vec_from_buf_shared(rust_task *task, type_desc *ty,
243243
return v;
244244
}
245245

246+
extern "C" CDECL void
247+
rust_istr_push(rust_task* task, rust_vec** sp, uint8_t byte) {
248+
size_t fill = (*sp)->fill;
249+
reserve_vec(task, sp, fill + 1);
250+
(*sp)->data[fill-1] = byte;
251+
(*sp)->data[fill] = 0;
252+
(*sp)->fill = fill + 1;
253+
}
254+
246255
extern "C" CDECL rust_str *
247256
str_from_cstr(rust_task *task, char *sbuf)
248257
{

src/rt/rust_upcall.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ upcall_vec_grow(rust_task* task, rust_vec** vp, size_t new_sz) {
353353

354354
extern "C" CDECL void
355355
upcall_vec_push(rust_task* task, rust_vec** vp, type_desc* elt_ty,
356-
void* elt) {
356+
void* elt) {
357357
LOG_UPCALL_ENTRY(task);
358358
size_t new_sz = (*vp)->fill + elt_ty->size;
359359
reserve_vec(task, vp, new_sz);
@@ -362,7 +362,6 @@ upcall_vec_push(rust_task* task, rust_vec** vp, type_desc* elt_ty,
362362
v->fill += elt_ty->size;
363363
}
364364

365-
366365
/**
367366
* Returns a token that can be used to deallocate all of the allocated space
368367
* space in the dynamic stack.

src/rt/rustrt.def.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ upcall_get_type_desc
8484
upcall_grow_task
8585
upcall_vec_grow
8686
upcall_vec_push
87+
upcall_istr_push
8788
upcall_kill
8889
upcall_log_double
8990
upcall_log_float

0 commit comments

Comments
 (0)