StructU | Define low-level structs in Luau

github_v2

Latest Update:


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)
14 Likes

There are not enough types, you should definitely add more luau types
just primitive types, thats not enough :face_with_diagonal_mouth:

Although, considering that this is a low-level library, I dont think its necessary maybe.

There are some ideas in my mind on some other lower-level types, but what kind of types would you like to see in the library?

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

If its more low-level, then its definitely a buffer primitive

1 Like

What’s the rationale behind this? I can see how it works but I’m not sure I understand the use cases for it

For serialization and compact use of data when dealing with a lot of objects.

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?

1 Like

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.

1 Like

Update 1.1.0


  • Added int64 and uint64 data types.
  • Made small changes and optimizations.

wow int64 and uint64 in roblox is cool! Good job

1 Like

How would you deserialize the buffer?

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.