Hi everyone,
<... threading, lua, performance...>
The age old problem: you think something is slow. Solution: never theorize always profile!
AFAIK: the slow part is accessing a lot of data and/or accessing data that is scattered. This is not lua performance issue. We could read from other thread but then it would probably corrupt the data in process. Also if lua was slow part we could move to luajit with INSANE speed gains.
That out of the way there is already at least 2 threads and you can use more if you want to. The two threads are DF and the console. However if you used it e.g. during world gen you can see how it hangs until DF does reaches a point where it updates the view. So if you want to you can use however many threads you want but only in C++ plugins and usually plugins that do a lot of work are written in that way.
TL;DR: threads are complicated. Stuff is slow because it reads/writes a lot of memory, not because computation.
Thank you so much for the feedback. I do appreciate it. It definitely helps me understand the issue better.
I want to explain the issue I'm having a little more and maybe you or others might have other ideas on how I can approach this problem.
I've been experimenting with visualizing different parts of the game map while the game is actively running. I know that others have done this using sockets or custom C++ plugins to external apps. The approach I took, mainly because of my lack of knowledge on dfhack with C++, was to cycle through the lua data and then use a socket to send the data to an external app. This works ok, but the more I add to the lua script or frequently I run it, the more the FPS take a hit. This is especially an issue if I'm using other mod scripts I wrote.
I had an idea of what some of the issue could be and you helped clarify things better. I do thank you for helping me understand why the memory reads/writes are probably a bigger issue than the processing. I don't know enough about this, but I had thought that the process to process memory access would cause more lag than the process to thread transfer. Any more advice on this would be appreciated.
However, because of the need to wait for other processes to complete, I originally thought threading could help. Especially in cases when running multiple mod scripts and those that aren't that memory heavy and that could improve the user experience by completing more timely.
To my knowledge I have a couple of options.
1. I could just send more of the data to an external app and let it handle more of the processing rather than relying on the dfhack lua. Doing so could allow the code to run by taking advantage of other processing cores. I understand the lag from memory reads will still be there but some processing can occur during the memory/read write times. However, doing so would require a lot of rewriting code and functions that already exist in dfhack LUA API.
2. I could write a C++ plugin that could thread and handle the visualization data. I might be able to use code from others to handle some of the functions. However, I feel that I would be relying too much on my own coding ability to try to handle the table data that LUA already is optimized for.
3. Optimize my code (any other ideas are appreciated).
I know threads are a real pain to work with and can cause all kinds of craziness. For me, it was the first way I could think of that could potentially help the issue and I thought maybe a plugin or something could help.
Thanks again for the feedback though and I don't mean to bother everyone with this . I know there are probably other workarounds, I'm not considering and I have a lot to learn, but I hope at the very least, it can help me and others that might be experiencing similar things gain additional insight or maybe someone else would come up with other solutions.