Skip to content

Commit

Permalink
Introduce constant function for further compiler optimizations
Browse files Browse the repository at this point in the history
GNU extension __attribute__((const)) helps the compiler to know semantic
meaning of a function call, so that it allows higher optimization than
to normal functions. For example, Common Sub-expression Elimination
(CSE) is used to avoid duplicating the same code inside a function, and
function "_mi_os_page_size" can benefit from CSE optimization once it is
declared as a constant function.

Reference:
  https://lwn.net/Articles/285332/
  • Loading branch information
jserv committed Apr 22, 2021
1 parent b19da8e commit 83d83c0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions include/mimalloc-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ terms of the MIT license. A copy of the license can be found in the file
#define mi_decl_noinline __declspec(noinline)
#define mi_decl_thread __declspec(thread)
#define mi_decl_cache_align __declspec(align(MI_CACHE_LINE))
#define mi_decl_const
#elif (defined(__GNUC__) && (__GNUC__>=3)) // includes clang and icc
#define mi_decl_noinline __attribute__((noinline))
#define mi_decl_thread __thread
#define mi_decl_cache_align __attribute__((aligned(MI_CACHE_LINE)))
#define mi_decl_const __attribute__((const))
#else
#define mi_decl_noinline
#define mi_decl_thread __thread // hope for the best :-)
#define mi_decl_cache_align
#define mi_decl_const
#endif

// "options.c"
Expand All @@ -56,7 +59,7 @@ bool _mi_is_main_thread(void);
bool _mi_preloading(); // true while the C runtime is not ready

// os.c
size_t _mi_os_page_size(void);
size_t _mi_os_page_size(void) mi_decl_const;
void _mi_os_init(void); // called from process init
void* _mi_os_alloc(size_t size, mi_stats_t* stats); // to allocate thread local data
void _mi_os_free(void* p, size_t size, mi_stats_t* stats); // to free thread local data
Expand Down Expand Up @@ -193,12 +196,12 @@ bool _mi_page_is_valid(mi_page_t* page);


// Is `x` a power of two? (0 is considered a power of two)
static inline bool _mi_is_power_of_two(uintptr_t x) {
mi_decl_const static inline bool _mi_is_power_of_two(uintptr_t x) {
return ((x & (x - 1)) == 0);
}

// Align upwards
static inline uintptr_t _mi_align_up(uintptr_t sz, size_t alignment) {
mi_decl_const static inline uintptr_t _mi_align_up(uintptr_t sz, size_t alignment) {
mi_assert_internal(alignment != 0);
uintptr_t mask = alignment - 1;
if ((alignment & mask) == 0) { // power of two?
Expand All @@ -210,7 +213,7 @@ static inline uintptr_t _mi_align_up(uintptr_t sz, size_t alignment) {
}

// Divide upwards: `s <= _mi_divide_up(s,d)*d < s+d`.
static inline uintptr_t _mi_divide_up(uintptr_t size, size_t divider) {
mi_decl_const static inline uintptr_t _mi_divide_up(uintptr_t size, size_t divider) {
mi_assert_internal(divider != 0);
return (divider == 0 ? size : ((size + divider - 1) / divider));
}
Expand All @@ -225,13 +228,13 @@ static inline bool mi_mem_is_zero(void* p, size_t size) {

// Align a byte size to a size in _machine words_,
// i.e. byte size == `wsize*sizeof(void*)`.
static inline size_t _mi_wsize_from_size(size_t size) {
mi_decl_const static inline size_t _mi_wsize_from_size(size_t size) {
mi_assert_internal(size <= SIZE_MAX - sizeof(uintptr_t));
return (size + sizeof(uintptr_t) - 1) / sizeof(uintptr_t);
}

// Does malloc satisfy the alignment constraints already?
static inline bool mi_malloc_satisfies_alignment(size_t alignment, size_t size) {
mi_decl_const static inline bool mi_malloc_satisfies_alignment(size_t alignment, size_t size) {
return (alignment == sizeof(void*) || (alignment == MI_MAX_ALIGN_SIZE && size > (MI_MAX_ALIGN_SIZE/2)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static size_t os_alloc_granularity = 4096;
static size_t large_os_page_size = 0;

// OS (small) page size
size_t _mi_os_page_size() {
mi_decl_const size_t _mi_os_page_size() {
return os_page_size;
}

Expand Down

0 comments on commit 83d83c0

Please sign in to comment.