Skip to content

Commit 8984b60

Browse files
committed
ye
1 parent 1801b01 commit 8984b60

44 files changed

Lines changed: 393 additions & 322 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bench/Run.server.luau

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ local LyncFolder = ReplicatedStorage:WaitForChild ("Lync")
1111
local Lync = require (LyncFolder)
1212
local Scenarios = require (LyncFolder:WaitForChild ("bench"):WaitForChild ("Scenarios"))
1313

14-
Lync.setChannelMaxSize (1048576) -- 1 MB, needed for Blink entities bench (100x6B x 1000 fires)
14+
Lync.setChannelMaxSize (1048576) -- 1 MB, needed for Blink entities bench(100x6B x 1000 fires)
1515
Lync.start ()
1616

17-
-- Config -----------------------------------------------------------------
17+
-- Config --------------------------------------------------------------
1818

1919
local FIRES_PER_FRAME = 1000
2020
local DURATION = 10
@@ -26,7 +26,7 @@ local sort = table.sort
2626
local max = math.max
2727
local min = math.min
2828

29-
-- Helpers ----------------------------------------------------------------
29+
-- Helpers -------------------------------------------------------------
3030

3131
local function getPlayer (): Player
3232
local players = Players:GetPlayers ()
@@ -189,7 +189,7 @@ local function runBlinkTest (config: BlinkTestConfig): ()
189189
print (` Sent={sent} Samples={n}`)
190190
end
191191

192-
-- Main -------------------------------------------------------------------
192+
-- Main ----------------------------------------------------------------
193193

