Skip to content

Commit

Permalink
chore: foundation/src/serialization tech debt (#2722)
Browse files Browse the repository at this point in the history
Fixes #2593 

I marked the `deserializeArrayFromVector` as deprecated because we only
use it in `SiblingPath` and everywhere else we use `BufferReader`.
  • Loading branch information
benesjan authored Oct 6, 2023
1 parent d18349f commit e92154b
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 429 deletions.
180 changes: 0 additions & 180 deletions yarn-project/foundation/src/serialize/deserializer.ts

This file was deleted.

26 changes: 5 additions & 21 deletions yarn-project/foundation/src/serialize/free_funcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,11 @@ export function numToUInt8(n: number) {
}

/**
* Serialize a Buffer into a vector format by encoding the length of the buffer and concatenating it with the original buffer.
* The resulting vector consists of a 4-byte header containing the big-endian representation of the original buffer's length, followed by the original buffer.
* This function is useful when storing buffers as data structures with dynamic lengths and later deserializing them using 'deserializeBufferFromVector'.
*
* @param buf - The input Buffer to be serialized into a vector format.
* @returns A Buffer containing the serialized vector with the encoded length header.
* Adds a 4-byte byte-length prefix to a buffer.
* @param buf - The input Buffer to be prefixed
* @returns A Buffer with 4-byte byte-length prefix.
*/
export function serializeBufferToVector(buf: Buffer) {
export function prefixBufferWithLength(buf: Buffer) {
const lengthBuf = Buffer.alloc(4);
lengthBuf.writeUInt32BE(buf.length, 0);
return Buffer.concat([lengthBuf, buf]);
Expand Down Expand Up @@ -131,20 +128,6 @@ export function serializeDate(date: Date) {
return serializeBigInt(BigInt(date.getTime()), 8);
}

/**
* Deserialize a buffer from a vector by reading the length from its first 4 bytes, and then extracting the contents of the buffer.
* The function returns an object containing the deserialized buffer as 'elem' and the number of bytes advanced ('adv') after deserialization.
*
* @param vector - The input buffer containing the serialized vector.
* @param offset - The starting position from where the deserialization should begin (default is 0).
* @returns An object with the deserialized buffer as 'elem' and the number of bytes advanced ('adv') after deserialization.
*/
export function deserializeBufferFromVector(vector: Buffer, offset = 0) {
const length = vector.readUInt32BE(offset);
const adv = 4 + length;
return { elem: vector.subarray(offset + 4, offset + adv), adv };
}

/**
* Deserialize a boolean value from a given buffer at the specified offset.
* Reads a single byte at the provided offset in the buffer and returns
Expand Down Expand Up @@ -225,6 +208,7 @@ export function serializeBufferArrayToVector(arr: Buffer[]) {
* @param vector - The input buffer containing the serialized array.
* @param offset - An optional starting position in the buffer for deserializing the array.
* @returns An object containing the deserialized array and the total number of bytes used during deserialization (adv).
* @deprecated User BufferReader instead.
*/
export function deserializeArrayFromVector<T>(
deserialize: (
Expand Down
2 changes: 0 additions & 2 deletions yarn-project/foundation/src/serialize/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from './free_funcs.js';
export * from './deserializer.js';
export * from './serializer.js';
export * from './buffer_reader.js';
export * from './types.js';
89 changes: 0 additions & 89 deletions yarn-project/foundation/src/serialize/serialize.test.ts

This file was deleted.

Loading

0 comments on commit e92154b

Please sign in to comment.