925e6b
Make it more likely for the system allocator to release free()d memory arenas on glibc-based systems. 
925e6b
Patch by Charles-François Natali.
925e6b
https://bugs.python.org/issue20494
925e6b
925e6b
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
925e6b
--- a/Objects/obmalloc.c
925e6b
+++ b/Objects/obmalloc.c
925e6b
@@ -2,6 +2,13 @@
925e6b
 
925e6b
 #ifdef WITH_PYMALLOC
925e6b
 
925e6b
+#ifdef HAVE_MMAP
925e6b
+ #include <sys/mman.h>
925e6b
+ #ifdef MAP_ANONYMOUS
925e6b
+  #define ARENAS_USE_MMAP
925e6b
+ #endif
925e6b
+#endif
925e6b
+
925e6b
 #ifdef WITH_VALGRIND
925e6b
 #include <valgrind/valgrind.h>
925e6b
 
925e6b
@@ -75,7 +82,8 @@ static int running_on_valgrind = -1;
925e6b
  * Allocation strategy abstract:
925e6b
  *
925e6b
  * For small requests, the allocator sub-allocates <Big> blocks of memory.
925e6b
- * Requests greater than 256 bytes are routed to the system's allocator.
925e6b
+ * Requests greater than SMALL_REQUEST_THRESHOLD bytes are routed to the
925e6b
+ * system's allocator. 
925e6b
  *
925e6b
  * Small requests are grouped in size classes spaced 8 bytes apart, due
925e6b
  * to the required valid alignment of the returned address. Requests of
925e6b
@@ -107,10 +115,11 @@ static int running_on_valgrind = -1;
925e6b
  *       57-64                   64                       7
925e6b
  *       65-72                   72                       8
925e6b
  *        ...                   ...                     ...
925e6b
- *      241-248                 248                      30
925e6b
- *      249-256                 256                      31
925e6b
+ *      497-504                 504                      62
925e6b
+ *      505-512                 512                      63 
925e6b
  *
925e6b
- *      0, 257 and up: routed to the underlying allocator.
925e6b
+ *      0, SMALL_REQUEST_THRESHOLD + 1 and up: routed to the underlying
925e6b
+ *      allocator.
925e6b
  */
925e6b
 
925e6b
 /*==========================================================================*/
925e6b
@@ -143,10 +152,13 @@ static int running_on_valgrind = -1;
925e6b
  *      1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256
925e6b
  *      2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT
925e6b
  *
925e6b
+ * Note: a size threshold of 512 guarantees that newly created dictionaries
925e6b
+ * will be allocated from preallocated memory pools on 64-bit.
925e6b
+ *
925e6b
  * Although not required, for better performance and space efficiency,
925e6b
  * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2.
925e6b
  */
925e6b
-#define SMALL_REQUEST_THRESHOLD 256
925e6b
+#define SMALL_REQUEST_THRESHOLD 512 
925e6b
 #define NB_SMALL_SIZE_CLASSES   (SMALL_REQUEST_THRESHOLD / ALIGNMENT)
925e6b
 
925e6b
 /*
925e6b
@@ -174,15 +186,15 @@ static int running_on_valgrind = -1;
925e6b
 /*
925e6b
  * The allocator sub-allocates <Big> blocks of memory (called arenas) aligned
925e6b
  * on a page boundary. This is a reserved virtual address space for the
925e6b
- * current process (obtained through a malloc call). In no way this means
925e6b
- * that the memory arenas will be used entirely. A malloc(<Big>) is usually
925e6b
- * an address range reservation for <Big> bytes, unless all pages within this
925e6b
- * space are referenced subsequently. So malloc'ing big blocks and not using
925e6b
- * them does not mean "wasting memory". It's an addressable range wastage...
925e6b
+ * current process (obtained through a malloc()/mmap() call). In no way this
925e6b
+ * means that the memory arenas will be used entirely. A malloc(<Big>) is
925e6b
+ * usually an address range reservation for <Big> bytes, unless all pages within
925e6b
+ * this space are referenced subsequently. So malloc'ing big blocks and not
925e6b
+ * using them does not mean "wasting memory". It's an addressable range
925e6b
+ * wastage... 
925e6b
  *
925e6b
- * Therefore, allocating arenas with malloc is not optimal, because there is
925e6b
- * some address space wastage, but this is the most portable way to request
925e6b
- * memory from the system across various platforms.
925e6b
+ * Arenas are allocated with mmap() on systems supporting anonymous memory
925e6b
+ * mappings to reduce heap fragmentation.
925e6b
  */
