diff --git a/cnt.lua b/cnt.lua index ea8e01b..65c460d 100644 --- a/cnt.lua +++ b/cnt.lua @@ -20,7 +20,8 @@ function box.counter.inc(space, ...) tuple = box.update(space, key, '+p', cnt_index, 1) if tuple ~= nil then break end local data = {...} - table.insert(data, 1) + data[#data + 1] = 1 +-- table.insert(data, 1) tuple = box.insert(space, unpack(data)) if tuple ~= nil then break end end diff --git a/expirationd.lua b/expirationd.lua index 6d488ae..1809cdd 100644 --- a/expirationd.lua +++ b/expirationd.lua @@ -14,16 +14,18 @@ -- Create a new table with constant members. A runtime error -- is raised on attempt to change a table field. + +local concat = table.concat + local function finalize_table(table) return setmetatable ({}, { __index = table, __newindex = function(table_arg, name_arg, value_arg) - error("attempt to change constant " .. - tostring(name_arg) .. - " to " - .. tostring(value_arg), 2) + error(concat{"attempt to change constant ", + tostring(name_arg), " to ", + tostring(value_arg), 2}) end }) end @@ -83,8 +85,7 @@ local function worker_loop(task) if checks >= task.tuples_per_iter then checks = 0 if scan_space:len() > 0 then - local delay = (task.tuples_per_iter * task.full_scan_time) - / scan_space:len() + local delay = (task.tuples_per_iter * task.full_scan_time) * (1 / scan_space:len()) if delay > expirationd.constants.max_delay then delay = expirationd.constants.max_delay @@ -107,16 +108,16 @@ end local function guardian_loop(task) -- detach the guardian from the creator and attach it to sched box.fiber.detach() - box.fiber.name("guardian of "..task.name) + box.fiber.name(concat{"guardian of ", task.name}) - print("expiration: task '" .. task.name .. "' started") + print(concat{"expiration: task '", task.name, "' started"}) -- create a worker fiber task.worker_fiber = box.fiber.create(worker_loop) box.fiber.resume(task.worker_fiber, task) while true do if task.worker_fiber:id() == 0 then - print("expiration: task '" .. task.name .. "' restarted") + print(concat{"expiration: task '", task.name, "' restarted"}) task.restarts = task.restarts + 1 -- create worker fiber task.worker_fiber = box.fiber.create(worker_loop) @@ -160,7 +161,7 @@ local function get_task(name) -- check, does the task exist if task_list[name] == nil then - error("task '" .. name .. "' doesn't exist") + error(concat{"task '", name, "' doesn't exist"}) end return task_list[name] @@ -243,7 +244,7 @@ function expirationd.run_task(name, -- check, does the task exist if task_list[name] ~= nil then - error("task '" .. name .. "' already running") + error(concat{"task '", name, "' already running"}) end local task = create_task(name) @@ -320,17 +321,17 @@ end -- function expirationd.show_task_list(print_head) if print_head == nil or print_head == true then - print("name" .. "\t" .. - "space" .. "\t" .. - "expired" .. "\t" .. - "time") + print(concat{"name", "\t", + "space", "\t", + "expired", "\t", + "time"}) print("-----------------------------------") end for i, task in pairs(task_list) do - print(task.name .. "\t" .. - task.space_no .. "\t" .. - task.tuples_expired .. "\t" .. - math.floor(os.time() - task.start_time)) + print(concat{task.name, "\t", + task.space_no, "\t", + task.tuples_expired, "\t", + math.floor(os.time() - task.start_time)}) end end @@ -399,7 +400,7 @@ end -- generate email string local function get_email(uid) - local email = "test_" .. uid .. "@sex.com" + local email = concat{"test_", uid, "@sex.com"} return email end diff --git a/notifications.lua b/notifications.lua index 8d8f690..9ed424b 100644 --- a/notifications.lua +++ b/notifications.lua @@ -63,7 +63,7 @@ local ring_max = 20 -- field index of the first ring in the tuple. -- field 0 - user id, field 1 - total unread count, hence local ring0_fieldno = 2 - +local concat = table.concat -- Find the tuple associated with user id or create a new -- one. function notification_find_or_insert(user_id) @@ -74,8 +74,10 @@ function notification_find_or_insert(user_id) -- create an empty ring for each notification type local rings = {} for i = 1, #notification_types do - table.insert(rings, 0) -- unread count - 0 - table.insert(rings, "") -- notification list - empty + rings[#rings + 1] = 0 + rings[#rings + 1] = "" +-- table.insert(rings, 0) -- unread count - 0 +-- table.insert(rings, "") -- notification list - empty end box.insert(space_no, user_id, 0, unpack(rings)) -- we can't use the tuple returned by box.insert, since it could @@ -99,15 +101,17 @@ end -- false len(ring) when not found -- local function is_duplicate(ring, id, tail_offset) - id_len = #id - id = string.sub(id, 1, id_len - tail_offset) + local id_len = #id + local id_len_min = id_len - 1 + local sub = string.sub + local id = sub(id, 1, id_len - tail_offset) for i=1, #ring, id_len do - n_id = string.sub(ring, i, i+id_len-1 - tail_offset) + n_id = sub(ring, i, i+ id_len_min - tail_offset) if n_id == id then -- we're going to insert a new notifiation, -- return the old notification offset relative to -- the new field size - return true, i - 1 + id_len + return true, i + id_len_min end end return false, ring_max * id_len @@ -138,10 +142,13 @@ function notification_push(user_id, ring_no, id) -- check if the counters need to be updated if unread_count < ring_max and dup_offset > unread_count * #id then -- prepend ++total_unread_count, ++unread_count to our update operation - format = "+p+p"..format + format = concat{"+p+p", format} local args1 = args args = { 1, 1, fieldno, 1 } - for _, v in ipairs(args1) do table.insert(args, v) end + for _, v in ipairs(args1) do + -- table.insert(args, v) + args[#args + 1 ] = v + end end return box.update(space_no, user_id, format, unpack(args)) end @@ -158,15 +165,18 @@ function notification_read(user_id, ring_no) local return_fields = {} local k, v = tuple:next() local k, v = tuple:next(k) -- skip user id - table.insert(return_fields, v) -- total unread count + return_fields[#return_fields + 1] = v +-- table.insert(return_fields, v) -- total unread count k, v = tuple:next(k) -- now at the first ring while k ~= nil do - table.insert(return_fields, v) -- insert unread count + return_fields[#return_fields + 1] = v +-- table.insert(return_fields, v) -- insert unread count k, v = tuple:next(k) if k ~= nil then k, v = tuple:next(k) -- skip this ring data end end - table.insert(return_fields, tuple[fieldno+1]) -- insert ring data + return_fields[#return_fields + 1] = tuple[fieldno+1] +-- table.insert(return_fields, tuple[fieldno+1]) -- insert ring data return return_fields end diff --git a/profiles.lua b/profiles.lua index c304f86..31c1ff1 100644 --- a/profiles.lua +++ b/profiles.lua @@ -82,9 +82,11 @@ local function store_profile(profile) -- put preference to tuple for pref_id, pref_value in pairs(profile.prefs) do -- insert preference id - table.insert(tuple, pref_id) +-- table.insert(tuple, pref_id) + tuple[#tuple + 1] = pref_id -- insert preference value - table.insert(tuple, pref_value) + tuple[#tuple + 1] = pref_value +-- table.insert(tuple, pref_value) end return box.replace(space_no, unpack(tuple)) end diff --git a/sorted_array.lua b/sorted_array.lua index 3797dfe..732ab40 100644 --- a/sorted_array.lua +++ b/sorted_array.lua @@ -6,7 +6,7 @@ local floor = require("math").floor local function s_to_a(data) local a = {} local bytes = {string.byte(data, i, #data)} - for i = 1, #data / 8 do + for i = 1, #data * 0.125 do -- #data * 0.125 will faster then #data/8 a[i] = 0 for c = 0, 7 do a[i] = bit.lshift(a[i], 8) @@ -20,7 +20,8 @@ local function a_to_s(a) local bytes = {} for i = 1, #a do for c = 0, 7 do - table.insert(bytes, bit.band(a[i], 0xff)) + bytes[#bytes + 1] = bit.band(a[i], 0xff) + --table.insert(bytes, bit.band(a[i], 0xff)) a[i] = bit.rshift(a[i], 8) end end @@ -33,18 +34,22 @@ local function amerge(a, b) local n = limit while #a > 0 and #b > 0 and n > 0 do if a[1] > b[1] then - table.insert(r, table.remove(a, 1)) + r[#r + 1] = table.remove(a, 1) + --table.insert(r, table.remove(a, 1)) else - table.insert(r, table.remove(b, 1)) + r[#r + 1] = table.remove(b, 1) + --table.insert(r, table.remove(b, 1)) end n = n - 1 end while #a > 0 and n > 0 do - table.insert(r, table.remove(a, 1)) + r[#r + 1] = table.remove(a, 1) + --table.insert(r, table.remove(a, 1)) n = n - 1 end while #b > 0 and n > 0 do - table.insert(r, table.remove(b, 1)) + r[#r + 1] = table.remove(b, 1) + --table.insert(r, table.remove(b, 1)) n = n - 1 end return r @@ -58,7 +63,7 @@ local function afind_ge(a, x) local first, last = 1, #a local mid repeat - mid = floor(first + (last - first) / 2) + mid = floor(first + (last - first) * 0.5) if x > a[mid] then last = mid else @@ -89,7 +94,7 @@ local function ains(a, key) end local function adel(a, key) - key = tonumber(key) + local key = tonumber(key) local i = afind_ge(a, key) if a[i] == key then table.remove(a, i) @@ -145,7 +150,8 @@ function box.sa_select(space, key, from, limit) if a[i] == nil then break end - table.insert(r, a[i]) + r[#r + 1] = a[i] + --table.insert(r, a[i]) limit = limit - 1 if limit == 0 then break diff --git a/statistics.lua b/statistics.lua index 3fe99a3..db9875b 100644 --- a/statistics.lua +++ b/statistics.lua @@ -1,6 +1,8 @@ -field_count = 10 -timeout = 0.006 -max_attempts = 5 +local field_count = 10 +local timeout = 0.006 +local max_attempts = 5 +local concat = table.concat +local sub = string.sub function increment_or_insert(space, key, field) retry = true @@ -14,7 +16,7 @@ function increment_or_insert(space, key, field) --insert new tuple tuple = {} for i = 2, field_count do tuple[i] = 0 end - tuple[1] = string.sub(key, -8) + tuple[1] = sub(key, -8) tuple[tonumber(field)] = 1 box.insert(space, key, unpack(tuple)) end @@ -22,7 +24,7 @@ function increment_or_insert(space, key, field) --exception count = count + 1 if count == max_attempts then - print("max attempts reached for space="..space.." key="..key.." field="..field) + print(concat{"max attempts reached for space=",space," key=",key," field=",field}) break end box.fiber.sleep(timeout) @@ -42,7 +44,7 @@ function increment_or_insert_2(space, key, field, element1, element2) --insert new tuple tuple = {} for i = 2, field_count + 2 do tuple[i] = 0 end - tuple[1] = string.sub(key, -8) + tuple[1] = sub(key, -8) tuple[tonumber(field)] = 1 tuple[field_count + 1] = element1 tuple[field_count + 2] = element2 @@ -52,7 +54,7 @@ function increment_or_insert_2(space, key, field, element1, element2) --exception count = count + 1 if count == max_attempts then - print("max attempts reached for space="..space.." key="..key.." field="..field) + print(concat{"max attempts reached for space=",space," key=",key," field=",field}) break end box.fiber.sleep(timeout) @@ -60,9 +62,9 @@ function increment_or_insert_2(space, key, field, element1, element2) end end -field_subject = 2 -field_first = 3 -field_last = 4 +local field_subject = 2 +local field_first = 3 +local field_last = 4 function update_or_insert(space, key, subject, timestamp) retry = true @@ -76,7 +78,7 @@ function update_or_insert(space, key, subject, timestamp) --insert new tuple tuple = {} for i = 2, field_last do tuple[i] = 0 end - tuple[1] = string.sub(key, -8) + tuple[1] = sub(key, -8) tuple[field_subject] = subject tuple[field_first] = timestamp tuple[field_last] = timestamp @@ -86,8 +88,8 @@ function update_or_insert(space, key, subject, timestamp) --exception count = count + 1 if count == max_attempts then - print("max attempts reached for space="..space.." key="..key) - break + print(concat{"max attempts reached for space=",space," key=",key}) + break end box.fiber.sleep(timeout) end