Browse Source

change: 移除HARDSTACKTESTS,移除resizebox里面的主动gc,在luaM_realloc_添加主动gc操作

修改的原因是, realloc的缩放内存已经得到保证

修改的内容:
1. HARDSTACKTESTS 导致运行速度太慢
2. resizebox不再需要主动GC
3. luaM_realloc_根据内存状态主动GC,补充resizebox的修改
Wendal Chen 5 years ago
parent
commit
dc05a030e9
3 changed files with 15 additions and 20 deletions
  1. 1 1
      lua/include/llimits.h
  2. 1 14
      lua/src/lauxlib.c
  3. 13 5
      lua/src/lmem.c

+ 1 - 1
lua/include/llimits.h

@@ -305,7 +305,7 @@ typedef unsigned long Instruction;
 /*
 ** macro to control inclusion of some hard tests on stack reallocation
 */
-#define HARDSTACKTESTS
+//#define HARDSTACKTESTS
 #if !defined(HARDSTACKTESTS)
 #define condmovestack(L,pre,pos)	((void)0)
 #else

+ 1 - 14
lua/src/lauxlib.c

@@ -44,9 +44,6 @@
 #define feof    luat_fs_feof
 #define getc    luat_fs_getc
 
-#define GC_WHAT_RESIZE_BOX 1
-extern int gc_running;
-
 /*
 ** {======================================================
 ** Traceback
@@ -483,17 +480,6 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) {
   lua_Alloc allocf = lua_getallocf(L, &ud);
   UBox *box = (UBox *)lua_touserdata(L, idx);
   void *temp = allocf(ud, box->box, box->bsize, newsize);
-  //-----------------
-  // if GC is NOT running, call Full GC
-  #if GC_WHAT_RESIZE_BOX
-  if (temp == NULL && newsize > 0) {
-    if (gc_running == 0) {
-      luaC_fullgc(L, 0);
-      temp = allocf(ud, box->box, box->bsize, newsize);
-    }
-  }
-  #endif
-  //-----------------
   if (temp == NULL && newsize > 0) {  /* allocation error? */
     resizebox(L, idx, 0);  /* free buffer */
     luaL_error(L, "not enough memory for buffer allocation");
@@ -536,6 +522,7 @@ static void *newbox (lua_State *L, size_t newsize) {
 LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
   lua_State *L = B->L;
   if (B->size - B->n < sz) {  /* not enough space? */
+    luaC_fullgc(L, 0); // add by wendal. GC before luaL_prepbuffsize
     char *newbuff;
     size_t newsize = B->size * 2;  /* double buffer size */
     if (newsize - B->n < sz)  /* not big enough? */

+ 13 - 5
lua/src/lmem.c

@@ -70,7 +70,7 @@ l_noret luaM_toobig (lua_State *L) {
 }
 
 
-
+#include "bget.h"
 /*
 ** generic allocation routine.
 */
@@ -79,10 +79,18 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
   global_State *g = G(L);
   size_t realosize = (block) ? osize : 0;
   lua_assert((realosize == 0) == (block == NULL));
-#if defined(HARDMEMTESTS)
-  if (nsize > realosize && g->gcrunning)
-    luaC_fullgc(L, 1);  /* force a GC whenever possible */
-#endif
+//#if defined(HARDMEMTESTS)
+  if (nsize > realosize && g->gcrunning) {
+    bufsize curalloc;
+    bufsize totfree;
+    bufsize maxfree;
+    unsigned long  nget;
+    unsigned long nrel;
+    bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
+    if (totfree < nsize)
+      luaC_fullgc(L, 1);  /* force a GC whenever possible */
+  }
+//#endif
   newblock = (*g->frealloc)(g->ud, block, osize, nsize);
   if (newblock == NULL && nsize > 0) {
     lua_assert(nsize > realosize);  /* cannot fail when shrinking a block */