diff -r 3e61d8e06ef7 Objects/obmalloc.c --- a/Objects/obmalloc.c Sun Feb 02 23:37:29 2014 +0100 +++ b/Objects/obmalloc.c Sun Feb 02 17:23:38 2014 -0800 @@ -2,6 +2,10 @@ #ifdef WITH_PYMALLOC +#ifdef HAVE_MALLOPT_MMAP_THRESHOLD + #include +#endif + #ifdef WITH_VALGRIND #include @@ -75,7 +79,8 @@ * Allocation strategy abstract: * * For small requests, the allocator sub-allocates blocks of memory. - * Requests greater than 256 bytes are routed to the system's allocator. + * Requests greater than SMALL_REQUEST_THRESHOLD bytes are routed to the + * system's allocator. * * Small requests are grouped in size classes spaced 8 bytes apart, due * to the required valid alignment of the returned address. Requests of @@ -107,10 +112,11 @@ * 57-64 64 7 * 65-72 72 8 * ... ... ... - * 241-248 248 30 - * 249-256 256 31 + * 497-504 504 62 + * 505-512 512 63 * - * 0, 257 and up: routed to the underlying allocator. + * 0, SMALL_REQUEST_THRESHOLD + 1 and up: routed to the underlying + * allocator. */ /*==========================================================================*/ @@ -143,10 +149,13 @@ * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256 * 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT * + * Note: a size threshold of 512 guarantees that newly created dictionaries + * will be allocated from preallocated memory pools on 64-bit. + * * Although not required, for better performance and space efficiency, * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2. */ -#define SMALL_REQUEST_THRESHOLD 256 +#define SMALL_REQUEST_THRESHOLD 512 #define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT) /* @@ -440,6 +449,9 @@ , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55) #if NB_SMALL_SIZE_CLASSES > 56 , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63) +#if NB_SMALL_SIZE_CLASSES > 64 +#error "NB_SMALL_SIZE_CLASSES should be less than 64" +#endif /* NB_SMALL_SIZE_CLASSES > 64 */ #endif /* NB_SMALL_SIZE_CLASSES > 56 */ #endif /* NB_SMALL_SIZE_CLASSES > 48 */ #endif /* NB_SMALL_SIZE_CLASSES > 40 */ @@ -545,6 +557,11 @@ if (numarenas > PY_SIZE_MAX / sizeof(*arenas)) return NULL; /* overflow */ #endif +#ifdef HAVE_MALLOPT_MMAP_THRESHOLD + /* Ensure arenas are allocated by mmap to avoid heap fragmentation. */ + if (numarenas == INITIAL_ARENA_OBJECTS) + mallopt(M_MMAP_THRESHOLD, ARENA_SIZE); +#endif nbytes = numarenas * sizeof(*arenas); arenaobj = (struct arena_object *)realloc(arenas, nbytes); if (arenaobj == NULL) diff -r 3e61d8e06ef7 configure --- a/configure Sun Feb 02 23:37:29 2014 +0100 +++ b/configure Sun Feb 02 17:23:38 2014 -0800 @@ -10571,6 +10571,34 @@ fi rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether mallopt can set malloc mmap threshold" >&5 +$as_echo_n "checking whether mallopt can set malloc mmap threshold... " >&6; } +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +#include + +int +main () +{ +mallopt(M_MMAP_THRESHOLD, 256 * 1024) + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + +$as_echo "#define HAVE_MALLOPT_MMAP_THRESHOLD 1" >>confdefs.h + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 +$as_echo "yes" >&6; } +else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 +$as_echo "no" >&6; } + +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + for ac_prog in true do # Extract the first word of "$ac_prog", so it can be a program name with args. diff -r 3e61d8e06ef7 configure.ac --- a/configure.ac Sun Feb 02 23:37:29 2014 +0100 +++ b/configure.ac Sun Feb 02 17:23:38 2014 -0800 @@ -3022,6 +3022,15 @@ AC_MSG_RESULT(yes) ]) +AC_MSG_CHECKING(whether mallopt can set malloc mmap threshold) +AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ +#include +]], [[mallopt(M_MMAP_THRESHOLD, 256 * 1024)]])], + [AC_DEFINE(HAVE_MALLOPT_MMAP_THRESHOLD, 1, Define if mallopt can set malloc mmap threshold.) + AC_MSG_RESULT(yes)], + [AC_MSG_RESULT(no) +]) + dnl check for true AC_CHECK_PROGS(TRUE, true, /bin/true) diff -r 3e61d8e06ef7 pyconfig.h.in --- a/pyconfig.h.in Sun Feb 02 23:37:29 2014 +0100 +++ b/pyconfig.h.in Sun Feb 02 17:23:38 2014 -0800 @@ -460,6 +460,9 @@ /* Define this if you have the makedev macro. */ #undef HAVE_MAKEDEV +/* Define this if mallopt can set malloc mmap threshold. */ +#undef HAVE_MALLOPT_MMAP_THRESHOLD + /* Define to 1 if you have the `memmove' function. */ #undef HAVE_MEMMOVE