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

Computer space limit and writing to file

Started by grabie2, 02 March 2013 - 11:29 AM
grabie2 #1
Posted 02 March 2013 - 12:29 PM
Hello!

I found a big issue with computer space limitation:
The more files you have on computer the longer it's going to take to write something to disk.

Steps to reproduce:
- make sure that you have "I:computerSpaceLimit" in config set to something other than 0
- create 10 files in computer main directory
- run this lua code:

local file = fs.open("test.bin", "wb")
for i = 1, 1024, 1 do file.write(i % 256) end
file.close()

Then turn off your minecraft instance
- set "I:computerSpaceLimit" to 0
- run this lua code again

For me the first time it errored "too long without yelling"
the second time was about 10 ms

From my tests:
- file 525 bytes long
- script copying it to new file 100 times
- 1st iteration - writing time 1.25 sec
- 20th iteration 2.6 sec
- 78th iteration - 4.95 sec
- 79th iteration - too long without yelling

It's not effecting reading time, tho.
I have idea why it's working like that, but it can't work like that.

Thanks for solving this issue!
Cranium #2
Posted 02 March 2013 - 01:05 PM
This is not a bug. It's due to the fact that you have 1,024 files open at once, and once that happens, it lags your computer horribly. Try putting the file.close() within the loop. It will not cause that same error.
Cloudy #3
Posted 02 March 2013 - 02:38 PM
No real way round it. It's take the speed hit or no protection at all.
grabie2 #4
Posted 02 March 2013 - 08:52 PM
This is not a bug. It's due to the fact that you have 1,024 files open at once, and once that happens, it lags your computer horribly. Try putting the file.close() within the loop. It will not cause that same error.

It's not opening 1024 files at once, I'm opening file, writing 1024 bytes and closing file…

@Cloudy
Maybe change the protection mechanism, for example, calculate free space when all file handles are closed and when some handle opens and writes to file decrease that number, once it hits 0 then lock writing ?
Cloudy #5
Posted 02 March 2013 - 10:13 PM
I already do that, but I need to check used space every time you write in case it is written to elsewhere.
immibis #6
Posted 02 March 2013 - 11:50 PM
I would think that if someone can edit the computer's data folder, then accurately limiting their disk space is not a priority as they can bypass it anyway by creating files outside the game.
Cloudy #7
Posted 03 March 2013 - 12:36 AM
I'll try and think of a nicer solution next time I delve into the Filesystem code.