925e6b
 #define ARENA_SIZE              (256 << 10)     /* 256KB */
925e6b
 
925e6b
@@ -440,6 +452,9 @@ static poolp usedpools[2 * ((NB_SMALL_SI
925e6b
     , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55)
925e6b
 #if NB_SMALL_SIZE_CLASSES > 56
925e6b
     , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63)
925e6b
+#if NB_SMALL_SIZE_CLASSES > 64
925e6b
+#error "NB_SMALL_SIZE_CLASSES should be less than 64"
925e6b
+#endif /* NB_SMALL_SIZE_CLASSES > 64 */
925e6b
 #endif /* NB_SMALL_SIZE_CLASSES > 56 */
925e6b
 #endif /* NB_SMALL_SIZE_CLASSES > 48 */
925e6b
 #endif /* NB_SMALL_SIZE_CLASSES > 40 */
925e6b
@@ -577,7 +592,12 @@ new_arena(void)
925e6b
     arenaobj = unused_arena_objects;
925e6b
     unused_arena_objects = arenaobj->nextarena;
925e6b
     assert(arenaobj->address == 0);
925e6b
+#ifdef ARENAS_USE_MMAP
925e6b
+    arenaobj->address = (uptr)mmap(NULL, ARENA_SIZE, PROT_READ|PROT_WRITE,
925e6b
+                                   MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
925e6b
+#else
925e6b
     arenaobj->address = (uptr)malloc(ARENA_SIZE);
925e6b
+#endif    
925e6b
     if (arenaobj->address == 0) {
925e6b
         /* The allocation failed: return NULL after putting the
925e6b
          * arenaobj back.
925e6b
@@ -1054,7 +1074,11 @@ PyObject_Free(void *p)
925e6b
                 unused_arena_objects = ao;
925e6b
 
925e6b
                 /* Free the entire arena. */
925e6b
+#ifdef ARENAS_USE_MMAP
925e6b
+                munmap((void *)ao->address, ARENA_SIZE);
925e6b
+#else
925e6b
                 free((void *)ao->address);
925e6b
+#endif
925e6b
                 ao->address = 0;                        /* mark unassociated */
925e6b
                 --narenas_currently_allocated;
925e6b
 
925e6b
diff --git a/configure b/configure
925e6b
--- a/configure
925e6b
+++ b/configure
925e6b
@@ -10164,7 +10164,7 @@ for ac_func in alarm setitimer getitimer
925e6b
  clock confstr ctermid execv fchmod fchown fork fpathconf ftime ftruncate \
925e6b
  gai_strerror getgroups getlogin getloadavg getpeername getpgid getpid \
925e6b
  getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
925e6b
- initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime \
925e6b
+ initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime mmap \
925e6b
  mremap nice pathconf pause plock poll pthread_init \
925e6b
  putenv readlink realpath \
925e6b
  select sem_open sem_timedwait sem_getvalue sem_unlink setegid seteuid \
925e6b
diff --git a/configure.ac b/configure.ac
925e6b
--- a/configure.ac
925e6b
+++ b/configure.ac
925e6b
@@ -2905,7 +2905,7 @@ AC_CHECK_FUNCS(alarm setitimer getitimer
925e6b
  clock confstr ctermid execv fchmod fchown fork fpathconf ftime ftruncate \
925e6b
  gai_strerror getgroups getlogin getloadavg getpeername getpgid getpid \
925e6b
  getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
925e6b
- initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime \
925e6b
+ initgroups kill killpg lchmod lchown lstat mkfifo mknod mktime mmap \
925e6b
  mremap nice pathconf pause plock poll pthread_init \
925e6b
  putenv readlink realpath \
925e6b
  select sem_open sem_timedwait sem_getvalue sem_unlink setegid seteuid \
925e6b
diff --git a/pyconfig.h.in b/pyconfig.h.in
925e6b
--- a/pyconfig.h.in
925e6b
+++ b/pyconfig.h.in
925e6b
@@ -475,6 +475,9 @@
925e6b
 /* Define to 1 if you have the `mktime' function. */
925e6b
 #undef HAVE_MKTIME
925e6b
 
925e6b
+/* Define to 1 if you have the `mmap' function. */
925e6b
+#undef HAVE_MMAP
925e6b
+
925e6b
 /* Define to 1 if you have the `mremap' function. */
925e6b
 #undef HAVE_MREMAP
925e6b