Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization scripts #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cnt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 21 additions & 20 deletions expirationd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
34 changes: 22 additions & 12 deletions notifications.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
6 changes: 4 additions & 2 deletions profiles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions sorted_array.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
28 changes: 15 additions & 13 deletions statistics.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,15 +16,15 @@ 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
else
--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)
Expand All @@ -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
Expand All @@ -52,17 +54,17 @@ 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)
end
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
Expand All @@ -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
Expand All @@ -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
Expand Down