This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kameko's profile picture

What is a good limit of active computers? + other technical questions

Started by Kameko, 29 May 2015 - 07:29 PM
Kameko #1
Posted 29 May 2015 - 09:29 PM
In a single-player world, is there a certain number of computers one should have in a world before things get laggy? Do computers in unloaded chunks stop running? Is there a hard limit of computers that can run at once? Is every computer it's own thread? Does the program size have anything to do with framerate/memory consumption? Does copypasting a computer in creative mode have any difference from just having two different computers running the same program? I'm making an adventure map and I'm pretty much spamming these things everywhere and I have many concerns about the future of the map. Thank you for your troubles.
MKlegoman357 #2
Posted 29 May 2015 - 10:40 PM
To start off, there is only one computer running only one program in CC at a time. When a computer yields (os.pullEvent, sleep, turtle.*, etc..) the other computers then can continue running their programs. All computers are running in the same thread too. So ComputerCraft doesn't require much resources. The only lag issue I have had was many monitors at the same place, but I mean many. It might be an issue with CC not optimizing them too much but I think the one to blame should be MC itself. Admit it, it's quite slow. So you shouldn't have too many problems with a lot of computers, actually there would probably be more problems with network cables, they seem to lag my machine a bit too, when there are a lot of them. My laptop is probably too slow… But no matter how fast CC can run I'd still suggest using as less computers, monitors and network cables as possible. The game will run slightly faster.
Edited on 29 May 2015 - 08:40 PM
Kaikaku #3
Posted 29 May 2015 - 11:35 PM
… Do computers in unloaded chunks stop running? …
Yes, they do. When the chunk is loaded again the computer will look for a program called startup. If it exists the computer will run it.

… Does copypasting a computer in creative mode have any difference from just having two different computers running the same program? …
So far I did this only for testing (or lazyness) and had no problems with it. For sure this will make updates much easier for you, as you only have to change the program in one directory.

… should have in a world before things get laggy? …
The only thing I experienced CC responsible for lagging is when I execute many thousand command block commands.
Kameko #4
Posted 30 May 2015 - 12:42 AM
Ah, I see, so CC is cooperatively multitasking? It's very smooth.

One more question, it appears computers keep shutting off for me, and I'm not sure why. Is there any reason for this? I tested to see if there was some kind of limit, but I placed more computers than I have in my world beside each other and they were all running (and running an actual program too). The computers just seem to randomly die.
Oh, and I should point out I believe this is due to exiting/entering a world, I'm not sure if it happens when the game is running.
Edited on 29 May 2015 - 11:18 PM
Bomb Bloke #5
Posted 30 May 2015 - 02:36 AM
Does the program size have anything to do with framerate/memory consumption?

Script size doesn't affect framerates, but may have a very minor effect on RAM usage. What'll have a much larger effect on RAM usage is whether you're doing things like eg building tables with millions of elements, which can be done with a single line of code (server side, specifically - there's not much CC can do to eat RAM on the client side).

Likewise, if you build a large monitor and proceed to constantly render all over it, users in the area may experience lag as a result of that (as the new texture data needs to be constantly transmitted to their clients, even if they're not looking at the computer's own in-built display).

In regards to framerate issues (not to be confused with lag issues!), partial blocks like fences, facades, microblocks etc are the biggest offenders there. ComputerCraft won't give you much to worry about in that area.

All computers are running in the same thread too.

More specifically, they get a thread each, but only one is active at a time.

Ah, I see, so CC is cooperatively multitasking? It's very smooth.

Yep, that's exactly it. :)/>

One more question, it appears computers keep shutting off for me, and I'm not sure why. Is there any reason for this? I tested to see if there was some kind of limit, but I placed more computers than I have in my world beside each other and they were all running (and running an actual program too). The computers just seem to randomly die.
Oh, and I should point out I believe this is due to exiting/entering a world, I'm not sure if it happens when the game is running.

If a script fails to yield every ~10 seconds or so (hence allowing a task switch to other systems that might have code to execute), ComputerCraft will attempt to crash it with a "too long without yielding" error. If the script doesn't reference one of the functions CC can hook into and error out of within a few more seconds, then CC will instead shut the whole system off.

Typically you can make a script yield by calling os.pullEvent(), or any function that makes use of os.pullEvent(). sleep(), most turtle-specific functions, many peripheral calls, rednet.receive() and so on are some examples of such functions - basically, anything that "waits" for something to happen before continuing, will probably be yielding to do it.

Computers are also shut down when their chunk unloads, but if a computer was turned off in that manner then it should turn back on when the chunk next loads (executing its startup script from the beginning as it does so, assuming it has one). Sometimes this process bugs out, and computers fail to restart correctly when reloaded. If you think this might be affecting you, perhaps check out the video linked here, see whether you're getting the same log messages?

If you're still not sure what might be going on, post a pastebin link to the code on one of the affected systems.
Kameko #6
Posted 30 May 2015 - 03:26 AM
Hi Bomb Bloke, thanks for the information. It's not a problem with the program, I have about three dozen computers running the same program and most of them are doing fine. But sometimes, I'm not sure when, one or two of them are just off. Didn't crash, didn't shutdown and startup again, they're just shut off and stay that way even if I close the world and open it again, meaning I have to manually turn them on again. I have no idea how to reproduce it, it seems totally random, and happens very rarely too. It's not any one specific computer either.
Bomb Bloke #7
Posted 30 May 2015 - 07:07 AM
Well, given that the "too long without yielding" crash I described may have no symptoms other than shutting the system off, in which case it'll stay off until you manually restart it, how do you know your scripts aren't crashing?
MKlegoman357 #8
Posted 30 May 2015 - 07:46 AM
All computers are running in the same thread too.

