From 85cf5dd097226465334821274596b7c7b7ed80a4 Mon Sep 17 00:00:00 2001 From: theodus Date: Sat, 2 Feb 2019 14:47:06 -0500 Subject: [PATCH 1/3] Avoid unnecessary allocation from realloc --- src/mem/slab.h | 2 +- src/override/malloc.cc | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/mem/slab.h b/src/mem/slab.h index 6387dc636..67f9712d9 100644 --- a/src/mem/slab.h +++ b/src/mem/slab.h @@ -71,7 +71,7 @@ namespace snmalloc meta.debug_slab_invariant(is_short(), this); - if (zero_mem == YesZero) + if constexpr (zero_mem == YesZero) { if (rsize < PAGE_ALIGNED_SIZE) memory_provider.zero(p, rsize); diff --git a/src/override/malloc.cc b/src/override/malloc.cc index 5e3bf1f55..7190f9910 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -80,13 +80,14 @@ extern "C" "Calling realloc on pointer that is not to the start of an allocation"); } #endif + if (size <= SNMALLOC_NAME_MANGLE(malloc_usable_size)(ptr)) + return ptr; + void* p = SNMALLOC_NAME_MANGLE(malloc)(size); - if (p) + if (p != nullptr) { assert(p == Alloc::external_pointer(p)); - size_t sz = - (std::min)(size, SNMALLOC_NAME_MANGLE(malloc_usable_size)(ptr)); - memcpy(p, ptr, sz); + memcpy(p, ptr, size); SNMALLOC_NAME_MANGLE(free)(ptr); } return p; From 2b8b4c884ffa5376101f3843153666db53ce0f94 Mon Sep 17 00:00:00 2001 From: theodus Date: Sat, 2 Feb 2019 17:28:02 -0500 Subject: [PATCH 2/3] Do not realloc if size is in the same sizeclass --- src/override/malloc.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/override/malloc.cc b/src/override/malloc.cc index 7190f9910..3a309220f 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -80,14 +80,17 @@ extern "C" "Calling realloc on pointer that is not to the start of an allocation"); } #endif - if (size <= SNMALLOC_NAME_MANGLE(malloc_usable_size)(ptr)) + size_t sz = SNMALLOC_NAME_MANGLE(malloc_usable_size)(ptr); + // Keep the current allocation if the given size is in the same sizeclass. + if (sz == sizeclass_to_size(size_to_sizeclass(size))) return ptr; void* p = SNMALLOC_NAME_MANGLE(malloc)(size); if (p != nullptr) { assert(p == Alloc::external_pointer(p)); - memcpy(p, ptr, size); + sz = (std::min)(size, sz); + memcpy(p, ptr, sz); SNMALLOC_NAME_MANGLE(free)(ptr); } return p; From ae3f9f6aec7fb3ebe1fbccb87a6ed85316ffe039 Mon Sep 17 00:00:00 2001 From: theodus Date: Sun, 3 Feb 2019 08:19:11 -0500 Subject: [PATCH 3/3] Get alloc size without wrapper --- src/override/malloc.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/override/malloc.cc b/src/override/malloc.cc index 3a309220f..218191e58 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -80,7 +80,7 @@ extern "C" "Calling realloc on pointer that is not to the start of an allocation"); } #endif - size_t sz = SNMALLOC_NAME_MANGLE(malloc_usable_size)(ptr); + size_t sz = Alloc::alloc_size(ptr); // Keep the current allocation if the given size is in the same sizeclass. if (sz == sizeclass_to_size(size_to_sizeclass(size))) return ptr;