lua-users home
lua-l archive

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


I'm looking at this right now myself.

I've found a few minor optimizations in the Lua core itself (based on
5.1work).  These aren't necessarily general-purpose, but they are
applicable for us (running on the PS2):

I changed the definition of Proto to this.  This reduced the size of
Proto from 72 bytes to 56 bytes.  This required a few changes elsewhere,
but they are pretty straightforward.

typedef struct Proto {
  CommonHeader;
  TValue *k;  /* constants used by the function */
  Instruction *code;
  struct Proto **p;  /* functions defined inside the function */
  lu_short *lineinfo;  /* map from opcodes to source lines */  /* CHANGE
*/
  struct LocVar *locvars;  /* information about local variables */
  TString **upvalues;  /* upvalue names */
  TString  *source;
  GCObject *gclist;
  lu_short lineDefined;  /* CHANGE */
  lu_short sizecode;  /* CHANGE */
  lu_short sizelineinfo;  /* CHANGE */
  lu_short sizelocvars;  /* CHANGE */
  lu_short sizek;  /* size of `k' */  /* CHANGE */
  lu_byte sizeupvalues;  /* CHANGE */
  lu_byte sizep;  /* size of `p' */  /* CHANGE */
  lu_byte nups;  /* number of upvalues */
  lu_byte numparams;
  lu_byte is_vararg;
  lu_byte maxstacksize;
} Proto; 

Note that this could be reduced further in release builds by removing
the entries that are stripped when debug info isn't included.

I was also able to reduce the size of Table by 4 bytes:

typedef struct Table {
  CommonHeader;
  lu_byte flags;  /* 1<<p means tagmethod(p) is not present */ 
  lu_byte lsizenode;  /* log2 of size of `node' array */
  struct Table *metatable;
  TValue *array;  /* array part */
  Node *node;
  GCObject *gclist;
  lu_short sizearray;  /* size of `array' array */  /* CHANGE */
  lu_short firstfreeidx;  /* this position is free; all positions after
it are full */  /* CHANGE */
} Table;


This limits arrays to having a maximum size of 2^16, but that's fine for
our purposes.  This reduces the size of Table from 32 bytes to 28 bytes;
not huge but still worthwhile.

Cheers,
Jasmin


-----Original Message-----
From: Nick Trout [mailto:nick@rockstarvancouver.com] 
Sent: Friday, August 27, 2004 11:49 AM
To: Lua list
Subject: Reducing memory consumption


So, this is quite an opened question and I have few ideas but I'd like
to throw it out open... What ideas do people have for reducing the
memory consumption of Lua in working scripts?

Nick