From 20299f87cb7a155c64e3fcbe747bcbf5591eaa23 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Thu, 8 Apr 2021 02:21:27 +0300 Subject: [PATCH] mdbx: minor refine `get_reasonable_db_maxsize()`. Change-Id: I5d654940a325c0c3966a59a37615820587c52c78 --- src/core.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/core.c b/src/core.c index b8e64c6e..8b79837e 100644 --- a/src/core.c +++ b/src/core.c @@ -10532,20 +10532,15 @@ __cold static intptr_t get_reasonable_db_maxsize(intptr_t *cached_result) { *cached_result = (intptr_t)((size_t)total_ram_pages * 207 >> 7) * pagesize; /* Round to the nearest human-readable granulation. */ - for (int i = 10; i < MDBX_WORDBITS - 1; i += 10) { - const size_t unit = (size_t)1 << i; + for (size_t unit = MEGABYTE; unit; unit <<= 5) { const size_t floor = floor_powerof2(*cached_result, unit); const size_t ceil = ceil_powerof2(*cached_result, unit); - if (*cached_result - floor < ceil - *cached_result || - ceil > MAX_MAPSIZE) { - if (*cached_result - floor > (size_t)*cached_result / 16) - break; - *cached_result = floor; - } else { - if (ceil - *cached_result > (size_t)*cached_result / 16) - break; - *cached_result = ceil; - } + const size_t threshold = (size_t)*cached_result >> 4; + const bool down = + *cached_result - floor < ceil - *cached_result || ceil > MAX_MAPSIZE; + if (threshold < (down ? *cached_result - floor : ceil - *cached_result)) + break; + *cached_result = down ? floor : ceil; } } return *cached_result;