Skip to content

Instantly share code, notes, and snippets.

@kingluo
Created April 6, 2015 12:54
Show Gist options
  • Save kingluo/7120d845ac8b12c71e6b to your computer and use it in GitHub Desktop.
Save kingluo/7120d845ac8b12c71e6b to your computer and use it in GitHub Desktop.
who can explain the -jv output in detail?
-- Copyright (C) Jinhua Luo
local select = select
local type = type
local tostring = tostring
local ipairs = ipairs
local tinsert = table.insert
local tremove = table.remove
local tpool = {n = 0}
local function append(t, ...)
local n = select("#", ...)
if n > 0 then
local v = ...
local typ = type(v)
if typ == "table" then
t.size = t.size + v.size
else
if typ ~= "string" then v = tostring(v) end
t.size = t.size + #v
end
tinsert(t, v)
if n > 1 then
return append(t, select(2, ...))
end
end
end
local function clear(t, idx)
idx = idx or #t
if idx > 0 then
local v = t[idx]
if type(v) == "table" then
v:free()
end
t[idx] = nil
if idx > 1 then
return clear(t, idx - 1)
end
end
end
local function free(t)
t:clear()
t.size = 0
tinsert(tpool, t)
tpool.n = tpool.n + 1
end
local mt = {
__index = {
free = free,
append = append,
clear = clear,
}
}
local function alloc()
local t
if tpool.n > 0 then
t = tremove(tpool)
tpool.n = tpool.n - 1
else
t = setmetatable({size = 0}, mt)
end
return t
end
local total = 0
for i = 1, 10000000 do
local t = alloc()
t:append("hello")
t:append("world")
t:append("foo")
local t2 = alloc()
t2:append(1,2,3)
t:append(t2)
total = total + #t
t:free()
-- local t = {}
-- tinsert(t, "hello")
-- tinsert(t, "world")
-- tinsert(t, "foo")
-- local t2 = {}
-- tinsert(t2, 1)
-- tinsert(t2, 2)
-- tinsert(t2, 3)
-- tinsert(t, t2)
-- total = total + #t
end
print(total)
return {
alloc = alloc
}
root@debian:~/ljio2# time luajit -jv lib/ljio/http/buf.lua
[TRACE 1 buf.lua:30 return]
[TRACE --- (1/3) buf.lua:39 -- down-recursion, restarting at buf.lua:42]
[TRACE --- buf.lua:42 -- inner loop in root trace at buf.lua:73]
[TRACE --- (1/0) buf.lua:31 -- down-recursion, restarting at buf.lua:42]
[TRACE --- buf.lua:42 -- inner loop in root trace at buf.lua:73]
[TRACE --- (1/0) buf.lua:31 -- loop unroll limit reached at buf.lua:25]
[TRACE --- (1/0) buf.lua:31 -- loop unroll limit reached at buf.lua:25]
[TRACE --- (1/0) buf.lua:31 -- loop unroll limit reached at buf.lua:25]
[TRACE 2 (1/0) buf.lua:31 -- fallback to interpreter]
[TRACE --- (1/3) buf.lua:39 -- down-recursion, restarting at buf.lua:42]
[TRACE --- buf.lua:42 -- inner loop in root trace at buf.lua:73]
[TRACE --- (1/3) buf.lua:39 -- down-recursion, restarting at buf.lua:42]
[TRACE --- buf.lua:42 -- inner loop in root trace at buf.lua:73]
[TRACE --- (1/3) buf.lua:39 -- down-recursion, restarting at buf.lua:42]
[TRACE --- buf.lua:42 -- inner loop in root trace at buf.lua:73]
[TRACE 3 (1/3) buf.lua:39 -- fallback to interpreter]
[TRACE 4 buf.lua:44 return]
[TRACE 5 buf.lua:59 return]
[TRACE 6 buf.lua:72 loop]
40000000
real 0m2.373s
user 0m2.336s
sys 0m0.028s
root@debian:~/ljio2# luajit -v
LuaJIT 2.1.0-alpha -- Copyright (C) 2005-2015 Mike Pall. http://luajit.org/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment