Jeroen Demeyer on Tue, 04 Apr 2017 13:11:05 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: When enlarging stack in new_chunk_resize(), add 1/8 to stack size


On 2017-04-03 21:36, Bill Allombert wrote:
Could you align the stack size on page boundary ? This would avoid
waisting part of the last page.

Done, see new patch.
>From c5d3f0368eb5e9a35c9f94667abb755f856b1c35 Mon Sep 17 00:00:00 2001
From: Jeroen Demeyer <jdemeyer@cage.ugent.be>
Date: Mon, 3 Apr 2017 14:06:22 +0200
Subject: [PATCH 1/2] When enlarging stack in new_chunk_resize(), add 1/8 to
 stack size

---
 src/language/init.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/language/init.c b/src/language/init.c
index 2a30482..34cce31 100644
--- a/src/language/init.c
+++ b/src/language/init.c
@@ -641,17 +641,17 @@ pari_mainstack_mextend(pari_sp from, pari_sp to)
   return mprotect((void*)from, s, PROT_READ|PROT_WRITE);
 }
 
-/* Set actual stack size to the given size. This sets st->size and
- * st->bot. If not enough system memory is available, this can fail.
- * Return 1 if successful, 0 if failed (in that case, st->size is not
- * changed) */
+/* Set actual stack size to the given size, aligned to the page size.
+ * This sets st->size and st->bot. If not enough system memory is
+ * available, this can fail. Return 1 if successful, 0 if failed
+ * (in that case, st->size is not changed) */
 static int
 pari_mainstack_setsize(struct pari_mainstack *st, size_t size)
 {
   pari_sp newbot = st->top - size;
   /* Align newbot to pagesize */
-  pari_sp alignbot = newbot & ~(pari_sp)(PARI_STACK_ALIGN - 1);
-  if (pari_mainstack_mextend(alignbot, st->top))
+  newbot &= ~(pari_sp)(PARI_STACK_ALIGN - 1);
+  if (pari_mainstack_mextend(newbot, st->top))
   {
     /* Making the memory available did not work: limit vsize to the
      * current actual stack size. */
@@ -659,9 +659,9 @@ pari_mainstack_setsize(struct pari_mainstack *st, size_t size)
     pari_warn(warnstack, st->vsize);
     return 0;
   }
-  pari_mainstack_mreset(st->vbot, alignbot);
+  pari_mainstack_mreset(st->vbot, newbot);
   st->bot = newbot;
-  st->size = size;
+  st->size = st->top - st->bot;
   return 1;
 }
 
@@ -809,7 +809,7 @@ parivstack_reset(void)
 }
 
 /* Enlarge the stack if needed such that the unused portion of the stack
- * (between bot and avma) is large enough to contain x longs. */
+ * (between bot and avma) is large enough to contain at least x longs. */
 void
 new_chunk_resize(size_t x)
 {
@@ -817,11 +817,12 @@ new_chunk_resize(size_t x)
   avail = (avma - pari_mainstack->bot) / sizeof(long);
   if (avail >= x) return;
 
-  /* We need to enlarge the stack. We try to at least double the
-   * stack, to avoid increasing the stack a lot of times by a small
-   * amount. */
+  /* We need to enlarge the stack. We compute the minimum size needed
+   * and add 1/8 to avoid increasing the stack a lot of times by a
+   * small amount. */
   size = pari_mainstack->size;
-  newsize = size + maxuu((x - avail) * sizeof(long), size);
+  newsize = size + (x - avail) * sizeof(long);
+  newsize += newsize / 8;
   paristack_resize(newsize);
 
   /* Verify that we have enough space. Using a division here instead
-- 
2.7.3