Skip to content

Commit 8805454

Browse files
committed
tweak LuaJIT performance.
1 parent 47d1a63 commit 8805454

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

iter.lua

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ local pack = table.pack or function(...) return { n = select('#', ...), ... }
3131
-- A stateful iterator has its own modifiable internal state, the
3232
-- "self" table. It still can not change state in iteration, but it
3333
-- can modify it's "self" table. A stateful iterator should implement
34-
-- "next" callback, accept self table, state and current key to
35-
-- generates next values, by read/write the self table.
34+
-- "next" callback, accept self table and state to generates next
35+
-- values, by read/write the self table.
3636
--
3737
-- Stateful iterator has a "reset" callback that will be called with
3838
-- one or two arguments. If only one argument passed, it initlizes
@@ -62,7 +62,6 @@ Iter.__name = "iterator"
6262
Iter.__index = Iter
6363

6464
-- internal callback defaults
65-
function Iter:next(state, key) return self.iter(state, key) end
6665
function Iter:reset(other)
6766
if other then
6867
for i, base in ipairs(other) do self[i] = base:clone() end
@@ -72,15 +71,23 @@ function Iter:reset(other)
7271
return self
7372
end
7473

75-
local function collect(self, key, ...)
74+
local function collect_current(self, key, ...)
7675
self.current = key
76+
return key, ...
77+
end
78+
79+
function Iter:next(state)
80+
return collect_current(self, self.iter(state, self.current))
81+
end
82+
83+
local function collect(self, key, ...)
7784
if key == nil then self.stopped = true end
7885
return key, ...
7986
end
8087

8188
function Iter:__call()
8289
if self.stopped then return end
83-
return collect(self, self:next(self.state, self.current))
90+
return collect(self, self:next(self.state))
8491
end
8592

8693
function Iter:rewind()
@@ -94,13 +101,11 @@ function Iter:rewind()
94101
end
95102

96103
function Iter:clone()
97-
local new = setmetatable({
98-
state = self.state,
99-
init = self.init,
100-
current = self.current
101-
}, Iter)
104+
local new = setmetatable({ state = self.state }, Iter)
102105
if self.next == Iter.next then
103-
new.iter = self.iter
106+
new.iter = self.iter
107+
new.init = self.init
108+
new.current = self.current
104109
else
105110
new.next = self.next
106111
new.reset = self.reset
@@ -115,8 +120,8 @@ local function new_stateless(func, state, init)
115120
return setmetatable(self, Iter)
116121
end
117122

118-
local function new_stateful(reset, func, state, init)
119-
local self = { reset = reset, next = func, state = state, init = init, current = init }
123+
local function new_stateful(reset, func, state)
124+
local self = { reset = reset, next = func, state = state }
120125
if not state then self.state = self end
121126
return reset(setmetatable(self, Iter)) or self
122127
end
@@ -200,7 +205,7 @@ local function rand(first, last)
200205
end
201206

202207
local function array_reset(self, other)
203-
self.index = other and other.index or self.init
208+
self.index = other and other.index or 0
204209
end
205210

206211
local function array_next(self, state)
@@ -212,7 +217,7 @@ end
212217

213218
local function array(t)
214219
assert(t, "table expected")
215-
return new_stateful(array_reset, array_next, t, 0)
220+
return new_stateful(array_reset, array_next, t)
216221
end
217222

218223
local function resolve1_iter(state, key) if key == nil then return state end end
@@ -430,20 +435,25 @@ local function flatmap(func, base)
430435
return self
431436
end
432437

438+
local function scan_reset(self, other)
439+
Iter.reset(self, other)
440+
self.current = other and other.current or nil
441+
end
442+
433443
local function scan_collect(self, state, key, ...)
434444
if key == nil then return end
435445
if not self.current and not state.acc then
436-
return state.func(key, self[1]())
446+
return collect_current(self, state.func(key, self[1]()))
437447
end
438-
return state.func(self.current or state.acc, key, ...)
448+
return collect_current(self, state.func(self.current or state.acc, key, ...))
439449
end
440450

441451
local function scan_next(self, state)
442452
return scan_collect(self, state, self[1]())
443453
end
444454

445455
local function scan(func, init, base)
446-
local self = new_stateful(Iter.reset, scan_next)
456+
local self = new_stateful(scan_reset, scan_next)
447457
self.state.func = func or id
448458
self.state.acc = init
449459
self[1] = base

0 commit comments

Comments
 (0)