Skip to content

Movegoods fix - #1655

Open
ab9rf wants to merge 6 commits into
DFHack:masterfrom
ab9rf:movegoods-fix
Open

ab9rf wants to merge 6 commits into
DFHack:masterfrom
ab9rf:movegoods-fix

Conversation

@ab9rf

@ab9rf ab9rf commented Nov 29, 2026

Copy link
Copy Markdown
Member

rewrite the UI and backend processing for movegoods to avoid repeatedly retraversing the item list while (un)marking items for trade

also use a coroutine to ensure the frame pump continues to run when (un)marking a large number of items

additional optimizations added to try to reduce the startup time especially in large forts

fixes DFHack/dfhack#6050

rewrite the UI and backend processing for `movegoods` to avoid repeatedly retraversing the item list while (un)marking items for trade

also use a coroutine to ensure the frame pump continues to run when (un)marking a large number of items

fixes DFHack/dfhack#6050
item scan in the test fort i am using was taking ~6s. these changes trim it to about ~2s which is a significant improvement

will look into why dfhack.item.* methods are so slow to invoke (~95-100 microseconds per call) - there might be a core issue here that can be mitigated but that'll be done separately
mark preservation was not working correctly in conjunction with item grouping, caused a CI test failure
test got desynced from implementation

@SilasD SilasD left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally, the scan_queued looks over-engineered and a bit confusing.

  • as far as I can tell, scan_queued is a hash table that can be empty, or can contain a single key, either true or false, with a value of true. that key is the value to use for inside_containers for the queued scan.
  • then there is a separate scan hash table, that contains (at least) a key inside_containers with the value being used on the currently in-progress scan.
  • the current inside_containers setting is sometimes taken directly from scan, and sometimes passed into functions as a parameter.

it all feels a bit inconsistent. and clunky. I'm sure it works though.

if you do choose to re-engineer it, perhaps settle on the scan hash table and all the keys it can use (I think this is currently only inside_containers), and make scan_queued into a table (or preferrably a list) of pending scan settings.
then starting the next scan is just a case of getting the next scan parameters from the table or list.
Something like

local next_scan_settings = { inside_containers = inside_containers }
table.insert(scan_queued, next_scan_settings)

and

self.scan = table.remove(scan_queued, 1)

would be ideal, as it would make the thing into a real queue.
(note that you can table.remove() from an empty table; it returns nil instead of throwing.)
this would still be over-engineered, but it would be clearer and would be trivially extendable to support scan parameters other than inside_containers. the current implementation would require rewriting to support other scan parameters.

neat coroutine idea. I can't quite tell if the game is forced paused during the asynchronous processing, so I'll just mention that it needs to be.
(later) especially as the code keeps references to items across yields.

I'm burned out, but I hope to get back to this.

Comment thread internal/caravan/movegoods.lua Resolved
Comment thread internal/caravan/movegoods.lua Resolved
Comment thread internal/caravan/common.lua Resolved
Comment thread internal/caravan/common.lua Resolved
Comment thread internal/caravan/common.lua Resolved
Comment thread internal/caravan/movegoods.lua Resolved
Comment thread internal/caravan/movegoods.lua Resolved
Comment thread internal/caravan/movegoods.lua Resolved
Comment on lines +394 to +395
local pos = item.pos
return pos.x ~= -30000 and pos or nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reminder that this is returning a live reference to an item's internal fields.
if that item goes away over a yield(), it'll be a stale reference.

I'm not alert enough right now to check if that's a problem.

Comment thread internal/caravan/movegoods.lua Resolved
ab9rf added 2 commits December 3, 2026 17:30
removed some unnecessary bypasses based on better empirical profiling data

also fixed a defect related to tracking the "risky" indicator

this version loads around 80,000 items in less than two seconds (about a second faster than the previous evolution) and has no discernable lag when selecting or deselecting even large numbers of items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dfhack trade interface is extremely slow when doing bulk trade marking or unmarking

2 participants