Most efficient way to create a log?

I want to make a chatlog that admins can see. but what would be the most efficient way of storing it?

atm i use this bit of code

module.PlayerAdded = function(player)
	player.Chatted:Connect(function(msg)
		table.insert(chatlog,1,{tostring(os.time());player.Name.." ["..player.UserId.."]",msg})
		if #chatlog>5000 then
			chatlog[5001] = nil
		end
    end)
end

every time someone chats it puts the message on top of the table and if we pass 5000 entries it removes the last one

but it had me thinking… if i have 5000 entry`s it needs to move everything in the table 1 place down.
i could add it to the last entry and use table.flip() to get the latest. but at 5000 entries you get the same issue

i am curious if there is a more optimised way

1 Like

you can use the table.remove
table.remove(1, table) to remove the first index value in a array

and theres a lot of resources of making an efficient data storing like datastore2

Circular buffer is the perfect data structure for this.

2 Likes

thank you for the reply. but I never mentioned datastore
and yeah I know about table.remove. but is there a less resource-intensive way?

local function CirculairBuffer (Table)
	Table.index=0
	local metatable = {}
	metatable.__call=function (MyTable,Value)
		MyTable.index += 1
		if MyTable.index > 5000 then
			MyTable.index = 1
		end
		MyTable[MyTable.index]=Value
	end
	setmetatable(Table,metatable)
end

any way to optimize it further??

There aren’t any issues I can see that might cause performance problems. You can do the index logic using % operator, don’t know if that’s faster than an if statement. Only way to know for sure is to test it.

Your implementation doesn’t have any ways of reading from the buffer, you’ll definitely need that.

the log is going to be serverside.
with a command, it sends the entire log to the client.

there I create a GUI with a list of all the inputs.

it will start to read at the index (newest entry) and go back to gain older messages

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.