Răsfoiți Sursa

update:开启LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP后将减少debug信息在ram的占用

alienwalker 4 ani în urmă
părinte
comite
a383275770
4 a modificat fișierele cu 208 adăugiri și 18 ștergeri
  1. 39 1
      lua/include/lobject.h
  2. 19 0
      lua/src/lobject.c
  3. 3 0
      lua/src/lstring.c
  4. 147 17
      lua/src/lundump.c

+ 39 - 1
lua/include/lobject.h

@@ -302,6 +302,24 @@ typedef TValue *StkId;  /* index to stack elements */
 */
 typedef struct TString {
   CommonHeader;
+#if defined(__LUATOS_SMALL_RAM__) && defined(__LUATOS_SCRIPT_BASE__)
+  struct
+  {
+	uint16_t extra:5;
+	uint16_t shrlen:6;
+	uint16_t static_flag:5;
+  };
+  unsigned int hash;
+  union {
+	struct
+	{
+	  uint32_t lnglen:17;/* length for long strings */
+	  uint32_t static_offset:15;
+	};
+    struct TString *hnext;  /* linked list for hash table */
+
+  } u;
+#else
   lu_byte extra;  /* reserved words for short strings; "has hash" for longs */
   lu_byte shrlen;  /* length for short strings */
   unsigned int hash;
@@ -309,6 +327,7 @@ typedef struct TString {
     size_t lnglen;  /* length for long strings */
     struct TString *hnext;  /* linked list for hash table */
   } u;
+#endif
 } TString;
 
 
@@ -325,9 +344,12 @@ typedef union UTString {
 ** Get the actual string (array of bytes) from a 'TString'.
 ** (Access to 'extra' ensures that value is really a 'TString'.)
 */
+#if defined(__LUATOS_SMALL_RAM__) && defined(__LUATOS_SCRIPT_BASE__)
+char *getstr(TString *ts);
+#else
 #define getstr(ts)  \
   check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString))
-
+#endif
 
 /* get the actual string (array of bytes) from a Lua value */
 #define svalue(o)       getstr(tsvalue(o))
@@ -396,8 +418,13 @@ typedef struct Upvaldesc {
 */
 typedef struct LocVar {
   TString *varname;
+#ifdef __LUATOS_SMALL_RAM__
+  uint16_t startpc;
+  uint16_t endpc;
+#else
   int startpc;  /* first point where variable is active */
   int endpc;    /* first point where variable is dead */
+#endif
 } LocVar;
 
 
@@ -409,6 +436,16 @@ typedef struct Proto {
   lu_byte numparams;  /* number of fixed parameters */
   lu_byte is_vararg;
   lu_byte maxstacksize;  /* number of registers needed by this function */
+#ifdef __LUATOS_SMALL_RAM__
+  uint16_t sizeupvalues;  /* size of 'upvalues' */
+  uint16_t sizek;  /* size of 'k' */
+  uint16_t sizecode;
+  uint16_t sizelineinfo;
+  uint16_t sizep;  /* size of 'p' */
+  uint16_t sizelocvars;
+  uint16_t linedefined;
+  uint16_t lastlinedefined;
+#else
   int sizeupvalues;  /* size of 'upvalues' */
   int sizek;  /* size of 'k' */
   int sizecode;
@@ -417,6 +454,7 @@ typedef struct Proto {
   int sizelocvars;
   int linedefined;  /* debug information  */
   int lastlinedefined;  /* debug information  */
+#endif
   TValue *k;  /* constants used by the function */
   Instruction *code;  /* opcodes */
   struct Proto **p;  /* functions defined inside the function */

+ 19 - 0
lua/src/lobject.c

@@ -520,3 +520,22 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) {
   }
 }
 
+#if defined(__LUATOS_SMALL_RAM__) && defined(__LUATOS_SCRIPT_BASE__)
+char *getstr(TString *ts) {
+	uint32_t offset;
+	if (ts->static_flag) {
+		if (!ts->shrlen)
+		{
+			offset = (ts->static_flag - 1);
+			offset = (offset << 15) + ts->u.static_offset + __LUATOS_SCRIPT_BASE__;
+		}
+		else
+		{
+			memcpy(&offset, cast(char *, (ts)) + sizeof(UTString), 4);
+		}
+		return (char *)(offset);
+	} else {
+		return cast(char *, (ts)) + sizeof(UTString);
+	}
+}
+#endif

+ 3 - 0
lua/src/lstring.c

@@ -139,6 +139,9 @@ static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
   ts = gco2ts(o);
   ts->hash = h;
   ts->extra = 0;
+#if defined(__LUATOS_SMALL_RAM__) && defined(__LUATOS_SCRIPT_BASE__)
+  ts->static_flag = 0;
+#endif
   getstr(ts)[l] = '\0';  /* ending 0 */
   return ts;
 }

+ 147 - 17
lua/src/lundump.c

@@ -43,13 +43,16 @@
 #define luai_verifycode(L,b,f)  /* empty */
 #endif
 
