keep track of accumulator size instead of using length operator

should be faster
This commit is contained in:
leaf corcoran 2011-08-13 19:19:17 -07:00
parent a76c251899
commit faf21dd805
8 changed files with 154 additions and 83 deletions

View File

@ -101,12 +101,14 @@ Block_ = (function(_parent_0)
declare = function(self, names) declare = function(self, names)
local undeclared = (function() local undeclared = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = names local _item_0 = names
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local name = _item_0[_index_0] local name = _item_0[_index_0]
if type(name) == "string" and not self:has_name(name) then if type(name) == "string" and not self:has_name(name) then
table.insert(_accum_0, name) _len_0 = _len_0 + 1
_accum_0[_len_0] = name
end end
end end
end end
@ -316,11 +318,13 @@ Block_ = (function(_parent_0)
local _with_0 = Line() local _with_0 = Line()
_with_0:append_list((function() _with_0:append_list((function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = values local _item_0 = values
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local v = _item_0[_index_0] local v = _item_0[_index_0]
table.insert(_accum_0, self:value(v)) _len_0 = _len_0 + 1
_accum_0[_len_0] = self:value(v)
end end
end end
return _accum_0 return _accum_0

View File

@ -21,11 +21,13 @@ line_compile = {
local _with_0 = self:line("local ") local _with_0 = self:line("local ")
_with_0:append_list((function() _with_0:append_list((function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = names local _item_0 = names
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local name = _item_0[_index_0] local name = _item_0[_index_0]
table.insert(_accum_0, self:name(name)) _len_0 = _len_0 + 1
_accum_0[_len_0] = self:name(name)
end end
end end
return _accum_0 return _accum_0
@ -83,11 +85,13 @@ line_compile = {
end end
_with_0:append_list((function() _with_0:append_list((function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = names local _item_0 = names
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local name = _item_0[_index_0] local name = _item_0[_index_0]
table.insert(_accum_0, self:value(name)) _len_0 = _len_0 + 1
_accum_0[_len_0] = self:value(name)
end end
end end
return _accum_0 return _accum_0
@ -96,11 +100,13 @@ line_compile = {
_with_0:append(" = ") _with_0:append(" = ")
_with_0:append_list((function() _with_0:append_list((function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = values local _item_0 = values
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local v = _item_0[_index_0] local v = _item_0[_index_0]
table.insert(_accum_0, self:value(v)) _len_0 = _len_0 + 1
_accum_0[_len_0] = self:value(v)
end end
end end
return _accum_0 return _accum_0
@ -166,11 +172,13 @@ line_compile = {
if type(source) == "string" then if type(source) == "string" then
local values = (function() local values = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = final_names local _item_0 = final_names
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local name = _item_0[_index_0] local name = _item_0[_index_0]
table.insert(_accum_0, get_value(name)) _len_0 = _len_0 + 1
_accum_0[_len_0] = get_value(name)
end end
end end
return _accum_0 return _accum_0
@ -334,11 +342,13 @@ line_compile = {
}) })
block = (function() block = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = block local _item_0 = block
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local s = _item_0[_index_0] local s = _item_0[_index_0]
table.insert(_accum_0, s) _len_0 = _len_0 + 1
_accum_0[_len_0] = s
end end
end end
return _accum_0 return _accum_0
@ -380,11 +390,13 @@ line_compile = {
_with_0:append("for ") _with_0:append("for ")
_with_0:append_list((function() _with_0:append_list((function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = names local _item_0 = names
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local name = _item_0[_index_0] local name = _item_0[_index_0]
table.insert(_accum_0, self:name(name)) _len_0 = _len_0 + 1
_accum_0[_len_0] = self:name(name)
end end
end end
return _accum_0 return _accum_0
@ -460,11 +472,13 @@ line_compile = {
end end
constructor.args = (function() constructor.args = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = constructor.args local _item_0 = constructor.args
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local arg = _item_0[_index_0] local arg = _item_0[_index_0]
table.insert(_accum_0, get_initializers(arg)) _len_0 = _len_0 + 1
_accum_0[_len_0] = get_initializers(arg)
end end
end end
return _accum_0 return _accum_0
@ -472,14 +486,16 @@ line_compile = {
constructor.arrow = "fat" constructor.arrow = "fat"
local dests = (function() local dests = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = self_args local _item_0 = self_args
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local name = _item_0[_index_0] local name = _item_0[_index_0]
table.insert(_accum_0, { _len_0 = _len_0 + 1
_accum_0[_len_0] = {
"self", "self",
name name
}) }
end end
end end
return _accum_0 return _accum_0
@ -504,11 +520,13 @@ line_compile = {
local calling_name = block:get("current_block") local calling_name = block:get("current_block")
local slice = (function() local slice = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = chain local _item_0 = chain
for _index_0 = 3, #_item_0 do for _index_0 = 3, #_item_0 do
local item = _item_0[_index_0] local item = _item_0[_index_0]
table.insert(_accum_0, item) _len_0 = _len_0 + 1
_accum_0[_len_0] = item
end end
end end
return _accum_0 return _accum_0
@ -676,38 +694,39 @@ line_compile = {
local _, exp, clauses = unpack(node) local _, exp, clauses = unpack(node)
if not action then if not action then
action = function(exp) action = function(exp)
return exp return {
exp
}
end end
end end
local statement = action(exp) local current_stms = action(exp)
for _, clause in reversed(clauses) do for _, clause in reversed(clauses) do
local t = clause[1] local t = clause[1]
if t == "for" then if t == "for" then
local names, iter local names, iter
_, names, iter = unpack(clause) _, names, iter = unpack(clause)
statement = { current_stms = {
"foreach", "foreach",
names, names,
iter, iter,
{ current_stms
statement
}
} }
elseif t == "when" then elseif t == "when" then
local cond local cond
_, cond = unpack(clause) _, cond = unpack(clause)
statement = { current_stms = {
"if", "if",
cond, cond,
{ current_stms
statement
}
} }
else else
statement = error("Unknown comprehension clause: " .. t) current_stms = error("Unknown comprehension clause: " .. t)
end end
current_stms = {
current_stms
}
end end
return self:stm(statement) return self:stms(current_stms)
end, end,
with = function(self, node, ret) with = function(self, node, ret)
local _, exp, block = unpack(node) local _, exp, block = unpack(node)

View File

@ -307,21 +307,22 @@ line_compile =
_, exp, clauses = unpack node _, exp, clauses = unpack node
if not action if not action
action = (exp) -> exp action = (exp) -> {exp}
statement = action exp current_stms = action exp
for _, clause in reversed clauses for _, clause in reversed clauses
t = clause[1] t = clause[1]
statement = if t == "for" current_stms = if t == "for"
_, names, iter = unpack clause _, names, iter = unpack clause
{"foreach", names, iter, {statement}} {"foreach", names, iter, current_stms}
elseif t == "when" elseif t == "when"
_, cond = unpack clause _, cond = unpack clause
{"if", cond, {statement}} {"if", cond, current_stms}
else else
error "Unknown comprehension clause: "..t error "Unknown comprehension clause: "..t
current_stms = {current_stms}
@stm statement @stms current_stms
with: (node, ret) => with: (node, ret) =>

View File

@ -6,8 +6,15 @@ require("moonscript.compile.format")
local ntype = data.ntype local ntype = data.ntype
local concat, insert = table.concat, table.insert local concat, insert = table.concat, table.insert
local table_append local table_append
table_append = function(name, value) table_append = function(name, len, value)
return { return {
{
"update",
len,
"+=",
1
},
{
"assign", "assign",
{ {
{ {
@ -15,15 +22,7 @@ table_append = function(name, value)
name, name,
{ {
"index", "index",
{ len
"exp",
{
"length",
name
},
"+",
1
}
} }
} }
}, },
@ -31,6 +30,7 @@ table_append = function(name, value)
value value
} }
} }
}
end end
local create_accumulate_wrapper local create_accumulate_wrapper
create_accumulate_wrapper = function(block_pos) create_accumulate_wrapper = function(block_pos)
@ -40,6 +40,7 @@ create_accumulate_wrapper = function(block_pos)
local accum_name = _with_0:init_free_var("accum", { local accum_name = _with_0:init_free_var("accum", {
"table" "table"
}) })
local count_name = _with_0:init_free_var("len", 0)
local value_name = _with_0:free_name("value", true) local value_name = _with_0:free_name("value", true)
local inner = node[block_pos] local inner = node[block_pos]
inner[#inner] = { inner[#inner] = {
@ -59,9 +60,7 @@ create_accumulate_wrapper = function(block_pos)
"~=", "~=",
"nil" "nil"
}, },
{ table_append(accum_name, count_name, value_name)
table_append(accum_name, value_name)
}
}) })
_with_0:stm(node) _with_0:stm(node)
_with_0:stm({ _with_0:stm({
@ -85,9 +84,11 @@ value_compile = {
local _with_0 = self:line() local _with_0 = self:line()
_with_0:append_list((function() _with_0:append_list((function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for i, v in ipairs(node) do for i, v in ipairs(node) do
if i > 1 then if i > 1 then
table.insert(_accum_0, _comp(i, v)) _len_0 = _len_0 + 1
_accum_0[_len_0] = _comp(i, v)
end end
end end
return _accum_0 return _accum_0
@ -105,11 +106,13 @@ value_compile = {
local _with_0 = self:line() local _with_0 = self:line()
_with_0:append_list((function() _with_0:append_list((function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = node local _item_0 = node
for _index_0 = 2, #_item_0 do for _index_0 = 2, #_item_0 do
local v = _item_0[_index_0] local v = _item_0[_index_0]
table.insert(_accum_0, self:value(v)) _len_0 = _len_0 + 1
_accum_0[_len_0] = self:value(v)
end end
end end
return _accum_0 return _accum_0
@ -145,9 +148,10 @@ value_compile = {
local tmp_name = _with_0:init_free_var("accum", { local tmp_name = _with_0:init_free_var("accum", {
"table" "table"
}) })
local len_name = _with_0:init_free_var("len", 0)
local action local action
action = function(value) action = function(value)
return table_append(tmp_name, value) return table_append(tmp_name, len_name, value)
end end
_with_0:stm(node, action) _with_0:stm(node, action)
_with_0:stm({ _with_0:stm({
@ -229,11 +233,13 @@ value_compile = {
end end
args = (function() args = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = args local _item_0 = args
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local arg = _item_0[_index_0] local arg = _item_0[_index_0]
table.insert(_accum_0, format_names(arg)) _len_0 = _len_0 + 1
_accum_0[_len_0] = format_names(arg)
end end
end end
return _accum_0 return _accum_0

View File

@ -12,23 +12,25 @@ import concat, insert from table
export value_compile export value_compile
table_append = (name, value) -> table_append = (name, len, value) ->
{
{"update", len, "+=", 1}
{"assign", { {"assign", {
{"chain", name, {"index", {"exp", {"length", name}, "+", 1}}} {"chain", name, {"index", len}} }, { value }}
}, { value }} }
create_accumulate_wrapper = (block_pos) -> create_accumulate_wrapper = (block_pos) ->
(node) => (node) =>
with @block "(function()", "end)()" with @block "(function()", "end)()"
accum_name = \init_free_var "accum", {"table"} accum_name = \init_free_var "accum", {"table"}
count_name = \init_free_var "len", 0
value_name = \free_name "value", true value_name = \free_name "value", true
inner = node[block_pos] inner = node[block_pos]
inner[#inner] = {"assign", {value_name}, {inner[#inner]}} inner[#inner] = {"assign", {value_name}, {inner[#inner]}}
insert inner, { insert inner, {
"if", {"exp", value_name, "~=", "nil"}, { "if", {"exp", value_name, "~=", "nil"},
table_append accum_name, value_name table_append accum_name, count_name, value_name
}
} }
\stm node \stm node
@ -73,9 +75,10 @@ value_compile =
with @block! with @block!
tmp_name = \init_free_var "accum", {"table"} tmp_name = \init_free_var "accum", {"table"}
len_name = \init_free_var "len", 0
action = (value) -> action = (value) ->
table_append tmp_name, value table_append tmp_name, len_name, value
\stm node, action \stm node, action
\stm {"return", tmp_name} \stm {"return", tmp_name}

View File

@ -1,12 +1,14 @@
local hi = (function() local hi = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for _, x in ipairs({ for _, x in ipairs({
1, 1,
2, 2,
3, 3,
4 4
}) do }) do
_accum_0[#_accum_0 + 1] = x * 2 _len_0 = _len_0 + 1
_accum_0[_len_0] = x * 2
end end
return _accum_0 return _accum_0
end)() end)()
@ -20,8 +22,10 @@ local items = {
} }
local mm = (function() local mm = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for self.x in ipairs(items) do for self.x in ipairs(items) do
_accum_0[#_accum_0 + 1] = self.x _len_0 = _len_0 + 1
_accum_0[_len_0] = self.x
end end
return _accum_0 return _accum_0
end)() end)()
@ -32,6 +36,7 @@ for z in ipairs(items) do
end end
local rad = (function() local rad = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for a in ipairs({ for a in ipairs({
1, 1,
2, 2,
@ -41,7 +46,8 @@ local rad = (function()
6 6
}) do }) do
if good_number(a) then if good_number(a) then
_accum_0[#_accum_0 + 1] = { _len_0 = _len_0 + 1
_accum_0[_len_0] = {
a a
} }
end end
@ -72,17 +78,21 @@ range = function(count)
end end
dump((function() dump((function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for x in range(10) do for x in range(10) do
_accum_0[#_accum_0 + 1] = x _len_0 = _len_0 + 1
_accum_0[_len_0] = x
end end
return _accum_0 return _accum_0
end)()) end)())
dump((function() dump((function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for x in range(5) do for x in range(5) do
if x > 2 then if x > 2 then
for y in range(5) do for y in range(5) do
_accum_0[#_accum_0 + 1] = { _len_0 = _len_0 + 1
_accum_0[_len_0] = {
x, x,
y y
} }
@ -93,11 +103,13 @@ dump((function()
end)()) end)())
local things = (function() local things = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for x in range(10) do for x in range(10) do
if x > 5 then if x > 5 then
for y in range(10) do for y in range(10) do
if y > 7 then if y > 7 then
_accum_0[#_accum_0 + 1] = x + y _len_0 = _len_0 + 1
_accum_0[_len_0] = x + y
end end
end end
end end
@ -127,8 +139,10 @@ for x in x do
end end
local x = (function() local x = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for x in x do for x in x do
_accum_0[#_accum_0 + 1] = x _len_0 = _len_0 + 1
_accum_0[_len_0] = x
end end
return _accum_0 return _accum_0
end)() end)()
@ -149,11 +163,13 @@ for x in ipairs({
end end
local double = (function() local double = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = items local _item_0 = items
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local x = _item_0[_index_0] local x = _item_0[_index_0]
_accum_0[#_accum_0 + 1] = x * 2 _len_0 = _len_0 + 1
_accum_0[_len_0] = x * 2
end end
end end
return _accum_0 return _accum_0
@ -167,12 +183,14 @@ do
end end
local cut = (function() local cut = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = items local _item_0 = items
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local x = _item_0[_index_0] local x = _item_0[_index_0]
if x > 3 then if x > 3 then
_accum_0[#_accum_0 + 1] = x _len_0 = _len_0 + 1
_accum_0[_len_0] = x
end end
end end
end end
@ -180,6 +198,7 @@ local cut = (function()
end)() end)()
local hello = (function() local hello = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = items local _item_0 = items
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
@ -188,7 +207,8 @@ local hello = (function()
local _item_1 = items local _item_1 = items
for _index_1 = 1, #_item_1 do for _index_1 = 1, #_item_1 do
local y = _item_1[_index_1] local y = _item_1[_index_1]
_accum_0[#_accum_0 + 1] = x + y _len_0 = _len_0 + 1
_accum_0[_len_0] = x + y
end end
end end
end end
@ -257,6 +277,7 @@ end
x = function(...) x = function(...)
return (function(...) return (function(...)
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = { local _item_0 = {
... ...
@ -264,7 +285,8 @@ x = function(...)
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
local x = _item_0[_index_0] local x = _item_0[_index_0]
if f(...) > 4 then if f(...) > 4 then
_accum_0[#_accum_0 + 1] = x * x _len_0 = _len_0 + 1
_accum_0[_len_0] = x * x
end end
end end
end end
@ -275,8 +297,10 @@ local normal
normal = function(hello) normal = function(hello)
return (function() return (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for x in yeah do for x in yeah do
_accum_0[#_accum_0 + 1] = x _len_0 = _len_0 + 1
_accum_0[_len_0] = x
end end
return _accum_0 return _accum_0
end)() end)()
@ -285,10 +309,12 @@ local dont_bubble
dont_bubble = function() dont_bubble = function()
return (function() return (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for x in (function(...) for x in (function(...)
return print(...) return print(...)
end)("hello") do end)("hello") do
_accum_0[#_accum_0 + 1] = x _len_0 = _len_0 + 1
_accum_0[_len_0] = x
end end
return _accum_0 return _accum_0
end)() end)()

View File

@ -46,6 +46,7 @@ local hello = {
} }
x = (function() x = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
do do
local _item_0 = hello local _item_0 = hello
for _index_0 = 1, #_item_0 do for _index_0 = 1, #_item_0 do
@ -55,7 +56,8 @@ x = (function()
_value_0 = y _value_0 = y
end end
if _value_0 ~= nil then if _value_0 ~= nil then
_accum_0[#_accum_0 + 1] = _value_0 _len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end end
end end
end end
@ -72,10 +74,12 @@ x = function()
end end
local t = (function() local t = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for i = 10, 20 do for i = 10, 20 do
local _value_0 = i * 2 local _value_0 = i * 2
if _value_0 ~= nil then if _value_0 ~= nil then
_accum_0[#_accum_0 + 1] = _value_0 _len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end end
end end
return _accum_0 return _accum_0
@ -83,11 +87,13 @@ end)()
local hmm = 0 local hmm = 0
local y = (function() local y = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for j = 3, 30, 8 do for j = 3, 30, 8 do
hmm = hmm + 1 hmm = hmm + 1
local _value_0 = j * hmm local _value_0 = j * hmm
if _value_0 ~= nil then if _value_0 ~= nil then
_accum_0[#_accum_0 + 1] = _value_0 _len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end end
end end
return _accum_0 return _accum_0
@ -101,10 +107,12 @@ end
_ = function() _ = function()
return (function() return (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for k = 10, 40 do for k = 10, 40 do
local _value_0 = "okay" local _value_0 = "okay"
if _value_0 ~= nil then if _value_0 ~= nil then
_accum_0[#_accum_0 + 1] = _value_0 _len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end end
end end
return _accum_0 return _accum_0
@ -124,11 +132,13 @@ end
local i = 0 local i = 0
x = (function() x = (function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
while i < 10 do while i < 10 do
i = i + 1 i = i + 1
local _value_0 = i local _value_0 = i
if _value_0 ~= nil then if _value_0 ~= nil then
_accum_0[#_accum_0 + 1] = _value_0 _len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end end
end end
return _accum_0 return _accum_0

View File

@ -91,8 +91,10 @@ x = -hello(world(one(two)))
local hi = -"herfef" local hi = -"herfef"
x = -(function() x = -(function()
local _accum_0 = { } local _accum_0 = { }
local _len_0 = 0
for x in x do for x in x do
_accum_0[#_accum_0 + 1] = x _len_0 = _len_0 + 1
_accum_0[_len_0] = x
end end
return _accum_0 return _accum_0
end)() end)()