-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
270 lines (241 loc) · 6.59 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import * as b from "benny"
// Common types and interfaces
type FieldType = "uint8" | "uint16" | "uint32" | "float32"
type StructData<T> = {
[K in keyof T]: number
}
// Version 1: Using new Function()
class BinaryStructBuilderV1<T extends Record<string, number>> {
private size: number = 0
private fields: Array<{
offset: number
size: number
getter: string
setter: string
}> = []
private fieldMap: Map<string, number> = new Map()
private littleEndian: boolean
constructor(littleEndian: boolean = true) {
this.littleEndian = littleEndian
}
private allocField(type: FieldType): {
offset: number
size: number
getter: string
setter: string
} {
const offset = this.size
let size: number
let getter: string
let setter: string
switch (type) {
case "uint8":
size = 1
getter = "getUint8"
setter = "setUint8"
break
case "uint16":
size = 2
getter = "getUint16"
setter = "setUint16"
break
case "uint32":
size = 4
getter = "getUint32"
setter = "setUint32"
break
case "float32":
size = 4
getter = "getFloat32"
setter = "setFloat32"
break
}
this.size += size
return { offset, size, getter, setter }
}
addField<K extends string>(
name: K,
type: FieldType
): BinaryStructBuilderV1<T & Record<K, number>> {
const field = this.allocField(type)
this.fields.push(field)
this.fieldMap.set(name, this.fields.length - 1)
return this as any
}
build() {
const structSize = this.size
const fields = this.fields
const fieldMap = this.fieldMap
const littleEndian = this.littleEndian
const readFn = new Function(
"buffer",
`
const view = buffer instanceof DataView ? buffer : new DataView(buffer);
return {
${Array.from(fieldMap.entries())
.map(([name, index]) => {
const field = fields[index]
return `${name}: view.${field.getter}(${field.offset}, ${littleEndian})`
})
.join(",\n")}
};
`
) as (buffer: ArrayBuffer | DataView) => StructData<T>
const writeFn = new Function(
"data",
"buffer",
`
const view = buffer instanceof DataView ? buffer :
buffer instanceof ArrayBuffer ? new DataView(buffer) :
new DataView(new ArrayBuffer(${structSize}));
${Array.from(fieldMap.entries())
.map(([name, index]) => {
const field = fields[index]
return `view.${field.setter}(${field.offset}, data.${name}, ${littleEndian});`
})
.join("\n")}
return view.buffer;
`
) as (data: StructData<T>, buffer?: ArrayBuffer | DataView) => ArrayBuffer
return {
size: structSize,
read: readFn,
write: writeFn,
}
}
}
// Version 2: Using regular functions
class BinaryStructBuilderV2<T extends Record<string, number>> {
private size: number = 0
private fields: Array<{
name: string
offset: number
size: number
read: (view: DataView) => number
write: (view: DataView, value: number) => void
}> = []
private littleEndian: boolean
constructor(littleEndian: boolean = true) {
this.littleEndian = littleEndian
}
private allocField(name: string, type: FieldType): void {
const offset = this.size
let size: number
let read: (view: DataView) => number
let write: (view: DataView, value: number) => void
switch (type) {
case "uint8":
size = 1
read = (view) => view.getUint8(offset)
write = (view, value) => view.setUint8(offset, value)
break
case "uint16":
size = 2
read = (view) => view.getUint16(offset, this.littleEndian)
write = (view, value) =>
view.setUint16(offset, value, this.littleEndian)
break
case "uint32":
size = 4
read = (view) => view.getUint32(offset, this.littleEndian)
write = (view, value) =>
view.setUint32(offset, value, this.littleEndian)
break
case "float32":
size = 4
read = (view) => view.getFloat32(offset, this.littleEndian)
write = (view, value) =>
view.setFloat32(offset, value, this.littleEndian)
break
}
this.size += size
this.fields.push({ name, offset, size, read, write })
}
addField<K extends string>(
name: K,
type: FieldType
): BinaryStructBuilderV2<T & Record<K, number>> {
this.allocField(name, type)
return this as any
}
build() {
const structSize = this.size
const fields = this.fields
return {
size: structSize,
read: (buffer: ArrayBuffer | DataView): StructData<T> => {
const view = buffer instanceof DataView ? buffer : new DataView(buffer)
const result: Partial<StructData<T>> = {}
for (const field of fields) {
result[field.name as keyof T] = field.read(view)
}
return result as StructData<T>
},
write: (
data: StructData<T>,
buffer?: ArrayBuffer | DataView
): ArrayBuffer => {
const view =
buffer instanceof DataView
? buffer
: buffer instanceof ArrayBuffer
? new DataView(buffer)
: new DataView(new ArrayBuffer(structSize))
for (const field of fields) {
field.write(view, data[field.name as keyof T])
}
return view.buffer
},
}
}
}
// Benchmark setup
const ComplexStruct = {
id: "uint32",
x: "float32",
y: "float32",
z: "float32",
flags: "uint16",
type: "uint8",
reserved: "uint8",
} as const
const structV1 = Object.entries(ComplexStruct)
.reduce(
(b, [name, type]) => b.addField(name, type as FieldType),
new BinaryStructBuilderV1()
)
.build()
const structV2 = Object.entries(ComplexStruct)
.reduce(
(b, [name, type]) => b.addField(name, type as FieldType),
new BinaryStructBuilderV2()
)
.build()
const data = {
id: 1234,
x: 1.5,
y: 2.5,
z: 3.5,
flags: 0b1010101010101010,
type: 255,
reserved: 0,
}
b.suite(
"Binary Struct Builder Benchmark",
b.add("new Function() ", () => {
const bufferV1 = structV1.write(data)
structV1.read(bufferV1)
}),
b.add("Regular functions", () => {
const bufferV2 = structV2.write(data)
structV2.read(bufferV2)
}),
b.add("JSON", () => {
const stringData = JSON.stringify(data)
JSON.parse(stringData)
}),
b.cycle(),
b.complete(),
b.save({ file: "binary-struct-benchmark", version: "1.0.0" }),
b.save({ file: "binary-struct-benchmark", format: "chart.html" })
)