Skip to content

Instantly share code, notes, and snippets.

View ochaton's full-sized avatar

Vladislav Grubov ochaton

View GitHub Profile
@ochaton
ochaton / graph.lua
Created January 9, 2021 08:58
Dijkstra implementation
local type Distance = number
local type VertexId = number
local record Node
preds: {VertexId:Distance}
succs: {VertexId:Distance}
end
local node_mt: metatable<Node> = {
__index = Node,
@ochaton
ochaton / orm_example.lua
Created November 14, 2020 12:31
Tarantool ORM interface
box.orm = require 'orm' . configure {
users = {
format = {
{ name = 'uid', type = 'unsigned' },
{ name = 'email', type = 'string' },
},
indexes = {
{ name = 'primary', parts = { 'uid' } },
{ name = 'email', parts = { 'email', 'uid' } },
},
@ochaton
ochaton / fibergc.lua
Last active November 3, 2020 21:03
Fibers collected by gc even if they still alive
#!/usr/bin/env tarantool
print(_G._TARANTOOL)
-- box.cfg{}
require 'jit.dump'.start("hotloop=1")
local fiber = require 'fiber'
local log = require 'log'
local json = require 'json'
json.cfg{ encode_use_tostring = true }
@ochaton
ochaton / replicator.lua
Last active October 29, 2020 23:05
Online parser of Tarantool 1.5 replication (working draft)
---
-- TODO:
-- Support all ops for update
-- Create separate module
local ffi = require 'ffi'
local fiber = require 'fiber'
ffi.cdef[[
struct header_v11 {
uint32_t header_crc32c;
@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
@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 / 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 / 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
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 / 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