milo: improve export speed

This commit is contained in:
kepler155c@gmail.com
2019-04-03 17:13:20 -04:00
parent ad165825bd
commit 1c30ccda29
3 changed files with 82 additions and 63 deletions

View File

@@ -2,6 +2,7 @@ local itemDB = require('core.itemDB')
local Util = require('util') local Util = require('util')
local fs = _G.fs local fs = _G.fs
local parallel = _G.parallel
local turtle = _G.turtle local turtle = _G.turtle
local Craft = { local Craft = {
@@ -147,6 +148,7 @@ local function turtleCraft(recipe, storage, request, count)
parallel.waitForAll(table.unpack(fns)) parallel.waitForAll(table.unpack(fns))
if failed then if failed then
Craft.clearGrid(storage)
return return
end end

View File

@@ -412,12 +412,18 @@ function Storage:export(target, slot, count, item)
local total = 0 local total = 0
local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':') local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':')
local function provide(adapter) local function provide(adapter, pcount)
local amount = rawExport(adapter, target.adapter, item, count, slot) -- update cache before export to allow for simultaneous calls
self:updateCache(adapter, item, -pcount)
local amount = rawExport(adapter, target.adapter, item, pcount, slot)
if amount ~= pcount then
-- this *should* only happen if cache is out of sync
self:updateCache(adapter, item, pcount - amount)
end
if amount > 0 then if amount > 0 then
self:updateCache(adapter, item, -amount)
_G._debug('EXT: %s(%d): %s -> %s%s in %s', _G._debug('EXT: %s(%d): %s -> %s%s in %s',
item.displayName or item.name, amount, self:_sn(adapter.name), self:_sn(target.name), item.displayName or item.name, amount, self:_sn(adapter.name), self:_sn(target.name),
slot and string.format('[%d]', slot) or '[*]', Util.round(timer(), 2)) slot and string.format('[%d]', slot) or '[*]', Util.round(timer(), 2))
@@ -428,8 +434,9 @@ function Storage:export(target, slot, count, item)
-- request from adapters with this item -- request from adapters with this item
for _, adapter in self:onlineAdapters() do for _, adapter in self:onlineAdapters() do
if adapter.cache and adapter.cache[key] then local cache = adapter.cache and adapter.cache[key]
provide(adapter) if cache then
provide(adapter, math.min(count, cache.count))
if count <= 0 then if count <= 0 then
return total return total
end end

View File

@@ -1,6 +1,8 @@
local itemDB = require('core.itemDB') local itemDB = require('core.itemDB')
local Milo = require('milo') local Milo = require('milo')
local parallel = _G.parallel
local ExportTask = { local ExportTask = {
name = 'exporter', name = 'exporter',
priority = 40, priority = 40,
@@ -11,7 +13,10 @@ local function filter(a)
end end
function ExportTask:cycle(context) function ExportTask:cycle(context)
local tasks = { }
for node in context.storage:filterActive('machine', filter) do for node in context.storage:filterActive('machine', filter) do
table.insert(tasks, function()
local s, m = pcall(function() local s, m = pcall(function()
for _, entry in pairs(node.exports) do for _, entry in pairs(node.exports) do
@@ -79,9 +84,14 @@ function ExportTask:cycle(context)
end end
end end
end) end)
if not s and m then if not s and m then
_G._debug('EXPORTER error: ' .. m) _G._debug('EXPORTER error: ' .. m)
end end
end)
end
if #tasks > 0 then
parallel.waitForAll(table.unpack(tasks))
end end
end end