Skip to content

Instantly share code, notes, and snippets.

View ochaton's full-sized avatar

Vladislav Grubov ochaton

  • Tarantool
  • Moscow, Russia
View GitHub Profile
@ochaton
ochaton / tablesort.lua
Last active November 21, 2019 08:53
First attempt to understand how table.sort works in Lua 5.1
-- 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
@ochaton
ochaton / prefix-tree.lua
Created November 26, 2019 12:19
ffi prefix-tree
-- v: 1
local ffi = require 'ffi'
local C = ffi.C
ffi.cdef[[
typedef struct node {
uint8_t nkids;
struct node **kids;
uint32_t sprefix;
@ochaton
ochaton / bus.lua
Created February 5, 2020 13:22
pub/sub bus lua
-- luacheck:ignore
max = {
req = 0,
seq = 0,
}
local function next_req()
max.req = max.req + 1
return max.req
end
@ochaton
ochaton / fast_iterator.lua
Last active March 6, 2024 18:18
Fast Index Tarantool Iterator
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()
@ochaton
ochaton / tablen.lua
Last active March 28, 2020 21:48
Attempt to understand how #t works in Lua
-- 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
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])
@ochaton
ochaton / bench.lua
Created May 27, 2020 16:28
helpers to consume CPU for specific amount of time
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
@ochaton
ochaton / factorial.lua
Created July 7, 2020 21:23
Infinitely deep recursion in Lua
#!/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
@ochaton
ochaton / conds.lua
Created July 28, 2020 10:42
Benchmarking of Tarantool IPC
#!/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()
@ochaton
ochaton / coro.lua
Created October 27, 2020 20:05
self made S3 (Simple Stupid Scheduler)
local Coro = {}
Coro.__index = Coro
local S = {
queue = require 'q'(),
}
function S:push(o)
self.queue:push(o)
end