More specifically, they get a thread each, but only one is active at a time.

Well, they are running in one Java thread but in separate Lua coroutines (Lua threads).
SquidDev #9
Posted 30 May 2015 - 08:01 AM
Well, they are running in one Java thread but in separate Lua coroutines (Lua threads).

Ah, but behind the scenes each Lua coroutine implemented by a Java thread. But that's just semantics.
Edited on 30 May 2015 - 06:01 AM
MKlegoman357 #10
Posted 30 May 2015 - 08:10 AM
Well, they are running in one Java thread but in separate Lua coroutines (Lua threads).

Ah, but behind the scenes each Lua coroutine implemented by a Java thread. But that's just semantics.

Oh right, I forgot about that :P/>
Kameko #11
Posted 30 May 2015 - 02:03 PM
Well, given that the "too long without yielding" crash I described may have no symptoms other than shutting the system off, in which case it'll stay off until you manually restart it, how do you know your scripts aren't crashing?
A crash only stops execution of the program, I haven't seen one actually shut the computer off. Reloading the world should just make the computers keep their 'on' state but run startup and keep working. I mean, if you want the code it's here: http://pastebin.com/w0rknV41 just a dead simple automatic door. I've had it happen for a few other programs too, but it mostly happened with the door ones just because they're the most prevalent in the map. It's only happened a few times though, and most of the computers are still working.
Edited on 30 May 2015 - 12:03 PM
flaghacker #12
Posted 30 May 2015 - 02:25 PM
Bomb Bloke is saying that computers get shut down in some too long without yielding cases.

That program shouldn't cause that error because commands.exec yields, but the command stuff has been a bit buggy… Try to put "sleep (0)" or even "sleep (1)" in your loop to make sure it yields. If your computers still terminate, then you have probably found a bug.

Could you post some other programs that sometimes crash the computer?
Kameko #13
Posted 30 May 2015 - 05:20 PM
Thanks flaghacker, I'll try that. It's also happened in my secret keypad http://pastebin.com/uKLmKBKG but it's mostly happened on a few of those automatic door computers since I have like 20 of them.

Also, is there any way to find out if something crashed? I want to try and make a logging program to see if stuff didn't yield, if possible.
Edited on 30 May 2015 - 03:36 PM
HDeffo #14
Posted 30 May 2015 - 10:34 PM
… Does copypasting a computer in creative mode have any difference from just having two different computers running the same program? …

I have actually done quite a bit of extensive abuse with ComputerCraft NBT tags which is what you're "copying" exactly. The main notable tags are position and id. Position tags are mainly used to check what peripherals a computer is attached to however depending on the CC version this could also affect the terminal display as well causing it to render on a different computer or not open the display at all. Copy/pasting appears to still correctly change these tags usually if you want them different you'll need to edit them with an NBT editor, so you should be good in this instance.

the tag you'll care more about is ID. This tag tells a computer what ID it is and also what folder to get and put it's programs on the server. When copy/pasting this ID won't change so both computers now share the same ID and thus the same folder/programs. For the most part you can just assume they're the same computer since they both have the same file system. Technically this will save total used space on the server since there's only 1 copy of each program versus just adding the programs to each new computer however ComputerCraft files tend to be really small so for the most part you can just assume there's really no performance advantage either way.
Bomb Bloke #15
Posted 31 May 2015 - 04:06 AM
Certainly one problem that script has is that it only ever yields for as long as it takes for the "testfor" command to complete - then it straight away performs another one, and so on for ever. Even when one of the redstone side inputs is enabled, in which case the command results are entirely ignored, it just keeps on going…

So let's refactor a bit:

local function oc(state)
    redstone.setOutput("back", state)
    redstone.setOutput("front", state)
end

while true do
	if redstone.getInput("left") or redstone.getInput("right") then
		oc(false)
		os.pullEvent("redstone")  -- Yield until a redstone state change occurs.
	else
		oc(commands.exec("testfor @a[r=3]"))
		sleep(1)  -- Bigger sleep here = less server load, but less frequent checks.
	end
end

Unfortunately, you probably won't be able to log yielding-related shutdowns. Worth a try, I suppose; just have your script define a new version of printError, and if an error occurs after that, it should automatically be called:

function printError( ... )
    local counter = 1
    while fs.exists("errorLog"..counter..".txt") do counter = counter + 1 end
    local output = fs.open("errorLog"..counter..".txt", "w")

    for i = 1, #arg do arg[i] = tostring(arg[i]) end
    local thisError = table.concat(arg)
    output.writeLine(thisError)
    output.close()

    if term.isColour() then
        term.setTextColour( colors.red )
    end
    print( thisError )
    if term.isColour() then
        term.setTextColour( colors.white )
    end
end
Kameko #16
Posted 31 May 2015 - 05:19 AM
Wow, thanks Bomb Bloke! That code is much better than mine. Guess I didn't realize commands.exec returns a value.
I'll try the logger too, I'll just make a loop that doesn't yield or something. Thanks for the help.