-
-
+size_t ptr_offset = 0;
+#ifdef LUAT_UNDUMP_DEBUG
 size_t code_size = 0;
+size_t code_max = 0;
+size_t proto_size = 0;
 size_t const_size = 0;
 size_t debug_size = 0;
 size_t str_size = 0;
-size_t ptr_offset = 0;
+size_t max_pc = 0;
+#endif
 
 typedef struct {
   lua_State *L;
@@ -107,12 +110,103 @@ static lua_Integer LoadInteger (LoadState *S) {
   return x;
 }
 
+#ifndef LoadF
+typedef struct LoadF {
+  int n;  /* number of pre-read characters */
+  FILE *f;  /* file being read */
+  char buff[BUFSIZ];  /* area for reading file */
+} LoadF;
+#endif
+
+#if defined(__LUATOS_SMALL_RAM__) && defined(__LUATOS_SCRIPT_BASE__)
+/*
+** creates a new string object
+*/
+static TString *static_createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
+  TString *ts;
+  GCObject *o;
+  o = luaC_newobj(L, tag, sizeof(TString) + l);
+  ts = gco2ts(o);
+  ts->hash = h;
+  ts->extra = 0;
+  return ts;
+}
+
+/*
+** checks whether short string exists and reuses it or creates a new one
+*/
+static TString *static_internshrstr (lua_State *L, const char *str, size_t l) {
+  TString *ts;
+  global_State *g = G(L);
+  unsigned int h = luaS_hash(str, l, g->seed);
+  TString **list = &g->strt.hash[lmod(h, g->strt.size)];
+  lua_assert(str != NULL);  /* otherwise 'memcmp'/'memcpy' are undefined */
+  for (ts = *list; ts != NULL; ts = ts->u.hnext) {
+    if (l == ts->shrlen &&
+        (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
+      /* found! */
+      if (isdead(g, ts))  /* dead (but not collected yet)? */
+        changewhite(ts);  /* resurrect it */
+      return ts;
+    }
+  }
+  if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
+    luaS_resize(L, g->strt.size * 2);
+    list = &g->strt.hash[lmod(h, g->strt.size)];  /* recompute with new size */
+  }
+  ts = static_createstrobj(L, 4, LUA_TSHRSTR, h);
+  ts->static_flag = 1;
+  memcpy(cast(char *, (ts)) + sizeof(UTString), &str, 4);
+  ts->shrlen = cast_byte(l);
+  ts->u.hnext = *list;
+  *list = ts;
+  g->strt.nuse++;
+  return ts;
+}
+#endif
 
 static TString *LoadString (LoadState *S, Proto *p) {
   size_t size = LoadByte(S);
   TString *ts;
   if (size == 0xFF)
     LoadVar(S, size);
+#if defined(__LUATOS_SMALL_RAM__) && defined(__LUATOS_SCRIPT_BASE__)
+  char* ptr = (char*)luat_vfs_mmap(((LoadF*)S->Z->data)->f);
+  uint32_t offset;
+  if (ptr && size) {
+	  offset = (uint32_t)ptr + ptr_offset - __LUATOS_SCRIPT_BASE__;
+	  char temp[128];
+	  uint32_t done_len = size;
+	  do {
+		  if (done_len > sizeof(temp)){
+			  LoadVector(S, temp, sizeof(temp));
+			  done_len -= sizeof(temp);
+		  } else {
+			  LoadVector(S, temp, done_len);
+			  done_len = 0;
+		  }
+	  }while(done_len);
+	  if (--size <= LUAI_MAXSHORTLEN) {  /* short string? */
+		  ts = static_internshrstr(S->L, offset + __LUATOS_SCRIPT_BASE__, size);
+#ifdef LUAT_UNDUMP_DEBUG
+		  str_size+= (4 + sizeof(TString) + (8 - 1)) & (~(8 - 1));
+#endif
+	  }
+	  else {  /* long string */
+		  ts = static_createstrobj(S->L, 0, LUA_TLNGSTR, G(S->L)->seed);
+		  ts->static_flag = (offset >> 15) + 1;
+		  ts->u.static_offset = offset & 0x00007fff;
+		  ts->u.lnglen = size;
+#ifdef LUAT_UNDUMP_DEBUG
+		  str_size+= (sizeof(TString) + (8 - 1)) & (~(8 - 1));
+#endif
+	  }
+
+  } else {
+	return NULL;
+  }
+  size = 0;
+#else
   if (size == 0)
     return NULL;
   else if (--size <= LUAI_MAXSHORTLEN) {  /* short string? */
@@ -124,25 +218,27 @@ static TString *LoadString (LoadState *S, Proto *p) {
     ts = luaS_createlngstrobj(S->L, size);
     LoadVector(S, getstr(ts), size);  /* load directly in final place */
   }
+#ifdef LUAT_UNDUMP_DEBUG
+  str_size+= (size + sizeof(TString) + (8 - 1)) & (~(8 - 1));
+#endif
+#endif
   luaC_objbarrier(S->L, p, ts);
-  str_size+= size + sizeof(TString);
+
+
   return ts;
 }
 
-#ifndef LoadF
-typedef struct LoadF {
-  int n;  /* number of pre-read characters */
-  FILE *f;  /* file being read */
-  char buff[BUFSIZ];  /* area for reading file */
-} LoadF;
-#endif
+
 
 static void LoadCode (LoadState *S, Proto *f) {
   int n = LoadInt(S);
   f->sizecode = n;
+#ifdef LUAT_UNDUMP_DEBUG
+  code_max += n * sizeof(Instruction);
+#endif
 #ifdef LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
   #if LUAT_UNDUMP_DEBUG
-  LLOGD("try mmap %p %p %p", S, S->Z, S->Z->data);
+//  LLOGD("try mmap %p %p %p", S, S->Z, S->Z->data);
   #endif
   char* ptr = (char*)luat_vfs_mmap(((LoadF*)S->Z->data)->f);
   Instruction inst[1];
@@ -161,10 +257,11 @@ static void LoadCode (LoadState *S, Proto *f) {
   // 调试时务必打开
 #if LUAT_UNDUMP_DEBUG
   LLOGD("code in ram");
+  code_size += ((n * sizeof(Instruction)) + (8 - 1)) & (~(8 - 1));
 #endif
   f->code = luaM_newvector(S->L, n, Instruction);
   LoadVector(S, f->code, n);
-  code_size += n * sizeof(Instruction);
+
 }
 
 
@@ -202,7 +299,9 @@ static void LoadConstants (LoadState *S, Proto *f) {
       lua_assert(0);
     }
   }
-  const_size += n * sizeof(TValue);
+#ifdef LUAT_UNDUMP_DEBUG
+  const_size += (n * sizeof(TValue) + (8 - 1)) & (~(8 - 1));
+#endif
 }
 
 
