Commit cf2d6e6f authored by Tim Peters's avatar Tim Peters

PyObject_Realloc(): If a small block is shrinking, bite the expense of

copying it if at least 25% of the input block can be reclaimed.
parent df4af78d
...@@ -793,11 +793,22 @@ PyObject_Realloc(void *p, size_t nbytes) ...@@ -793,11 +793,22 @@ PyObject_Realloc(void *p, size_t nbytes)
if (ADDRESS_IN_RANGE(p, pool->arenaindex)) { if (ADDRESS_IN_RANGE(p, pool->arenaindex)) {
/* We're in charge of this block */ /* We're in charge of this block */
size = INDEX2SIZE(pool->szidx); size = INDEX2SIZE(pool->szidx);
if (size >= nbytes) if (nbytes <= size) {
/* Don't bother if a smaller size was requested. */ /* The block is staying the same or shrinking. If
* it's shrinking, there's a tradeoff: it costs
* cycles to copy the block to a smaller size class,
* but it wastes memory not to copy it. The
* compromise here is to copy on shrink only if at
* least 25% of size can be shaved off.
*/
if (4 * nbytes > 3 * size) {
/* It's the same,
* or shrinking and new/old > 3/4.
*/
return p; return p;
/* We need more memory. */ }
assert(nbytes != 0); size = nbytes;
}
bp = PyObject_Malloc(nbytes); bp = PyObject_Malloc(nbytes);
if (bp != NULL) { if (bp != NULL) {
memcpy(bp, p, size); memcpy(bp, p, size);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment