lua-users home
lua-l archive

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


Chris Chapman wrote:
> Just a quick (and vague) question: what are the expected sizes of compiled
> lua scripts? I had expected quite a good ratio of plain text script size to
> compiled script size.

The main goal in precompilation is just that: to avoid compilation. In other
words, the main goal is fast loading, not compression.

Adam D. Moss wrote:
>While these strings are uniquified per-chunk, that won't help much when
>you have many small scripts.

Actually, the strings are unique only within a function. :-(

>Still, your bytecode will compress well, if you use a compressed
>on-disc format.

Yes, and you can load compressed files by using zlib and a load function that
uses gzread instead of fread. It will also work for uncompressed files. (It is
also easy to change the writer in luac.c to use zlib and so have luac output
compressed files.) Here is some code for loading:

 #include <zlib.h>

 static const char *getF(lua_State *L, void *ud, size_t *size)
 {
  gzFile *f=(gzFile *)ud;
  static char buff[512];
  if (gzeof(f)) return NULL;
  *size=gzread(f,buff,sizeof(buff));
  return (*size>0) ? buff : NULL;
 }

 int luaL_gzloadfile(lua_State *L, const char *filename)
 {
  int status;
  gzFile f=gzopen(filename,"rb");
  if (f==NULL) {
   const char *why = (errno==0) ? "not enough memory in zlib" : strerror(errno);
   luaL_error(L,"cannot open %s: %s",filename,why); 
  }
  status=lua_load(L,getF,f,filename);
  gzclose(f);
  return status;
 }

--lhf