lua-users home
lua-l archive

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



Hi.

I have previously (http://lua-users.org/lists/lua-l/2003-12/msg00160.html) asked about tracking table access in Lua 5. In my program I’m using Lua table to store data. The table contains sub tables inside sub tables etc.

My idea is to make data table to analyze it’s contents automatically. The analysis is then used later on. Currently I’m doing the analysis by recursively going through the whole table when changes happen. I thought that tracking table access could be more efficient because in case of changes in data table I only have to track modifications of table and don’t have to analyze whole table again. Usually the changes in data table are small but the table itself is big. I require more than just info that table is being accessed.

Simple example of tracking of table access:
local index = {}

local mt =
{
  __index = function(t, k)
    print('*access of element ' .. tostring(k))
    return t[index][k]
  end,

  __newindex = function(t, k, v)
    print('*update of element ' .. tostring(k) .. ' to ' .. tostring(v))
    t[index][k] = v
  end
}

function track(t)
  local proxy = {}
  proxy[index] = t
  setmetatable(proxy, mt)
  return proxy
end

t = {}
t = track(t)

t[1] = 'a'	--> *update of element 1 to a
print(t[1])	--> *access of element 1
	--> a

t.x =
{
  y = 'y',
  z = {1, 2, 3},
}	--> *update of element x to table: 00619110

The problem is that this implementation does not track sub tables access. And for analyze to work I need to track all access. Even if I add metatable to all tables when they are added I cannot track all the things happening in the previous “t.x = ...” example.

I’d like to hear your opinions and suggestions. Perhaps somebody has already implemented something similar?

floru