mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
perf(iter): make ListIter.totable more efficient (#23714)
This commit is contained in:
parent
2db719f6c2
commit
40db569014
@ -66,7 +66,7 @@ end
|
|||||||
---@class ListIter : Iter
|
---@class ListIter : Iter
|
||||||
---@field _table table Underlying table data
|
---@field _table table Underlying table data
|
||||||
---@field _head number Index to the front of a table iterator
|
---@field _head number Index to the front of a table iterator
|
||||||
---@field _tail number Index to the end of a table iterator
|
---@field _tail number Index to the end of a table iterator (exclusive)
|
||||||
local ListIter = {}
|
local ListIter = {}
|
||||||
ListIter.__index = setmetatable(ListIter, Iter)
|
ListIter.__index = setmetatable(ListIter, Iter)
|
||||||
ListIter.__call = function(self)
|
ListIter.__call = function(self)
|
||||||
@ -321,17 +321,33 @@ end
|
|||||||
|
|
||||||
---@private
|
---@private
|
||||||
function ListIter.totable(self)
|
function ListIter.totable(self)
|
||||||
if self._head == 1 and self._tail == #self._table + 1 and self.next == ListIter.next then
|
if self.next ~= ListIter.next or self._head >= self._tail then
|
||||||
-- Sanitize packed table values
|
return Iter.totable(self)
|
||||||
if getmetatable(self._table[1]) == packedmt then
|
|
||||||
for i = 1, #self._table do
|
|
||||||
self._table[i] = sanitize(self._table[i])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return self._table
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return Iter.totable(self)
|
local needs_sanitize = getmetatable(self._table[1]) == packedmt
|
||||||
|
|
||||||
|
-- Reindex and sanitize.
|
||||||
|
local len = self._tail - self._head
|
||||||
|
|
||||||
|
if needs_sanitize then
|
||||||
|
for i = 1, len do
|
||||||
|
self._table[i] = sanitize(self._table[self._head - 1 + i])
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for i = 1, len do
|
||||||
|
self._table[i] = self._table[self._head - 1 + i]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = len + 1, table.maxn(self._table) do
|
||||||
|
self._table[i] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
self._head = 1
|
||||||
|
self._tail = len + 1
|
||||||
|
|
||||||
|
return self._table
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Fold an iterator or table into a single value.
|
--- Fold an iterator or table into a single value.
|
||||||
|
Loading…
Reference in New Issue
Block a user