Skip to content

Commit c4b52e0

Browse files
committed
Convert from "deferred write" to resizing
1 parent ee76dca commit c4b52e0

24 files changed

Lines changed: 371 additions & 346 deletions

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ This project uses [semantic versioning](https://semver.org/spec/v2.0.0.html).
44

55
---
66

7+
## version 0.3.1
8+
9+
### Improvements
10+
- Rewrote serialization to use an allocator w/ resizing instead of using "deferred write" functions. Should be an incredibly large performance boost.
11+
712
## version 0.3.0
813

914
### Added

dev/server/serverTests.server.luau

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ local data = {
1818
}
1919

2020
RunService.Heartbeat:Connect(function()
21-
--[[for i = 1, 100 do
22-
ReplicatedStorage.RemoteEvent:FireAllClients({
23-
a = false,
24-
b = { true, false, false },
25-
c = { { false }, { true } },
26-
d = nil,
27-
e = { [8] = 4 },
28-
f = {
29-
[Vector3.new(15, 15, 15)] = 2554,
30-
},
31-
})
32-
end]]
3321
debug.profilebegin("send")
3422
for _ = 1, 100 do
3523
testPackets.a:sendToAll(data)

src/dataTypes/array.luau

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,41 @@ local bufferWriter = require(script.Parent.Parent.process.bufferWriter)
22
local types = require(script.Parent.Parent.types)
33

44
local u16 = bufferWriter.u16
5+
local alloc = bufferWriter.alloc
56

67
--[[
78
Create a new array with the given dataTypeInterface
89
]]
9-
return function(valueType: types.dataTypeInterface<any>)
10-
local valueWrite = valueType.write
11-
12-
return {
13-
read = function(b: buffer, cursor: number)
14-
local arrayLength = buffer.readu16(b, cursor)
15-
local arrayCursor = cursor + 2
16-
local array = {}
17-
18-
for _ = 1, arrayLength do
19-
local item, length = valueType.read(b, arrayCursor)
20-
table.insert(array, item)
21-
22-
arrayCursor += length
23-
end
24-
25-
return array, (arrayCursor - cursor)
26-
end,
27-
write = function(cursor: number, value: any)
28-
u16(cursor, #value) -- write length, 2 bytes
29-
local arrayCursor = cursor + 2
30-
31-
for _, item in value do
32-
-- add the length of the item to the cursor
33-
arrayCursor += valueWrite(arrayCursor, item)
34-
end
35-
36-
-- return size of the array: arrayCursor - cursor = size
37-
return arrayCursor - cursor
38-
end,
39-
}
40-
end :: <T>(valueType: types.dataTypeInterface<T>) -> types.dataTypeInterface<{ [number]: T }>
10+
return function(
11+
valueType: types.dataTypeInterface<
12+
any
13+
>
14+
)
15+
local valueWrite = valueType.write
16+
17+
return {
18+
read = function(b: buffer, cursor: number)
19+
local arrayLength = buffer.readu16(b, cursor)
20+
local arrayCursor = cursor + 2
21+
local array = {}
22+
23+
for _ = 1, arrayLength do
24+
local item, length = valueType.read(b, arrayCursor)
25+
table.insert(array, item)
26+
27+
arrayCursor += length
28+
end
29+
30+
return array, (arrayCursor - cursor)
31+
end,
32+
write = function(value: any)
33+
alloc(2)
34+
u16(#value) -- write length, 2 bytes
35+
36+
for _, item in value do
37+
-- add the length of the item to the cursor
38+
valueWrite(item)
39+
end
40+
end,
41+
}
42+
end :: <T>(valueType: types.dataTypeInterface<T>) -> types.dataTypeInterface<{ [number]: T }>

src/dataTypes/bool.luau

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local types = require(script.Parent.Parent.types)
33

44
local btrue = bufferWriter.btrue
55
local bfalse = bufferWriter.bfalse
6+
local alloc = bufferWriter.alloc
67

78
local bool = {
89
--[[
@@ -14,8 +15,14 @@ local bool = {
1415
read = function(b: buffer, cursor: number)
1516
return buffer.readu8(b, cursor) == 1, 1
1617
end,
17-
write = function(cursor: number, value: boolean)
18-
return if value then btrue(cursor) else bfalse(cursor)
18+
write = function(value: boolean)
19+
alloc(1)
20+
21+
if value then
22+
btrue()
23+
else
24+
bfalse()
25+
end
1926
end,
2027
}
2128

src/dataTypes/buff.luau

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local types = require(script.Parent.Parent.types)
33

44
local u16 = bufferWriter.u16
55
local copy = bufferWriter.copy
6+
local dyn_alloc = bufferWriter.dyn_alloc
67

78
local buff = {
89
read = function(b: buffer, cursor: number)
@@ -14,14 +15,13 @@ local buff = {
1415

1516
return freshBuffer, length + 2
1617
end,
17-
write = function(cursor: number, data: buffer)
18+
write = function(data: buffer)
1819
local length = buffer.len(data)
20+
dyn_alloc(length + 2)
1921

2022
-- write the length of the buffer, then the buffer itself
21-
u16(cursor, length)
22-
copy(cursor + 2, data)
23-
24-
return length + 2
23+
u16(length)
24+
copy(data)
2525
end,
2626
}
2727

src/dataTypes/cframe.luau

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local bufferWriter = require(script.Parent.Parent.process.bufferWriter)
22
local types = require(script.Parent.Parent.types)
33

44
local f32 = bufferWriter.f32
5+
local alloc = bufferWriter.alloc
56

67
-- thanks jack :p
78
local cframe = {
@@ -19,7 +20,7 @@ local cframe = {
1920

2021
return CFrame.fromAxisAngle(axis, angle) + Vector3.new(x, y, z), 24
2122
end,
22-
write = function(cursor: number, value: CFrame)
23+
write = function(value: CFrame)
2324
-- Convert the CFrame to an axis-angle representation
2425
local x, y, z = value.X, value.Y, value.Z
2526

@@ -28,14 +29,13 @@ local cframe = {
2829
axis = axis * angle
2930

3031
-- Math done, write it now
31-
f32(cursor, x)
32-
f32(cursor + 4, y)
33-
f32(cursor + 8, z)
34-
f32(cursor + 12, rx)
35-
f32(cursor + 16, ry)
36-
f32(cursor + 20, rz)
37-
38-
return 24
32+
alloc(24)
33+
f32(x)
34+
f32(y)
35+
f32(z)
36+
f32(rx)
37+
f32(ry)
38+
f32(rz)
3939
end,
4040
}
4141

src/dataTypes/float32.luau

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ local bufferWriter = require(script.Parent.Parent.process.bufferWriter)
22
local types = require(script.Parent.Parent.types)
33

44
local f32 = bufferWriter.f32
5+
local alloc = bufferWriter.alloc
56

67
local float32 = {
7-
write = f32,
8+
write = function(value: number)
9+
alloc(4)
10+
f32(value)
11+
end,
812

913
read = function(b: buffer, cursor: number)
1014
return buffer.readf32(b, cursor), 4

src/dataTypes/float64.luau

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ local bufferWriter = require(script.Parent.Parent.process.bufferWriter)
22
local types = require(script.Parent.Parent.types)
33

44
local f64 = bufferWriter.f64
5+
local alloc = bufferWriter.f64
56

67
local float64 = {
7-
write = f64,
8+
write = function(value: number)
9+
alloc(8)
10+
f64(value)
11+
end,
812

913
read = function(b: buffer, cursor: number)
1014
return buffer.readf64(b, cursor), 8

src/dataTypes/int16.luau

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ local bufferWriter = require(script.Parent.Parent.process.bufferWriter)
22
local types = require(script.Parent.Parent.types)
33

44
local i16 = bufferWriter.i16
5+
local alloc = bufferWriter.alloc
56

67
local int16 = {
7-
write = i16,
8+
write = function(value: number)
9+
alloc(8)
10+
i16(value)
11+
end,
812

913
read = function(b: buffer, cursor: number)
1014
return buffer.readi16(b, cursor), 2

src/dataTypes/int32.luau

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ local bufferWriter = require(script.Parent.Parent.process.bufferWriter)
22
local types = require(script.Parent.Parent.types)
33

44
local i32 = bufferWriter.i32
5+
local alloc = bufferWriter.alloc
56

67
local int32 = {
7-
write = i32,
8+
write = function(value: number)
9+
alloc(4)
10+
i32(value)
11+
end,
812

913
read = function(b: buffer, cursor: number)
1014
return buffer.readi32(b, cursor), 4

0 commit comments

Comments
 (0)