Hello! Today I’d like to share with you a simple and lightweight struct creation system that I have created for Luau. It allows you to manage your data in a lower-level struct system, allowing you to define how many bytes a certain property will take in your object.
You can visit here to learn more about how to use the library. Feedback is appreciated.
Examples
local StructU = require('@StructU')
local struct = StructU.struct
local float64 = StructU.float64
local vector3 = struct {
x = float64,
y = float64,
z = float64
}
local newVector = vector3 {
x = 0.4,
y = 0.552,
z = 66.66
}
print(newVector.x, newVector.y, newVector.z)
local StructU = require('@StructU')
local struct = StructU.struct
local string = StructU.string
local int16 = StructU.int16
local Person = struct {
Name = string(12),
Gender = string(12),
Age = int16
}
local newPerson = Person {
Name = "Alex",
Gender = "Male",
Age = 18
}
print(newPerson.Name, newPerson.Gender, newPerson.Age)
It would be nice to have higher-level, but efficient types, something like CFrameF32U16, Vector3F32, such functions can really be useful in 90% of cases
Hmm. How many objects? Genuinely, I struggle to see when “buffer + metatable based operations” become the better option, rather than using native datatypes
I can kinda reasonably see the benefit with memory, but the performance of doing operations with this module is gonna be a bit slower, no?
In the scale of thousands. For example in ECS, where you have hundreds of entities, you may wish to store the entity data in a more compact way. Or in another case, if you wish to transfer data across the network faster.
In any way, it will be slower than the native table indexing operation. This is due to the speed in which the buffer reading methods operate. At the moment, the module uses a proxy object to make the indexing system work. If buffer metatables were to be implemented, then the overhead of the proxy object would be removed, thus allowing faster indexing.
That aside, at the moment, per 1000 objects, the indexing operation takes around 100 microseconds. (0.0001 seconds) Versus 10 (0.00001) from native table indexing.
You can index the struct object with the properties that you want to read in order to deserialize them. Or you can manually call each type’s read function yourself.