perf(iter): make ListIter.totable more efficient (#23714)

This commit is contained in:
Lewis Russell 2023-06-03 11:06:10 +01:00 committed by GitHub
parent 2db719f6c2
commit 40db569014
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -66,7 +66,7 @@ end
---@class ListIter : Iter
---@field _table table Underlying table data
---@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 = {}
ListIter.__index = setmetatable(ListIter, Iter)
ListIter.__call = function(self)
@ -321,17 +321,33 @@ end
---@private
function ListIter.totable(self)
if self._head == 1 and self._tail == #self._table + 1 and self.next == ListIter.next then
-- Sanitize packed table values
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
if self.next ~= ListIter.next or self._head >= self._tail then
return Iter.totable(self)
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
--- Fold an iterator or table into a single value.