Skip to content

Commit 20fd0c5

Browse files
committed
auto merge of #4938 : thestinger/rust/no_zero, r=brson
I removed the unused wrappers methods named `calloc` because they relied on the malloc wrapper having a `bool zero = true` default parameter (which resulted in some accidental zeroing). Perhaps wrapping the actual calloc function would be useful, but I don't know of an existing use case that could use it so I just removed these. This gives an ~1% performance improvement for TreeMap, which does a lot of small allocations. Vectors use `realloc` which didn't zero before these changes so there's no measurable change in performance.
2 parents af2f0ef + 1a41b48 commit 20fd0c5

10 files changed

+12
-44
lines changed

src/libcore/private/exchange_alloc.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use c_malloc = libc::malloc;
1414
use c_free = libc::free;
1515
use managed::raw::{BoxHeaderRepr, BoxRepr};
1616
use cast::transmute;
17-
use ptr::{set_memory, null};
17+
use ptr::null;
1818
use intrinsic::TyDesc;
1919

2020
pub unsafe fn malloc(td: *TypeDesc, size: uint) -> *c_void {
@@ -25,10 +25,6 @@ pub unsafe fn malloc(td: *TypeDesc, size: uint) -> *c_void {
2525
let p = c_malloc(total_size as size_t);
2626
assert p.is_not_null();
2727

28-
// FIXME #4761: Would be very nice to not memset all allocations
29-
let p: *mut u8 = transmute(p);
30-
set_memory(p, 0, total_size);
31-
3228
// FIXME #3475: Converting between our two different tydesc types
3329
let td: *TyDesc = transmute(td);
3430

src/libstd/uv_ll.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ pub mod test {
12531253
as *request_wrapper;
12541254
let buf_base = get_base_from_buf(buf);
12551255
let buf_len = get_len_from_buf(buf);
1256-
let bytes = vec::from_buf(buf_base, buf_len as uint);
1256+
let bytes = vec::from_buf(buf_base, nread as uint);
12571257
let read_chan = (*client_data).read_chan.clone();
12581258
let msg_from_server = str::from_bytes(bytes);
12591259
read_chan.send(msg_from_server);
@@ -1445,15 +1445,15 @@ pub mod test {
14451445
buf_base as uint,
14461446
buf_len as uint,
14471447
nread));
1448-
let bytes = vec::from_buf(buf_base, buf_len);
1448+
let bytes = vec::from_buf(buf_base, nread as uint);
14491449
let request_str = str::from_bytes(bytes);
14501450

14511451
let client_data = get_data_for_uv_handle(
14521452
client_stream_ptr as *libc::c_void) as *tcp_server_data;
14531453

14541454
let server_kill_msg = (*client_data).server_kill_msg;
14551455
let write_req = (*client_data).server_write_req;
1456-
if (str::contains(request_str, server_kill_msg)) {
1456+
if str::contains(request_str, server_kill_msg) {
14571457
log(debug, ~"SERVER: client req contains kill_msg!");
14581458
log(debug, ~"SERVER: sending response to client");
14591459
read_stop(client_stream_ptr);

src/rt/memory_region.cpp

+3-10
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ memory_region::realloc(void *mem, size_t orig_size) {
121121
}
122122

123123
void *
124-
memory_region::malloc(size_t size, const char *tag, bool zero) {
124+
memory_region::malloc(size_t size, const char *tag) {
125+
# if RUSTRT_TRACK_ALLOCATIONS >= 1
125126
size_t old_size = size;
127+
# endif
126128
size += HEADER_SIZE;
127129
alloc_header *mem = (alloc_header *)::malloc(size);
128130
if (mem == NULL) {
@@ -143,18 +145,9 @@ memory_region::malloc(size_t size, const char *tag, bool zero) {
143145
void *data = get_data(mem);
144146
claim_alloc(data);
145147

146-
if(zero) {
147-
memset(data, 0, old_size);
148-
}
149-
150148
return data;
151149
}
152150

153-
void *
154-
memory_region::calloc(size_t size, const char *tag) {
155-
return malloc(size, tag, true);
156-
}
157-
158151
memory_region::~memory_region() {
159152
if (_synchronized) { _lock.lock(); }
160153
if (_live_allocations == 0 && !_detailed_leaks) {

src/rt/memory_region.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ class memory_region {
7777
public:
7878
memory_region(rust_env *env, bool synchronized);
7979
memory_region(memory_region *parent);
80-
void *malloc(size_t size, const char *tag, bool zero = true);
81-
void *calloc(size_t size, const char *tag);
80+
void *malloc(size_t size, const char *tag);
8281
void *realloc(void *mem, size_t size);
8382
void free(void *mem);
8483
~memory_region();

src/rt/rust_exchange_alloc.cpp

+1-9
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,15 @@
1818
uintptr_t exchange_count = 0;
1919

2020
void *
21-
rust_exchange_alloc::malloc(size_t size, bool zero) {
21+
rust_exchange_alloc::malloc(size_t size) {
2222
void *value = ::malloc(size);
2323
assert(value);
24-
if (zero) {
25-
memset(value, 0, size);
26-
}
2724

2825
sync::increment(exchange_count);
2926

3027
return value;
3128
}
3229

33-
void *
34-
rust_exchange_alloc::calloc(size_t size) {
35-
return this->malloc(size);
36-
}
37-
3830
void *
3931
rust_exchange_alloc::realloc(void *ptr, size_t size) {
4032
void *new_ptr = ::realloc(ptr, size);

src/rt/rust_exchange_alloc.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
class rust_exchange_alloc {
1818
public:
19-
void *malloc(size_t size, bool zero = true);
20-
void *calloc(size_t size);
19+
void *malloc(size_t size);
2120
void *realloc(void *mem, size_t size);
2221
void free(void *mem);
2322
};

src/rt/rust_kernel.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ rust_kernel::malloc(size_t size, const char *tag) {
7979
return exchange_alloc.malloc(size);
8080
}
8181

82-
void *
83-
rust_kernel::calloc(size_t size, const char *tag) {
84-
return exchange_alloc.calloc(size);
85-
}
86-
8782
void *
8883
rust_kernel::realloc(void *mem, size_t size) {
8984
return exchange_alloc.realloc(mem, size);

src/rt/rust_kernel.h

-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ class rust_kernel {
133133
void fatal(char const *fmt, ...);
134134

135135
void *malloc(size_t size, const char *tag);
136-
void *calloc(size_t size, const char *tag);
137136
void *realloc(void *mem, size_t size);
138137
void free(void *mem);
139138
rust_exchange_alloc *region() { return &exchange_alloc; }

src/rt/rust_stack.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ check_stack_canary(stk_seg *stk) {
5858
stk_seg *
5959
create_stack(memory_region *region, size_t sz) {
6060
size_t total_sz = sizeof(stk_seg) + sz;
61-
stk_seg *stk = (stk_seg *)region->malloc(total_sz, "stack", false);
61+
stk_seg *stk = (stk_seg *)region->malloc(total_sz, "stack");
6262
memset(stk, 0, sizeof(stk_seg));
6363
stk->end = (uintptr_t) &stk->data[sz];
6464
add_stack_canary(stk);
@@ -75,7 +75,7 @@ destroy_stack(memory_region *region, stk_seg *stk) {
7575
stk_seg *
7676
create_exchange_stack(rust_exchange_alloc *exchange, size_t sz) {
7777
size_t total_sz = sizeof(stk_seg) + sz;
78-
stk_seg *stk = (stk_seg *)exchange->malloc(total_sz, false);
78+
stk_seg *stk = (stk_seg *)exchange->malloc(total_sz);
7979
memset(stk, 0, sizeof(stk_seg));
8080
stk->end = (uintptr_t) &stk->data[sz];
8181
add_stack_canary(stk);

src/rt/rust_task.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,6 @@ rust_task::backtrace() {
450450
#endif
451451
}
452452

453-
void *
454-
rust_task::calloc(size_t size, const char *tag) {
455-
return local_region.calloc(size, tag);
456-
}
457-
458453
size_t
459454
rust_task::get_next_stack_size(size_t min, size_t current, size_t requested) {
460455
LOG(this, mem, "calculating new stack size for 0x%" PRIxPTR, this);

0 commit comments

Comments
 (0)