From 46ed01630d109d2a166185f88f23b51c5dc326cf Mon Sep 17 00:00:00 2001 From: kepler155c Date: Mon, 29 Oct 2018 16:04:41 -0400 Subject: [PATCH] milo perf --- milo/Milo.lua | 2 +- milo/MiloRemote.lua | 31 ++++---------- milo/apis/storage.lua | 75 ++++++++++++++++++++++++++-------- milo/plugins/autocraftTask.lua | 1 + milo/plugins/item.lua | 2 + milo/plugins/listing.lua | 14 ++----- milo/plugins/remote.lua | 2 +- 7 files changed, 75 insertions(+), 52 deletions(-) diff --git a/milo/Milo.lua b/milo/Milo.lua index 6e2f61f..c394fa1 100644 --- a/milo/Milo.lua +++ b/milo/Milo.lua @@ -150,7 +150,7 @@ UI:setPage(page) Event.onInterval(5, function() if not Milo:isCraftingPaused() and context.storage:isOnline() then Milo:resetCraftingStatus() - Milo:refreshItems() + --Milo:refreshItems() for _, task in ipairs(context.tasks) do local s, m = pcall(function() task:cycle(context) end) diff --git a/milo/MiloRemote.lua b/milo/MiloRemote.lua index 13cd1e4..da8fc79 100644 --- a/milo/MiloRemote.lua +++ b/milo/MiloRemote.lua @@ -60,6 +60,7 @@ local page = UI.Page { { heading = ' Qty', key = 'count' , width = 4, justify = 'right' }, { heading = 'Name', key = 'displayName' }, }, + values = { }, sortColumn = 'displayName', help = '^(s)tack, ^(a)ll' }, @@ -103,17 +104,6 @@ local page = UI.Page { [ 'control-a' ] = 'eject_all', q = 'quit', - - [ 'control-1' ] = 'eject_1', - [ 'control-2' ] = 'eject_1', - [ 'control-3' ] = 'eject_1', - [ 'control-4' ] = 'eject_1', - [ 'control-5' ] = 'eject_1', - [ 'control-6' ] = 'eject_1', - [ 'control-7' ] = 'eject_1', - [ 'control-8' ] = 'eject_1', - [ 'control-9' ] = 'eject_1', - [ 'control-0' ] = 'eject_1', }, displayMode = 0, } @@ -178,10 +168,6 @@ debug('got response') return response end -function page.statusBar:draw() - return UI.Window.draw(self) -end - function page.grid:getRowTextColor(row, selected) if row.is_craftable then return colors.yellow @@ -195,12 +181,6 @@ end function page.grid:getDisplayValues(row) row = Util.shallowCopy(row) row.count = row.count > 0 and Util.toBytes(row.count) or '' - if row.low then - row.low = Util.toBytes(row.low) - end - if row.limit then - row.limit = Util.toBytes(row.limit) - end return row end @@ -224,6 +204,7 @@ function page:eventHandler(event) local item = self.grid:getSelected() if item then self:setStatus('requesting stack ...') + -- TODO: send a stack request - have server figure out stack size local response = self:sendRequest({ request = 'transfer', item = item, count = 64 }) item.count = response.count self.grid:draw() @@ -233,6 +214,7 @@ function page:eventHandler(event) local item = self.grid:getSelected() if item then self:setStatus('requesting all ...') + -- TODO: let server figure out count local response = self:sendRequest({ request = 'transfer', item = item, count = item.count }) item.count = response.count self.grid:draw() @@ -264,7 +246,6 @@ function page:eventHandler(event) [1] = 'I', [2] = 'C', } - event.button.value = (event.button.value + 1) % 3 self.displayMode = event.button.value event.button.text = values[event.button.value] @@ -306,13 +287,17 @@ function page:applyFilter() self.grid:setValues(t) end +_G.p4 = Event + debug(options.slot) if options.slot.value then debug('Transfer items initialized') Event.addRoutine(function() while true do +debug('sleeping') os.sleep(1.5) local neural = device.neuralInterface +debug(neural) if neural and neural.getInventory then local item = neural.getInventory().getItem(options.slot.value) if item then @@ -321,6 +306,8 @@ if options.slot.value then -- local item = -- TODO: update count for this one item -- page.grid:draw() page:sync() + else +debug('empty') end else debug('missing Introspection module') diff --git a/milo/apis/storage.lua b/milo/apis/storage.lua index 048c86e..e18a752 100644 --- a/milo/apis/storage.lua +++ b/milo/apis/storage.lua @@ -16,6 +16,8 @@ function Storage:init(args) listCount = 0, activity = { }, storageOnline = true, + hits = 0, + misses = 0, } Util.merge(self, defaults) Util.merge(self, args) @@ -29,6 +31,7 @@ debug('%s: %s', e, tostring(dev)) end) Event.onInterval(15, function() self:showStorage() + debug('STORAGE: cache: %d/%d', self.hits, self.misses) end) end @@ -43,7 +46,7 @@ function Storage:showStorage() if #t > 0 then debug('Storage:') for _, k in pairs(t) do - debug(' %s: %s', online and ' online' or 'offline', k) + debug(' offline: ' .. k) end debug('') end @@ -71,6 +74,7 @@ function Storage:initStorage() elseif device[k] and device[k].list and device[k].size and device[k].pullItems then v.adapter = InventoryAdapter.wrap({ side = k }) v.adapter.online = true + v.adapter.dirty = true end if v.mtype == 'storage' then online = online and not not (v.adapter and v.adapter.online) @@ -132,6 +136,10 @@ end function Storage:refresh(throttle) self.dirty = true +debug('STORAGE: Forcing full refresh') + for _, adapter in self:onlineAdapters() do + adapter.dirty = true + end return self:listItems(throttle) end @@ -144,33 +152,35 @@ self.listCount = self.listCount + 1 --debug(self.listCount) -- todo: only listItems from dirty remotes - +local ct = os.clock() local cache = { } local items = { } throttle = throttle or Util.throttle() for _, adapter in self:onlineAdapters() do - adapter:listItems(throttle) + if adapter.dirty then +debug('STORAGE: refresh: ' .. adapter.name) + adapter:listItems(throttle) + adapter.dirty = false + end local rcache = adapter.cache or { } - -- TODO: add a method in each adapter that only updates a passed cache for key,v in pairs(rcache) do - if v.count > 0 then - local entry = cache[key] - if not entry then - entry = Util.shallowCopy(v) - entry.count = v.count - entry.key = key - cache[key] = entry - table.insert(items, entry) - else - entry.count = entry.count + v.count - end - - throttle() + local entry = cache[key] + if not entry then + entry = Util.shallowCopy(v) + entry.count = v.count + entry.key = key + cache[key] = entry + table.insert(items, entry) + else + entry.count = entry.count + v.count end + + throttle() end end +debug('STORAGE: refresh in ' .. (os.clock() - ct)) self.dirty = false self.cache = cache @@ -185,6 +195,29 @@ end function Storage:provide(item, qty, slot, direction) local total = 0 + local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':') + for _, adapter in self:onlineAdapters() do + if adapter.cache and adapter.cache[key] then + local amount = adapter:provide(item, qty, slot, direction or self.localName) + if amount > 0 then + self.hits = self.hits + 1 + debug('EXT: %s(%d): %s -> %s%s', + item.name, amount, adapter.name, direction or self.localName, + slot and string.format('[%d]', slot) or '') + self.dirty = true + adapter.dirty = true + end + qty = qty - amount + total = total + amount + if qty <= 0 then + return total + end + end + end + + debug('miss: %s - %d', key, qty) + self.misses = self.misses + 1 + for _, adapter in self:onlineAdapters() do local amount = adapter:provide(item, qty, slot, direction or self.localName) if amount > 0 then @@ -241,6 +274,14 @@ debug('INS: %s(%d): %s[%d] -> %s', adapter.dirty = true local entry = self.activity[key] or 0 self.activity[key] = entry + amount + +--[[ + local cached = adapter.cache[key] + if cached then + cached.count = cached.count + amount + else + end +]] end qty = qty - amount total = total + amount diff --git a/milo/plugins/autocraftTask.lua b/milo/plugins/autocraftTask.lua index fd59616..50d2f9f 100644 --- a/milo/plugins/autocraftTask.lua +++ b/milo/plugins/autocraftTask.lua @@ -6,6 +6,7 @@ local Autocraft = { priority = 100, } +-- TODO: fix/test function Autocraft:cycle(context) local list = { } diff --git a/milo/plugins/item.lua b/milo/plugins/item.lua index 3ef6200..f7e0879 100644 --- a/milo/plugins/item.lua +++ b/milo/plugins/item.lua @@ -8,6 +8,8 @@ local device = _G.device local context = Milo:getContext() +-- TODO: allow change of machine + local itemPage = UI.Page { titleBar = UI.TitleBar { title = 'Limit Resource', diff --git a/milo/plugins/listing.lua b/milo/plugins/listing.lua index fd04423..aab75c3 100644 --- a/milo/plugins/listing.lua +++ b/milo/plugins/listing.lua @@ -89,23 +89,15 @@ local listingPage = UI.Page { accelerators = { r = 'refresh', [ 'control-r' ] = 'refresh', - q = 'quit', + [ 'control-e' ] = 'eject', [ 'control-s' ] = 'eject_stack', [ 'control-a' ] = 'eject_all', - [ 'control-1' ] = 'eject_1', - [ 'control-2' ] = 'eject_1', - [ 'control-3' ] = 'eject_1', - [ 'control-4' ] = 'eject_1', - [ 'control-5' ] = 'eject_1', - [ 'control-6' ] = 'eject_1', - [ 'control-7' ] = 'eject_1', - [ 'control-8' ] = 'eject_1', - [ 'control-9' ] = 'eject_1', - [ 'control-0' ] = 'eject_1', [ 'control-m' ] = 'machines', [ 'control-l' ] = 'resume', + + q = 'quit', }, displayMode = 0, } diff --git a/milo/plugins/remote.lua b/milo/plugins/remote.lua index e386a17..c30a651 100644 --- a/milo/plugins/remote.lua +++ b/milo/plugins/remote.lua @@ -37,7 +37,7 @@ local function client(socket) end debug('remote: ' .. data.request) if data.request == 'list' then - local items = Milo:refreshItems() + local items = Milo:listItems() Milo:mergeResources(items) socket:write(items)