From ab393a2c2192d34beffec11da7cf82aef8590843 Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Sun, 1 May 2011 17:15:47 +0200 Subject: [PATCH] [fluksod] use the new table.create to eliminate the rehashes in json_encode --- .../package/flukso/luasrc/flukso/data.lua | 19 ++++++++++++++++--- .../openwrt/package/flukso/luasrc/fluksod.lua | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/mote/v2/openwrt/package/flukso/luasrc/flukso/data.lua b/mote/v2/openwrt/package/flukso/luasrc/flukso/data.lua index b01d723..bec8dc5 100644 --- a/mote/v2/openwrt/package/flukso/luasrc/flukso/data.lua +++ b/mote/v2/openwrt/package/flukso/luasrc/flukso/data.lua @@ -113,19 +113,32 @@ function polish(M, now, cutoff) end end -function json_encode(M) +function json_encode(M, entries) local J = {} + if entries then + arr_size = 5*entries + 2 + else + arr_size = 0 + end + for sensor, T in pairs(M) do local H = timestamps(T) - local SB = {'['} -- use a string buffer for building up the JSON string + + local SB = table.create(arr_size, 0) + SB[1] = '[' -- use a string buffer for building up the JSON string for k, timestamp in ipairs(H) do - SB[#SB+1] = '[' .. timestamp .. ',' .. T[timestamp] .. '],' + SB[#SB+1] = '[' + SB[#SB+1] = timestamp + SB[#SB+1] = ',' + SB[#SB+1] = T[timestamp] + SB[#SB+1] = '],' end SB[#SB] = SB[#SB]:sub(1, -2) -- remove the trailing comma from the last entry SB[#SB+1] = ']' + J[sensor] = table.concat(SB) end diff --git a/mote/v2/openwrt/package/flukso/luasrc/fluksod.lua b/mote/v2/openwrt/package/flukso/luasrc/fluksod.lua index ffa1dcc..c524db9 100755 --- a/mote/v2/openwrt/package/flukso/luasrc/fluksod.lua +++ b/mote/v2/openwrt/package/flukso/luasrc/fluksod.lua @@ -321,7 +321,7 @@ function publish(child) while true do measurements:polish(os.time(), LAN_POLISH_CUTOFF) - local measurements_json = measurements:json_encode() + local measurements_json = measurements:json_encode(LAN_POLISH_CUTOFF) for sensor_id, json in pairs(measurements_json) do local file = LAN_PUBLISH_PATH .. '/' .. sensor_id