lua-users home
lua-l archive

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

2014-03-20 20:51 GMT+08:00 Roberto Ierusalimschy <>:
>> There are many lua vm (more then 10K) in one process in our system. We
>> run all of them by a thread pool. It runs well except huge memory
>> usage , because the same byte codes exist in each vm .
>> So I make a patch to lua 5.2.3 to share the same Proto objects among
>> multiple lua vm [1].
> Are you really sharing the Proto object or only the bytecode (and maybe
> some other fields)? If you are sharing the Proto object, how to you
> handle its constants (field 'k')? They include strings, and short strings
> cannot be shared (at least directly), due to internalization.

I defined a new struct SharedProto :

typedef struct SharedProto {
  void *l_G;  /* global state belongs to */
  Instruction *code;
  int *lineinfo;  /* map from opcodes to source lines (debug information) */
  LocVar *locvars;  /* information about local variables (debug information) */
  Upvaldesc *upvalues;  /* upvalue information */
  TString  *source;  /* used for debug information */
  int sizeupvalues;  /* size of 'upvalues' */
  int sizek;  /* size of `k' */
  int sizecode;
  int sizelineinfo;
  int sizep;  /* size of `p' */
  int sizelocvars;
  int linedefined;
  int lastlinedefined;
  lu_byte numparams;  /* number of fixed parameters */
  lu_byte is_vararg;
  lu_byte maxstacksize;  /* maximum stack used by this function */
} SharedProto;

** Function Prototypes
typedef struct Proto {
  TValue *k;  /* constants used by the function */
  struct SharedProto *sp;
  struct Proto **p;  /* functions defined inside the function */
  union Closure *cache;  /* last created closure with this prototype */
  GCObject *gclist;
} Proto;

The data in the struct SharedProto can be share, and others (constants
/ cache, etc)  are not.