Optimization Question about multi-dimensional arrays/tables

I’m making a 3D chunk system and I want to know which is better for performance.

Imagine I have 100x100x100 chunk system all stored on one table.
There’s 2 ways to store the chunk positions

chunk[-15][11][-42].ChunkData

chunk["-15,11,-42"].ChunkData

One is string based, the other is array based.
I would have stuck with the first options but I made it so chunks can have more chunk division (when there is a large amount of objects in it and other reasons) and those chunk divisions can have divisions so it gets really Indexy

chunk[32][-23][53].Division[3][-4][1].Divsion[2][4][1].Division[3][-4][1].ChunkData

Vs

chunk["32,-23,53"].Division["3,-4,1"].Divsion["2,4,1"].Division["3,-4,1"].ChunkData

Just wondering which one is better performance wise for large amounts of chunks.
One doesn’t have to constantly convert to string back to number then to string again.
One doesn’t have to use a large amount of tables and indexing.

AFAIK numbers are lighter than strings plus you would be suffering the effects of type coercion when you need to construct the key to index the table with. Luau is really good with tables and has a lot of optimisations for accessing them. I don’t have enough knowledge to compare the memory consumption however, which may be a point you’re considering for optimisation.

2 Likes

Can’t you store those Data in Vector3?

1 Like

Y̶o̶u̶ c̶a̶n̶n̶o̶t̶ u̶s̶e̶ a̶ v̶e̶c̶t̶o̶r̶3̶ a̶s̶ a̶ t̶a̶b̶l̶e̶ i̶n̶d̶e̶x̶. T̶h̶a̶t̶’s̶ l̶i̶k̶e̶ t̶r̶y̶i̶n̶g̶ t̶o̶ s̶a̶y̶ V̶e̶c̶t̶o̶r̶3̶.n̶e̶w̶(̶)̶ =̶ {̶}̶

you can definitely use Vectors as indexes

local Positions = {
	[Vector3.new(1, 1, 1)] = "First position";
	[Vector3.new(2, 2, 2)] = "Second position";
	[Vector3.new(3, 3, 3)] = "Third position";
}

local TargetPosition = Vector3.new(1, 1, 1)
print(Positions[TargetPosition] or "No position found")
--> First position

doing Vector3.new() = {} is just trying to set the Vector3 equal to an empty table

local TargetPosition = Vector3.new(1, 1, 1)
TargetPosition = {} -- Would be more like this instead
2 Likes

Oh wow, my bad I swore I tried. Would this be better then both of my current options.

Luau has a native Vector3 type. Numbers are still lighter than constructing Vector3s whenever you need to index your dimensional array but they also happen to look nicer from a readability standpoint. To be fair, you might want to fiddle around with collectgarbage("count") to compare the memory usage, but as far as indexing goes table hashes are very fast and I would opt for numbers. Luau has great handling for large tables.

1 Like

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