Skip to content

Commit

Permalink
Implement reset ser method
Browse files Browse the repository at this point in the history
  • Loading branch information
allevo committed Nov 30, 2023
1 parent 4709fd9 commit 519e9e2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ This library exports the following functions:

- `createSer()`/`createSer({ bufferSize: number })`: creates a new serializer.
- `createDes(buffer)`: creates a new deserializer.
- `ser.reset()`: reset the serializer.
- `ser.serializeBoolean(b)`: serializes a boolean value.
- `des.deserializeBoolean()`: deserializes a boolean value.
- `ser.serializeUInt32(uint32)`: serializes a 32-bit unsigned integer.
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/e2e/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ server.get('/todos', {
}
}, async (request) => {
if (request.headers.accept === 'application/seqproto') {
ser.index = 0
ser.reset()
ser.serializeArray(todos, (ser, todo) => {
ser.serializeUInt32(todo.id)
ser.serializeUInt32(todo.userId)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/isolated/des.benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const type = avro.Type.forSchema({
}
})
const ser = createSer()
ser.index = 0
ser.reset()
ser.serializeArray(todos, (ser, todo) => {
ser.serializeUInt32(todo.id)
ser.serializeUInt32(todo.userId)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/isolated/ser.benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ await b.suite(
'Serialize',

b.add('seqproto', () => {
ser.index = 0
ser.reset()
ser.serializeArray(todos, (ser, todo) => {
ser.serializeUInt32(todo.id)
ser.serializeUInt32(todo.userId)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/isolated/serdes.benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ await b.suite(
'Serialize / Deserialize',

b.add('seqproto', () => {
ser.index = 0
ser.reset()
ser.serializeArray(todos, (ser, todo) => {
ser.serializeUInt32(todo.id)
ser.serializeUInt32(todo.userId)
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Ser {
buffer: ArrayBuffer
uint32Array: Uint32Array
float32Array: Float32Array
reset: () => void
getBuffer: () => StrictArrayBuffer
serializeBoolean: (b: boolean) => void
serializeUInt32: (n: number) => void
Expand Down Expand Up @@ -47,6 +48,7 @@ export function createSer ({ bufferSize }: CreateSerOption = {}): Ser {
buffer,
uint32Array: new Uint32Array(buffer),
float32Array: new Float32Array(buffer),
reset: function () { this.index = 0 },
serializeBoolean,
serializeUInt32,
serializeFloat32,
Expand Down
26 changes: 22 additions & 4 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ await t.test('array', async t => {
})

await t.test('iterable', async t => {
await t.test('map<number, number>', async t => {
await t.test('map<number, number>', async () => {
const expected: Map<number, number> = new Map([[0, 1], [2, 3], [4, 5]])

const ser = createSer()
Expand All @@ -229,7 +229,7 @@ await t.test('iterable', async t => {
assert.deepStrictEqual(m, expected)
})

await t.test('map<string, number>', async t => {
await t.test('map<string, number>', async () => {
const expected: Map<string, number> = new Map([
['foo', 1],
['bar', 3],
Expand All @@ -247,7 +247,7 @@ await t.test('iterable', async t => {
assert.deepStrictEqual(m, expected)
})

await t.test('set<number>', async t => {
await t.test('set<number>', async () => {
const expected: Set<number> = new Set([1, 3, 5])

const ser = createSer()
Expand Down Expand Up @@ -305,6 +305,24 @@ await t.test('setBuffer with options', async t => {
})
})

await t.test('reset', async () => {
const ser = createSer()
ser.serializeUInt32(42)

const des = createDes(ser.getBuffer())
assert.deepStrictEqual(des.deserializeUInt32(), 42)

ser.serializeUInt32(33)
des.setBuffer(ser.getBuffer())
assert.deepStrictEqual(des.deserializeUInt32(), 42)
assert.deepStrictEqual(des.deserializeUInt32(), 33)

ser.reset()
ser.serializeUInt32(11)
des.setBuffer(ser.getBuffer())
assert.deepStrictEqual(des.deserializeUInt32(), 11)
})

function serializeItem (ser: Ser, t: { foo: string, bar: number }): void {
ser.serializeString(t.foo)
ser.serializeUInt32(t.bar)
Expand Down Expand Up @@ -365,7 +383,7 @@ await t.test('serialize + getArrayelements + serialize unsafe + deserialize with
})

await t.test('with option', async t => {
await t.test('bufferSize', async t => {
await t.test('bufferSize', async () => {
{
const ser = createSer({ bufferSize: 4 })
assert.equal(ser.buffer.byteLength, 4)
Expand Down

0 comments on commit 519e9e2

Please sign in to comment.