This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- I suggest to execute this script using lua5.3+ (for integer division) | |
-- Algorithm was rewritten from https://www.lua.org/source/5.1/ltablib.c.html#auxsort | |
local function sort(a, comp, l, u) | |
while(l < u) do | |
if comp(a[u], a[l]) then -- a[u] < a[l] | |
a[u], a[l] = a[l], a[u] -- swap | |
end | |
if u-l == 1 then -- only 2 elements | |
break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- v: 1 | |
local ffi = require 'ffi' | |
local C = ffi.C | |
ffi.cdef[[ | |
typedef struct node { | |
uint8_t nkids; | |
struct node **kids; | |
uint32_t sprefix; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- luacheck:ignore | |
max = { | |
req = 0, | |
seq = 0, | |
} | |
local function next_req() | |
max.req = max.req + 1 | |
return max.req | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ffi = require 'ffi' | |
local fun = require 'fun' | |
local C = ffi.C | |
local function generator(iterator, rtuple) | |
local res = C.box_iterator_next(iterator, rtuple) | |
if res == -1 then | |
box.error() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Execute with tarantool or luajit | |
assert(jit, "jit required") | |
local table_new = require 'table.new' | |
local function tablen(t, len) | |
local j = len | |
if j > 1 and t[j] == nil then | |
-- Use i = 0 here because in Lua tables are 1-indexed: | |
local i = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
collectgarbage('stop') | |
local MAX = 1000000 | |
print("50: for+..", clock.bench(function() for j = 1, MAX do local s = "" for i = 1, 50 do s = s .. i end end end)[1]) | |
print("50: for+table.insert", clock.bench(function() for j = 1, MAX do local s = {} for i = 1, 50 do table.insert(s, i) end table.concat(s) end end)[1]) | |
print("50: for+table", clock.bench(function() for j = 1, MAX do local s = {} for i = 1, 50 do s[i] = i end table.concat(s) end end)[1]) | |
print("50: for+table+new", clock.bench(function() for j = 1, MAX do local s = table.new(50, 0) for i = 1, 50 do s[i] = i end table.concat(s) end end)[1]) | |
print("100: for+..", clock.bench(function() for j = 1, MAX do local s = "" for i = 1, 100 do s = s .. i end end end)[1]) | |
print("100: for+table.insert", clock.bench(function() for j = 1, MAX do local s = {} for i = 1, 100 do table.insert(s, i) end table.concat(s) end end)[1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local clock = require 'clock'.time | |
function getms1() | |
local bench = function(f, ...) | |
local tail = function(s, ...) | |
return clock()-s, ... | |
end | |
local s = clock() | |
return tail(s, f(...)) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env tarantool | |
local function stack() | |
local i = 1 | |
while debug.getinfo(i) do | |
i = i * 2 | |
end | |
local l, r = math.floor(i/2), i | |
while l < r - 1 do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/tarantool | |
local fiber = require 'fiber' | |
local clock = require 'clock' | |
local log = require 'log' | |
local bus = setmetatable({ | |
put = function(self, msg) | |
while not self.closed and self.msg ~= nil do | |
self.cond:wait() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Coro = {} | |
Coro.__index = Coro | |
local S = { | |
queue = require 'q'(), | |
} | |
function S:push(o) | |
self.queue:push(o) | |
end |