Thanks @keyhan . I originally had it this way but the CPU spiked to 100% quite easily (now that I think about it, I think I was opening/reading/closing for every chunk). One note though, I’m using file:pread since according to the documentation its faster than manually calling position + read.
Combines position/2 and read/2 in one operation, which is more efficient than calling them one at a time.
In any case, I went back to the drawing board and decided not to load the entire file in RAM. The reason is that I’m running on a very modest server (1vcore, 2GB RAM) and each level/scenario can weight between 1MB all the way to 100MB which would limit how many lobbies I can create quite drastically.
What I ended up doing is, I’m holding the file’s FD in the state and processing the chunks on demand. When I no longer need it, I close the file.
Having the FD, means I don’t have to open/close every time I need to process a chunk.
When I need to send the chunks to a player, I have to split and send many chunks in sizes up to 1KB (I can’t do bigger because it crashes the client) For this, I read ChunksToSend * 1024 from the file, and from that resulting binary I split and send the processed chunks (as opposed to reading 1024, process, repeat).
Will keep you posted. Hopefully it works better now. Thanks everybody!
EDIT: went back and check for this:
I originally had it this way but the CPU spiked to 100% quite easily (now that I think about it, I think I was opening/reading/closing for every chunk)
And indeed, I was opening, reading up to 1024 bytes, process, close for every chunk so yeah, maybe that was the cause of the CPU spikes haha.