@@ -218,6 +317,9 @@ static void LoadProtos (LoadState *S, Proto *f) {
     luaC_objbarrier(S->L, f, f->p[i]);
     LoadFunction(S, f->p[i], f->source);
   }
+#ifdef LUAT_UNDUMP_DEBUG
+  proto_size += (sizeof(Proto) + (8 - 1)) & (~(8 - 1));
+#endif
 }
 
 
@@ -238,9 +340,24 @@ static void LoadUpvalues (LoadState *S, Proto *f) {
 static void LoadDebug (LoadState *S, Proto *f) {
   int i, n;
   n = LoadInt(S);
+#ifdef LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
+  char* ptr = (char*)luat_vfs_mmap(((LoadF*)S->Z->data)->f);
+  int inst[1];
+  if (ptr) {
+	f->lineinfo = ptr + ptr_offset;
+    for (size_t i = 0; i < n; i++)
+    {
+      LoadVector(S, &inst, 1);
+    }
+  }
+#else
   f->lineinfo = luaM_newvector(S->L, n, int);
   f->sizelineinfo = n;
   LoadVector(S, f->lineinfo, n);
+#ifdef LUAT_UNDUMP_DEBUG
+  debug_size += (f->sizelineinfo * sizeof(int) + (8 - 1)) & (~(8 - 1));
+#endif
+#endif
   n = LoadInt(S);
   f->locvars = luaM_newvector(S->L, n, LocVar);
   f->sizelocvars = n;
@@ -250,13 +367,24 @@ static void LoadDebug (LoadState *S, Proto *f) {
     f->locvars[i].varname = LoadString(S, f);
     f->locvars[i].startpc = LoadInt(S);
     f->locvars[i].endpc = LoadInt(S);
+#ifdef LUAT_UNDUMP_DEBUG
+    if (f->locvars[i].startpc > max_pc)
+    {
+    	max_pc = f->locvars[i].startpc;
+    }
+    if (f->locvars[i].endpc > max_pc)
+    {
+    	max_pc = f->locvars[i].endpc;
+    }
+#endif
   }
   n = LoadInt(S);
   for (i = 0; i < n; i++)
     f->upvalues[i].name = LoadString(S, f);
 
-  debug_size += f->sizelineinfo * sizeof(int);
-  debug_size += f->sizelocvars * sizeof(LocVar);
+#ifdef LUAT_UNDUMP_DEBUG
+  debug_size += f->sizelocvars * (sizeof(LocVar) + (8 - 1)) & (~(8 - 1));
+#endif
 }
 
 
@@ -324,7 +452,6 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
   // 复位偏移量数据
   ptr_offset = 0;
   ptr_offset ++; // 之前的方法已经读取了一个字节
-
   if (*name == '@' || *name == '=')
     S.name = name + 1;
   else if (*name == LUA_SIGNATURE[0])
@@ -349,7 +476,10 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
   LLOGD("debug_size %d", debug_size);
   LLOGD("const_size now %d", const_size);
   LLOGD("code_size now %d", code_size);
+  LLOGD("code max now %d", code_max);
   LLOGD("ptr_offset %d", ptr_offset);
+  LLOGD("proto size now %d", proto_size);
+  LLOGD("max pc %d", max_pc);
   luat_os_print_heapinfo("func");
 #endif