Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use C23's free_sized when available #6598

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/include/lean/lean.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,16 @@ static inline unsigned lean_small_object_size(lean_object * o) {
void free(void *); // avoid including big `stdlib.h`
#endif

#if !defined(__STDC_VERSION_STDLIB_H__) || __STDC_VERSION_STDLIB_H__ < 202311L
void free_sized(void* ptr, size_t);
#endif

static inline void lean_free_small_object(lean_object * o) {
#ifdef LEAN_SMALL_ALLOCATOR
lean_free_small(o);
#else
free((size_t*)o - 1);
size_t* ptr = (size_t*)o - 1;
free_sized(ptr, *ptr + sizeof(size_t));
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/library/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ extern "C" LEAN_EXPORT object * lean_read_module_data(object * fname, object *)
#endif
buffer = static_cast<char *>(malloc(size - sizeof(olean_header)));
free_data = [=]() {
free(buffer);
free_sized(buffer, size - sizeof(olean_header));
};
in.read(buffer, size - sizeof(olean_header));
if (!in) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ void dealloc(void * o, size_t sz) {
LEAN_RUNTIME_STAT_CODE(g_num_dealloc++);
sz = lean_align(sz, LEAN_OBJECT_SIZE_DELTA);
if (LEAN_UNLIKELY(sz > LEAN_MAX_SMALL_OBJECT_SIZE)) {
return free(o);
return free_sized(o, sz);
}
dealloc_small_core(o);
}
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class buffer {

void free_memory() {
if (m_buffer != reinterpret_cast<T*>(m_initial_buffer))
delete[] reinterpret_cast<char*>(m_buffer);
#if __cpp_sized_deallocation >= 201309L
operator delete[](reinterpret_cast<char*>(m_buffer), sizeof(T) * m_capacity);
#else
delete[] reinterpret_cast<char*>(m_buffer);
#endif
}

void set_capacity(size_t new_capacity) {
Expand Down
8 changes: 7 additions & 1 deletion src/runtime/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Author: Leonardo de Moura
#define isinf(x) std::isinf(x)
#endif

#if !defined(__STDC_VERSION_STDLIB_H__) || __STDC_VERSION_STDLIB_H__ < 202311L
extern "C" __attribute__((weak)) void free_sized(void *ptr, size_t) {
free(ptr);
}
#endif

// see `Task.Priority.max`
#define LEAN_MAX_PRIO 8

Expand Down Expand Up @@ -198,7 +204,7 @@ static inline void lean_dealloc(lean_object * o, size_t sz) {
#ifdef LEAN_SMALL_ALLOCATOR
dealloc(o, sz);
#else
free(o);
free_sized(o, sz);
#endif
}

Expand Down
Loading