194194
local player = getPlayer ()
195195
Scenarios.Ready:wait ()
@@ -275,7 +275,7 @@ print (`\n\n Blink-Compatible Bench {FIRES_PER_FRAME}/frame {DURATION}s per t
275275
print (" Same data shapes as github.com/1Axen/blink/benchmark\n")
276276

277277
runBlinkTest ({
278-
name = "Entities (100x struct, 6x u8)",
278+
name = "Entities(100x struct, 6x u8)",
279279
player = player,
280280
packet = Scenarios.BlinkEntities,
281281
data = Scenarios.BLINK_ENTITIES,
@@ -284,7 +284,7 @@ runBlinkTest ({
284284
print ("")
285285

286286
runBlinkTest ({
287-
name = "Booleans (1000x bool)",
287+
name = "Booleans(1000x bool)",
288288
player = player,
289289
packet = Scenarios.BlinkBooleans,
290290
data = Scenarios.BLINK_BOOLEANS,

bench/Scenarios.luau

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
local Lync = require (script.Parent.Parent)
66

7-
-- Packets ----------------------------------------------------------------
7+
-- Packets -------------------------------------------------------------
88

99
local Scenarios = {}
1010

1111
Scenarios.Ready = Lync.definePacket ("BenchReady", {
1212
value = Lync.bool,
1313
})
1414

15-
-- Lync -------------------------------------------------------------------
15+
-- Lync ----------------------------------------------------------------
1616

1717
Scenarios.Booleans = Lync.definePacket ("BenchBool", {
1818
value = Lync.bool,

src/Types.luau

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--!strict
22
--!optimize 2
3-
-- Shared type definitions.
3+
-- Shared type definitions for the Lync networking library.
44

55
export type ChannelState = {
66
buff: buffer,
@@ -10,7 +10,7 @@ export type ChannelState = {
1010
lastId: number,
1111
countPos: number,
1212
itemCount: number,
13-
deltas: { [number]: any },
13+
deltas: { [number]: any }, -- per-codec delta caches; shape varies by codec kind
1414
prevDump: buffer?,
1515
}
1616

@@ -19,6 +19,17 @@ export type Codec<T> = {
1919
read: (src: buffer, pos: number, refs: { Instance }?) -> (T, number),
2020
}
2121

22+
-- Extended codec exposing internal fast-path metadata used by composite codecs.
23+
export type InternalCodec<T> = {
24+
write: (ch: ChannelState, value: T) -> (),
25+
read: (src: buffer, pos: number, refs: { Instance }?) -> (T, number),
26+
_size: number?,
27+
_directWrite: ((b: buffer, offset: number, value: T) -> ())?,
28+
_directRead: ((b: buffer, offset: number) -> T)?,
29+
_isDelta: boolean?,
30+
_isBool: boolean?,
31+
}
32+
2233
export type RateLimitConfig = {
2334
maxPerSecond: number,
2435
burstAllowance: number?,

src/api/Group.luau

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
local Players = game:GetService ("Players")
66

7-
-- State ------------------------------------------------------------------
7+
-- State --------------------------------------------------------------
88

99
local _groups = {} :: { [string]: { [Player]: true } }
1010
local _counts = {} :: { [string]: number }
1111
local _playerGroups = {} :: { [Player]: { [string]: true } }
1212

13-
-- Private ----------------------------------------------------------------
13+
-- Private ------------------------------------------------------------
1414

1515
local function onPlayerRemoving (player: Player): ()
1616
local memberships = _playerGroups[player]
@@ -31,8 +31,6 @@ end
3131

3232
Players.PlayerRemoving:Connect (onPlayerRemoving)
3333

34-
-- GroupImpl ---------------------------------------------------------------
35-
3634
local GroupImpl = {}
3735
GroupImpl.__index = GroupImpl
3836

@@ -139,7 +137,7 @@ end
139137

140138
table.freeze (GroupImpl)
141139

142-
-- Public -----------------------------------------------------------------
140+
-- Public -------------------------------------------------------------
143141

144142
local Group = {}
145143

src/api/Namespace.luau

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ type Connection = Types.Connection
1111
type PacketConfig<T> = Types.PacketConfig<T>
1212
type QueryConfig<R, S> = Types.QueryConfig<R, S>
1313

14-
-- Constants --------------------------------------------------------------
14+
-- Constants ----------------------------------------------------------
1515

1616
local MAX_NAMESPACES = 64
1717

18-
-- State ------------------------------------------------------------------
18+
-- State --------------------------------------------------------------
1919

2020
local _registered = {} :: { [string]: true }
2121
local _count = 0
2222

23-
-- Private ----------------------------------------------------------------
23+
-- Private ------------------------------------------------------------
2424

2525
type NamespaceConfig = {
2626
packets: { [string]: PacketConfig<any> }?,
@@ -47,8 +47,6 @@ local function scopedHandler (
4747
end
4848
end
4949

50-
-- Namespace --------------------------------------------------------------
51-
5250
local NamespaceImpl = {}
5351
NamespaceImpl.__index = NamespaceImpl
5452

@@ -173,7 +171,9 @@ function NamespaceImpl.queryNames (self: any): { string }
173171
return result
174172
end
175173

176-
-- Public -----------------------------------------------------------------
174+
table.freeze (NamespaceImpl)
175+
176+
-- Public -------------------------------------------------------------
177177

178178
local Namespace = {}
179179

src/api/Packet.luau

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ local Signal = require (script.Parent.Signal)
1111
local Types = require (script.Parent.Parent.Types)
1212

1313
type Connection = Types.Connection
14+
type InternalCodec<T> = Types.InternalCodec<T>
1415
type PacketConfig<T> = Types.PacketConfig<T>
1516

16-
-- Constants --------------------------------------------------------------
17+
-- Constants ----------------------------------------------------------
1718

1819
local IS_SERVER = RunService:IsServer ()
1920

@@ -24,7 +25,10 @@ local serverWriteToAllExcept = Server.writeToAllExcept
2425
local serverWriteToSet = Server.writeToSet
2526
local clientWrite = Client.write
2627

27-
-- Private ----------------------------------------------------------------
28+
local DELTA_TARGETED = 1
29+
local DELTA_BROADCAST = 2
30+
31+
-- Private ------------------------------------------------------------
2832

2933
-- Shared methods (both contexts)
3034
local SharedImpl = {}
@@ -49,9 +53,6 @@ end
4953
local ServerImpl = setmetatable ({}, { __index = SharedImpl })
5054
ServerImpl.__index = ServerImpl
5155

52-
local DELTA_TARGETED = 1
53-
local DELTA_BROADCAST = 2
54-
5556
local function checkDeltaMode (self: any, mode: number): ()
5657
if not self._isDelta then
5758
return
@@ -122,7 +123,7 @@ end
122123

123124
table.freeze (ClientImpl)
124125

125-
-- Public -----------------------------------------------------------------
126+
-- Public -------------------------------------------------------------
126127

127128
local Packet = {}
128129

@@ -136,7 +137,7 @@ function Packet.define (name: string, config: PacketConfig<any>): any
136137

137138
local isUnreliable = config.unreliable or false
138139

139-
if (config.value :: any)._isDelta and isUnreliable then
140+
if (config.value :: InternalCodec<any>)._isDelta and isUnreliable then
140141
error (`[Lync] Delta codec requires reliable delivery: "{name}"`)
141142
end
142143

@@ -152,7 +153,7 @@ function Packet.define (name: string, config: PacketConfig<any>): any
152153
config.maxPayloadBytes
153154
)
154155

155-
local isDelta = (config.value :: any)._isDelta == true
156+
local isDelta = (config.value :: InternalCodec<any>)._isDelta == true
156157

157158
return setmetatable ({
158159
_id = reg.id,

src/api/Query.luau

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ local Types = require (script.Parent.Parent.Types)
1414
type Connection = Types.Connection
1515
type QueryConfig<R, S> = Types.QueryConfig<R, S>
1616

17-
-- Constants --------------------------------------------------------------
17+
-- Constants ----------------------------------------------------------
1818

1919
local IS_SERVER = RunService:IsServer ()
2020

2121
-- Wire ceiling: correlationId is u16 in Channel.writeQuery / Reader.process.
2222
local POOL_SIZE = 65536
2323

24-
-- State ------------------------------------------------------------------
24+
-- State --------------------------------------------------------------
2525

2626
type PendingQuery = {
2727
thread: thread?,
@@ -33,7 +33,7 @@ local _pending = {} :: { [number]: PendingQuery }
3333
local _nextId = 0
3434
local _activeCount = 0
3535

36-
-- Private ----------------------------------------------------------------
36+
-- Private ------------------------------------------------------------
3737

3838
local function allocCorrelation (): number
3939
if _activeCount >= POOL_SIZE then
@@ -53,7 +53,9 @@ local function allocCorrelation (): number
5353
return id
5454
end
5555

56-
local function freeCorrelation (): ()
56+
-- Decrements the active correlation counter. Does not free a specific slot;
57+
-- that is handled by nil-ing _pending[id] at the call site.
58+
local function releaseCorrelation (): ()
5759
_activeCount -= 1
5860
end
5961

@@ -64,7 +66,7 @@ local function completeQuery (id: number, data: any): ()
6466
end
6567

6668
_pending[id] = nil
67-
freeCorrelation ()
69+
releaseCorrelation ()
6870

6971
if entry.timeout then
7072
task.cancel (entry.timeout)
@@ -158,7 +160,7 @@ local function requestMulti (self: any, players: { Player }, data: any): { [Play
158160
local entry = _pending[cid]
159161
if entry then
160162
_pending[cid] = nil
161-
freeCorrelation ()
163+
releaseCorrelation ()
162164
end
163165
end
164166

@@ -176,8 +178,6 @@ local function requestMulti (self: any, players: { Player }, data: any): { [Play
176178
return coroutine.yield ()
177179
end
178180

179-
-- Query ------------------------------------------------------------------
180-
181181
local QueryImpl = {}
182182
QueryImpl.__index = QueryImpl
183183

@@ -274,7 +274,9 @@ function QueryImpl.requestGroup (self: any, group: any, data: any): { [Player]:
274274
return requestMulti (self, players, data)
275275
end
276276

277-
-- Public -----------------------------------------------------------------
277+
table.freeze (QueryImpl)
278+
279+
-- Public -------------------------------------------------------------
278280

279281
local Query = {}
280282

src/api/Scope.luau

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
--!optimize 2
33
-- Batched connection lifecycle management.
44

5-
-- Private ----------------------------------------------------------------
5+
-- Private ------------------------------------------------------------
66

77
local ScopeImpl = {}
88
ScopeImpl.__index = ScopeImpl
@@ -62,7 +62,7 @@ end
6262

6363
table.freeze (ScopeImpl)
6464

65-
-- Public -----------------------------------------------------------------
65+
-- Public -------------------------------------------------------------
6666

6767
local Scope = {}
6868

src/api/Signal.luau

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ local Types = require (script.Parent.Parent.Types)
66

77
type Connection = Types.Connection
88

9-
-- Listener state: single numeric field for both connected and once checks.
9+
-- Constants ----------------------------------------------------------
10+
1011
local STATE_DISCONNECTED = 0
1112
local STATE_CONNECTED = 1
1213
local STATE_ONCE = 2
1314

14-
-- Private ----------------------------------------------------------------
15+
-- Private ------------------------------------------------------------
1516

1617
local _freeThread: thread? = nil
1718

@@ -36,7 +37,9 @@ local function spawn (fn: (...any) -> (), ...: any): ()
3637
task.spawn (_freeThread :: thread, fn, ...)
3738
end
3839

39-
-- Entry doubles as the Connection handle , 1 alloc per listener instead of 2.
40+
-- Entry doubles as the Connection handle: 1 alloc per listener instead of 2.
41+
-- self: any — metatable typing via typeof(setmetatable) is not practical here
42+
-- because Entry and Signal reference each other circularly.
4043
local EntryImpl = {}
4144
EntryImpl.__index = EntryImpl
4245

@@ -213,7 +216,10 @@ function SignalImpl.disconnectAll (self: any): ()
213216
self._count = 0
214217
end
215218

216-
-- Public -----------------------------------------------------------------
219+
table.freeze (EntryImpl)
220+
table.freeze (SignalImpl)
221+
222+
-- Public -------------------------------------------------------------
217223

218224
local Signal = {}
219225

0 commit comments

Comments
 (0)