1

In order to quickly save Lua tables containing large 1-dimensional arrays (the number of arrays is known however the number of elements isn't fixed. approximately 800,000 elements in each array), I planned to use Lua C binding in the following way-

#include "lua.h"
#include "lauxlib.h"
#include <stdio.h>
#include <assert.h>

static int save_table(lua_State *L) {
  assert(L && lua_type(L, -1) == LUA_TTABLE);

  int len, r;
  void *ptr;
  FILE *f;

  lua_pushstring(L, "p");
  lua_gettable(L, -2);
  len = lua_objlen(L, -1);
  ptr = lua_topointer(L, -1);

  f = fopen("p.bin", "wb");
  assert(f);
  r = fwrite(ptr, sizeof(int), len, f);
  printf("[p] wrote %d elements out of %d requested\n", r, len);
  fclose(f);

  lua_pop(L, 1);

  lua_pushstring(L, "q");
  lua_gettable(L, -2);
  len = lua_objlen(L, -1);
  ptr = lua_topointer(L, -1);

  f = fopen("q.bin", "wb");
  assert(f);
  r = fwrite(ptr, sizeof(float), len, f);
  printf("[q] wrote %d elements out of %d requested\n", r, len);
  fclose(f);

  lua_pop(L, 1);

  return 1;
}

int luaopen_savetable(lua_State *L) {
  static const luaL_reg Map[] = {{"save_table", save_table}, {NULL, NULL}};

  luaL_register(L, "mytask", Map);
  return 1;
}

Lua code is shown below-

-- sample table containg two 1-d array
my_table = {p = {11, 22, 33, 44}, q = {0.12, 0.23, 0.34, 0.45, 0.56}}

require "savetable"
mytask.save_table(my_table)

The above code produces two binary files with the wrong content. What is wrong here?

PS: I am using Lua 5.1. I am not sure if this is the fastest way of dumping large Lua tables. Suggestions are always welcome.

ravi
  • 6,140
  • 18
  • 77
  • 154
  • 1
    The data at the address returned by `lua_topointer()` is not a sequence of `int`s as you expect. You should use `lua_gettable(); lua_tointeger()` for every table index. – Egor Skriptunoff Jul 01 '18 at 17:25
  • Possible duplicate of [Fast serialize / deserialize table (no recursion) in Lua](https://stackoverflow.com/questions/51115497/fast-serialize-deserialize-table-no-recursion-in-lua) – brianolive Jul 01 '18 at 18:43

0 Answers0